diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/whitespace_around_named_parameter_equals.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/whitespace_around_named_parameter_equals.rs index f5baea1508..a0c4f49bf2 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/whitespace_around_named_parameter_equals.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/whitespace_around_named_parameter_equals.rs @@ -1,4 +1,4 @@ -use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, Violation}; +use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_parser::TokenKind; use ruff_text_size::{Ranged, TextRange, TextSize}; @@ -34,11 +34,15 @@ use crate::rules::pycodestyle::rules::logical_lines::{LogicalLine, LogicalLineTo #[violation] pub struct UnexpectedSpacesAroundKeywordParameterEquals; -impl Violation for UnexpectedSpacesAroundKeywordParameterEquals { +impl AlwaysFixableViolation for UnexpectedSpacesAroundKeywordParameterEquals { #[derive_message_formats] fn message(&self) -> String { format!("Unexpected spaces around keyword / parameter equals") } + + fn fix_title(&self) -> String { + format!("Remove whitespace") + } } /// ## What it does @@ -165,22 +169,31 @@ pub(crate) fn whitespace_around_named_parameter_equals( } } } else { + // If there's space between the preceding token and the equals sign, report it. if token.start() != prev_end { - context.push( + let mut diagnostic = Diagnostic::new( UnexpectedSpacesAroundKeywordParameterEquals, TextRange::new(prev_end, token.start()), ); + diagnostic.set_fix(Fix::safe_edit(Edit::deletion(prev_end, token.start()))); + context.push_diagnostic(diagnostic); } + // If there's space between the equals sign and the following token, report it. while let Some(next) = iter.peek() { if next.kind() == TokenKind::NonLogicalNewline { iter.next(); } else { if next.start() != token.end() { - context.push( + let mut diagnostic = Diagnostic::new( UnexpectedSpacesAroundKeywordParameterEquals, TextRange::new(token.end(), next.start()), ); + diagnostic.set_fix(Fix::safe_edit(Edit::deletion( + token.end(), + next.start(), + ))); + context.push_diagnostic(diagnostic); } break; } diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E251_E25.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E251_E25.py.snap index 88d211954b..a87bcdff55 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E251_E25.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E251_E25.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E25.py:2:12: E251 Unexpected spaces around keyword / parameter equals +E25.py:2:12: E251 [*] Unexpected spaces around keyword / parameter equals | 1 | #: E251 E251 2 | def foo(bar = False): @@ -9,8 +9,17 @@ E25.py:2:12: E251 Unexpected spaces around keyword / parameter equals 3 | '''Test function with an error in declaration''' 4 | pass | + = help: Remove whitespace -E25.py:2:14: E251 Unexpected spaces around keyword / parameter equals +ℹ Safe fix +1 1 | #: E251 E251 +2 |-def foo(bar = False): + 2 |+def foo(bar= False): +3 3 | '''Test function with an error in declaration''' +4 4 | pass +5 5 | #: E251 + +E25.py:2:14: E251 [*] Unexpected spaces around keyword / parameter equals | 1 | #: E251 E251 2 | def foo(bar = False): @@ -18,8 +27,17 @@ E25.py:2:14: E251 Unexpected spaces around keyword / parameter equals 3 | '''Test function with an error in declaration''' 4 | pass | + = help: Remove whitespace -E25.py:6:9: E251 Unexpected spaces around keyword / parameter equals +ℹ Safe fix +1 1 | #: E251 E251 +2 |-def foo(bar = False): + 2 |+def foo(bar =False): +3 3 | '''Test function with an error in declaration''' +4 4 | pass +5 5 | #: E251 + +E25.py:6:9: E251 [*] Unexpected spaces around keyword / parameter equals | 4 | pass 5 | #: E251 @@ -28,8 +46,19 @@ E25.py:6:9: E251 Unexpected spaces around keyword / parameter equals 7 | #: E251 8 | foo(bar =True) | + = help: Remove whitespace -E25.py:8:8: E251 Unexpected spaces around keyword / parameter equals +ℹ Safe fix +3 3 | '''Test function with an error in declaration''' +4 4 | pass +5 5 | #: E251 +6 |-foo(bar= True) + 6 |+foo(bar=True) +7 7 | #: E251 +8 8 | foo(bar =True) +9 9 | #: E251 E251 + +E25.py:8:8: E251 [*] Unexpected spaces around keyword / parameter equals | 6 | foo(bar= True) 7 | #: E251 @@ -38,8 +67,19 @@ E25.py:8:8: E251 Unexpected spaces around keyword / parameter equals 9 | #: E251 E251 10 | foo(bar = True) | + = help: Remove whitespace -E25.py:10:8: E251 Unexpected spaces around keyword / parameter equals +ℹ Safe fix +5 5 | #: E251 +6 6 | foo(bar= True) +7 7 | #: E251 +8 |-foo(bar =True) + 8 |+foo(bar=True) +9 9 | #: E251 E251 +10 10 | foo(bar = True) +11 11 | #: E251 + +E25.py:10:8: E251 [*] Unexpected spaces around keyword / parameter equals | 8 | foo(bar =True) 9 | #: E251 E251 @@ -48,8 +88,19 @@ E25.py:10:8: E251 Unexpected spaces around keyword / parameter equals 11 | #: E251 12 | y = bar(root= "sdasd") | + = help: Remove whitespace -E25.py:10:10: E251 Unexpected spaces around keyword / parameter equals +ℹ Safe fix +7 7 | #: E251 +8 8 | foo(bar =True) +9 9 | #: E251 E251 +10 |-foo(bar = True) + 10 |+foo(bar= True) +11 11 | #: E251 +12 12 | y = bar(root= "sdasd") +13 13 | #: E251:2:29 + +E25.py:10:10: E251 [*] Unexpected spaces around keyword / parameter equals | 8 | foo(bar =True) 9 | #: E251 E251 @@ -58,8 +109,19 @@ E25.py:10:10: E251 Unexpected spaces around keyword / parameter equals 11 | #: E251 12 | y = bar(root= "sdasd") | + = help: Remove whitespace -E25.py:12:14: E251 Unexpected spaces around keyword / parameter equals +ℹ Safe fix +7 7 | #: E251 +8 8 | foo(bar =True) +9 9 | #: E251 E251 +10 |-foo(bar = True) + 10 |+foo(bar =True) +11 11 | #: E251 +12 12 | y = bar(root= "sdasd") +13 13 | #: E251:2:29 + +E25.py:12:14: E251 [*] Unexpected spaces around keyword / parameter equals | 10 | foo(bar = True) 11 | #: E251 @@ -68,8 +130,19 @@ E25.py:12:14: E251 Unexpected spaces around keyword / parameter equals 13 | #: E251:2:29 14 | parser.add_argument('--long-option', | + = help: Remove whitespace -E25.py:15:29: E251 Unexpected spaces around keyword / parameter equals +ℹ Safe fix +9 9 | #: E251 E251 +10 10 | foo(bar = True) +11 11 | #: E251 +12 |-y = bar(root= "sdasd") + 12 |+y = bar(root="sdasd") +13 13 | #: E251:2:29 +14 14 | parser.add_argument('--long-option', +15 15 | default= + +E25.py:15:29: E251 [*] Unexpected spaces around keyword / parameter equals | 13 | #: E251:2:29 14 | parser.add_argument('--long-option', @@ -80,8 +153,20 @@ E25.py:15:29: E251 Unexpected spaces around keyword / parameter equals 17 | #: E251:1:45 18 | parser.add_argument('--long-option', default | + = help: Remove whitespace -E25.py:18:45: E251 Unexpected spaces around keyword / parameter equals +ℹ Safe fix +12 12 | y = bar(root= "sdasd") +13 13 | #: E251:2:29 +14 14 | parser.add_argument('--long-option', +15 |- default= +16 |- "/rather/long/filesystem/path/here/blah/blah/blah") + 15 |+ default="/rather/long/filesystem/path/here/blah/blah/blah") +17 16 | #: E251:1:45 +18 17 | parser.add_argument('--long-option', default +19 18 | ="/rather/long/filesystem/path/here/blah/blah/blah") + +E25.py:18:45: E251 [*] Unexpected spaces around keyword / parameter equals | 16 | "/rather/long/filesystem/path/here/blah/blah/blah") 17 | #: E251:1:45 @@ -92,8 +177,20 @@ E25.py:18:45: E251 Unexpected spaces around keyword / parameter equals 20 | #: E251:3:8 E251:3:10 21 | foo(True, | + = help: Remove whitespace -E25.py:23:8: E251 Unexpected spaces around keyword / parameter equals +ℹ Safe fix +15 15 | default= +16 16 | "/rather/long/filesystem/path/here/blah/blah/blah") +17 17 | #: E251:1:45 +18 |-parser.add_argument('--long-option', default +19 |- ="/rather/long/filesystem/path/here/blah/blah/blah") + 18 |+parser.add_argument('--long-option', default="/rather/long/filesystem/path/here/blah/blah/blah") +20 19 | #: E251:3:8 E251:3:10 +21 20 | foo(True, +22 21 | baz=(1, 2), + +E25.py:23:8: E251 [*] Unexpected spaces around keyword / parameter equals | 21 | foo(True, 22 | baz=(1, 2), @@ -102,8 +199,19 @@ E25.py:23:8: E251 Unexpected spaces around keyword / parameter equals 24 | ) 25 | #: Okay | + = help: Remove whitespace -E25.py:23:10: E251 Unexpected spaces around keyword / parameter equals +ℹ Safe fix +20 20 | #: E251:3:8 E251:3:10 +21 21 | foo(True, +22 22 | baz=(1, 2), +23 |- biz = 'foo' + 23 |+ biz= 'foo' +24 24 | ) +25 25 | #: Okay +26 26 | foo(bar=(1 == 1)) + +E25.py:23:10: E251 [*] Unexpected spaces around keyword / parameter equals | 21 | foo(True, 22 | baz=(1, 2), @@ -112,5 +220,16 @@ E25.py:23:10: E251 Unexpected spaces around keyword / parameter equals 24 | ) 25 | #: Okay | + = help: Remove whitespace + +ℹ Safe fix +20 20 | #: E251:3:8 E251:3:10 +21 21 | foo(True, +22 22 | baz=(1, 2), +23 |- biz = 'foo' + 23 |+ biz ='foo' +24 24 | ) +25 25 | #: Okay +26 26 | foo(bar=(1 == 1))