@@ -2,18 +2,17 @@ use anyhow::Result;
2
2
use ruff_diagnostics:: { AlwaysFixableViolation , Diagnostic , Edit , Fix } ;
3
3
use ruff_macros:: { derive_message_formats, ViolationMetadata } ;
4
4
use ruff_python_ast:: { self as ast, Expr } ;
5
- use ruff_python_codegen:: Stylist ;
6
5
use ruff_python_parser:: { TokenKind , Tokens } ;
7
6
use ruff_python_stdlib:: open_mode:: OpenMode ;
8
7
use ruff_text_size:: { Ranged , TextSize } ;
9
8
10
9
use crate :: checkers:: ast:: Checker ;
11
10
12
11
/// ## What it does
13
- /// Checks for redundant `open` mode parameters .
12
+ /// Checks for redundant `open` mode arguments .
14
13
///
15
14
/// ## Why is this bad?
16
- /// Redundant `open` mode parameters are unnecessary and should be removed to
15
+ /// Redundant `open` mode arguments are unnecessary and should be removed to
17
16
/// avoid confusion.
18
17
///
19
18
/// ## Example
@@ -40,18 +39,18 @@ impl AlwaysFixableViolation for RedundantOpenModes {
40
39
fn message ( & self ) -> String {
41
40
let RedundantOpenModes { replacement } = self ;
42
41
if replacement. is_empty ( ) {
43
- "Unnecessary open mode parameters " . to_string ( )
42
+ "Unnecessary mode argument " . to_string ( )
44
43
} else {
45
- format ! ( "Unnecessary open mode parameters , use \" {replacement}\" " )
44
+ format ! ( "Unnecessary modes , use ` {replacement}` " )
46
45
}
47
46
}
48
47
49
48
fn fix_title ( & self ) -> String {
50
49
let RedundantOpenModes { replacement } = self ;
51
50
if replacement. is_empty ( ) {
52
- "Remove open mode parameters " . to_string ( )
51
+ "Remove mode argument " . to_string ( )
53
52
} else {
54
- format ! ( "Replace with \" {replacement}\" " )
53
+ format ! ( "Replace with ` {replacement}` " )
55
54
}
56
55
}
57
56
}
@@ -71,68 +70,71 @@ pub(crate) fn redundant_open_modes(checker: &mut Checker, call: &ast::ExprCall)
71
70
return ;
72
71
}
73
72
74
- let Some ( mode_param ) = call. arguments . find_argument_value ( "mode" , 1 ) else {
73
+ let Some ( mode_arg ) = call. arguments . find_argument_value ( "mode" , 1 ) else {
75
74
return ;
76
75
} ;
77
- let Expr :: StringLiteral ( ast:: ExprStringLiteral { value, .. } ) = & mode_param else {
76
+ let Expr :: StringLiteral ( ast:: ExprStringLiteral { value, .. } ) = & mode_arg else {
78
77
return ;
79
78
} ;
80
79
let Ok ( mode) = OpenMode :: from_chars ( value. chars ( ) ) else {
81
80
return ;
82
81
} ;
83
82
let reduced = mode. reduce ( ) ;
84
83
if reduced != mode {
85
- checker. diagnostics . push ( create_diagnostic (
86
- call,
87
- mode_param,
88
- reduced,
89
- checker. tokens ( ) ,
90
- checker. stylist ( ) ,
91
- ) ) ;
84
+ checker
85
+ . diagnostics
86
+ . push ( create_diagnostic ( call, mode_arg, reduced, checker) ) ;
92
87
}
93
88
}
94
89
95
90
fn create_diagnostic (
96
91
call : & ast:: ExprCall ,
97
- mode_param : & Expr ,
92
+ mode_arg : & Expr ,
98
93
mode : OpenMode ,
99
- tokens : & Tokens ,
100
- stylist : & Stylist ,
94
+ checker : & Checker ,
101
95
) -> Diagnostic {
96
+ let range = if checker. settings . preview . is_enabled ( ) {
97
+ mode_arg. range ( )
98
+ } else {
99
+ call. range
100
+ } ;
101
+
102
102
let mut diagnostic = Diagnostic :: new (
103
103
RedundantOpenModes {
104
104
replacement : mode. to_string ( ) ,
105
105
} ,
106
- call . range ( ) ,
106
+ range,
107
107
) ;
108
108
109
109
if mode. is_empty ( ) {
110
- diagnostic
111
- . try_set_fix ( || create_remove_param_fix ( call, mode_param, tokens) . map ( Fix :: safe_edit) ) ;
110
+ diagnostic. try_set_fix ( || {
111
+ create_remove_argument_fix ( call, mode_arg, checker. tokens ( ) ) . map ( Fix :: safe_edit)
112
+ } ) ;
112
113
} else {
114
+ let stylist = checker. stylist ( ) ;
113
115
diagnostic. set_fix ( Fix :: safe_edit ( Edit :: range_replacement (
114
116
format ! ( "{}{mode}{}" , stylist. quote( ) , stylist. quote( ) ) ,
115
- mode_param . range ( ) ,
117
+ mode_arg . range ( ) ,
116
118
) ) ) ;
117
119
}
118
120
119
121
diagnostic
120
122
}
121
123
122
- fn create_remove_param_fix (
124
+ fn create_remove_argument_fix (
123
125
call : & ast:: ExprCall ,
124
- mode_param : & Expr ,
126
+ mode_arg : & Expr ,
125
127
tokens : & Tokens ,
126
128
) -> Result < Edit > {
127
- // Find the last comma before mode_param and create a deletion fix
128
- // starting from the comma and ending after mode_param .
129
+ // Find the last comma before mode_arg and create a deletion fix
130
+ // starting from the comma and ending after mode_arg .
129
131
let mut fix_start: Option < TextSize > = None ;
130
132
let mut fix_end: Option < TextSize > = None ;
131
133
let mut is_first_arg: bool = false ;
132
134
let mut delete_first_arg: bool = false ;
133
135
134
136
for token in tokens. in_range ( call. range ( ) ) {
135
- if token. start ( ) == mode_param . start ( ) {
137
+ if token. start ( ) == mode_arg . start ( ) {
136
138
if is_first_arg {
137
139
delete_first_arg = true ;
138
140
continue ;
0 commit comments