Skip to content

Commit 5a95b25

Browse files
authored
add auto-fix for E252 (#8142)
## Summary Introduce auto fix for `E252`. This partially address #8121. ## Test Plan Already covered.
1 parent 8338143 commit 5a95b25

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/whitespace_around_named_parameter_equals.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ruff_diagnostics::Violation;
1+
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, Violation};
22
use ruff_macros::{derive_message_formats, violation};
33
use ruff_python_parser::TokenKind;
44
use ruff_text_size::{Ranged, TextRange, TextSize};
@@ -69,11 +69,15 @@ impl Violation for UnexpectedSpacesAroundKeywordParameterEquals {
6969
#[violation]
7070
pub struct MissingWhitespaceAroundParameterEquals;
7171

72-
impl Violation for MissingWhitespaceAroundParameterEquals {
72+
impl AlwaysFixableViolation for MissingWhitespaceAroundParameterEquals {
7373
#[derive_message_formats]
7474
fn message(&self) -> String {
7575
format!("Missing whitespace around parameter equals")
7676
}
77+
78+
fn fix_title(&self) -> String {
79+
format!("Add missing whitespace")
80+
}
7781
}
7882

7983
fn is_in_def(tokens: &[LogicalLineToken]) -> bool {
@@ -131,7 +135,13 @@ pub(crate) fn whitespace_around_named_parameter_equals(
131135
if annotated_func_arg && parens == 1 {
132136
let start = token.start();
133137
if start == prev_end && prev_end != TextSize::new(0) {
134-
context.push(MissingWhitespaceAroundParameterEquals, token.range());
138+
let mut diagnostic =
139+
Diagnostic::new(MissingWhitespaceAroundParameterEquals, token.range);
140+
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
141+
" ".to_string(),
142+
token.start(),
143+
)));
144+
context.push_diagnostic(diagnostic);
135145
}
136146

137147
while let Some(next) = iter.peek() {
@@ -141,7 +151,15 @@ pub(crate) fn whitespace_around_named_parameter_equals(
141151
let next_start = next.start();
142152

143153
if next_start == token.end() {
144-
context.push(MissingWhitespaceAroundParameterEquals, token.range());
154+
let mut diagnostic = Diagnostic::new(
155+
MissingWhitespaceAroundParameterEquals,
156+
token.range,
157+
);
158+
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
159+
" ".to_string(),
160+
token.end(),
161+
)));
162+
context.push_diagnostic(diagnostic);
145163
}
146164
break;
147165
}
Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
33
---
4-
E25.py:46:15: E252 Missing whitespace around parameter equals
4+
E25.py:46:15: E252 [*] Missing whitespace around parameter equals
55
|
66
44 | return a + b
77
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
@@ -10,8 +10,19 @@ E25.py:46:15: E252 Missing whitespace around parameter equals
1010
47 | return a + b + c
1111
48 | #: Okay
1212
|
13+
= help: Add missing whitespace
1314

14-
E25.py:46:15: E252 Missing whitespace around parameter equals
15+
Fix
16+
43 43 | async def add(a: int = 0, b: int = 0) -> int:
17+
44 44 | return a + b
18+
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
19+
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
20+
46 |+def add(a: int =0, b: int =0, c: int= 0) -> int:
21+
47 47 | return a + b + c
22+
48 48 | #: Okay
23+
49 49 | def add(a: int = _default(name='f')):
24+
25+
E25.py:46:15: E252 [*] Missing whitespace around parameter equals
1526
|
1627
44 | return a + b
1728
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
@@ -20,8 +31,19 @@ E25.py:46:15: E252 Missing whitespace around parameter equals
2031
47 | return a + b + c
2132
48 | #: Okay
2233
|
34+
= help: Add missing whitespace
35+
36+
Fix
37+
43 43 | async def add(a: int = 0, b: int = 0) -> int:
38+
44 44 | return a + b
39+
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
40+
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
41+
46 |+def add(a: int= 0, b: int =0, c: int= 0) -> int:
42+
47 47 | return a + b + c
43+
48 48 | #: Okay
44+
49 49 | def add(a: int = _default(name='f')):
2345

24-
E25.py:46:26: E252 Missing whitespace around parameter equals
46+
E25.py:46:26: E252 [*] Missing whitespace around parameter equals
2547
|
2648
44 | return a + b
2749
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
@@ -30,8 +52,19 @@ E25.py:46:26: E252 Missing whitespace around parameter equals
3052
47 | return a + b + c
3153
48 | #: Okay
3254
|
55+
= help: Add missing whitespace
3356

34-
E25.py:46:36: E252 Missing whitespace around parameter equals
57+
Fix
58+
43 43 | async def add(a: int = 0, b: int = 0) -> int:
59+
44 44 | return a + b
60+
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
61+
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
62+
46 |+def add(a: int=0, b: int = 0, c: int= 0) -> int:
63+
47 47 | return a + b + c
64+
48 48 | #: Okay
65+
49 49 | def add(a: int = _default(name='f')):
66+
67+
E25.py:46:36: E252 [*] Missing whitespace around parameter equals
3568
|
3669
44 | return a + b
3770
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
@@ -40,5 +73,16 @@ E25.py:46:36: E252 Missing whitespace around parameter equals
4073
47 | return a + b + c
4174
48 | #: Okay
4275
|
76+
= help: Add missing whitespace
77+
78+
Fix
79+
43 43 | async def add(a: int = 0, b: int = 0) -> int:
80+
44 44 | return a + b
81+
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
82+
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
83+
46 |+def add(a: int=0, b: int =0, c: int = 0) -> int:
84+
47 47 | return a + b + c
85+
48 48 | #: Okay
86+
49 49 | def add(a: int = _default(name='f')):
4387

4488

0 commit comments

Comments
 (0)