diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/space_around_operator.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/space_around_operator.rs index b150173b854f76..81bf247421bfb1 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/space_around_operator.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/space_around_operator.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}; @@ -60,11 +60,15 @@ impl AlwaysFixableViolation for TabBeforeOperator { #[violation] pub struct MultipleSpacesBeforeOperator; -impl Violation for MultipleSpacesBeforeOperator { +impl AlwaysFixableViolation for MultipleSpacesBeforeOperator { #[derive_message_formats] fn message(&self) -> String { format!("Multiple spaces before operator") } + + fn fix_title(&self) -> String { + format!("Replace with single space") + } } /// ## What it does @@ -120,11 +124,15 @@ impl AlwaysFixableViolation for TabAfterOperator { #[violation] pub struct MultipleSpacesAfterOperator; -impl Violation for MultipleSpacesAfterOperator { +impl AlwaysFixableViolation for MultipleSpacesAfterOperator { #[derive_message_formats] fn message(&self) -> String { format!("Multiple spaces after operator") } + + fn fix_title(&self) -> String { + format!("Replace with single space") + } } /// ## What it does @@ -175,11 +183,15 @@ impl AlwaysFixableViolation for TabAfterComma { #[violation] pub struct MultipleSpacesAfterComma; -impl Violation for MultipleSpacesAfterComma { +impl AlwaysFixableViolation for MultipleSpacesAfterComma { #[derive_message_formats] fn message(&self) -> String { format!("Multiple spaces after comma") } + + fn fix_title(&self) -> String { + format!("Replace with single space") + } } /// E221, E222, E223, E224 @@ -204,10 +216,15 @@ pub(crate) fn space_around_operator(line: &LogicalLine, context: &mut LogicalLin context.push_diagnostic(diagnostic); } (Whitespace::Many, offset) => { - context.push( + let mut diagnostic = Diagnostic::new( MultipleSpacesBeforeOperator, TextRange::at(token.start() - offset, offset), ); + diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( + " ".to_string(), + TextRange::at(token.start() - offset, offset), + ))); + context.push_diagnostic(diagnostic); } _ => {} } @@ -224,7 +241,15 @@ pub(crate) fn space_around_operator(line: &LogicalLine, context: &mut LogicalLin context.push_diagnostic(diagnostic); } (Whitespace::Many, len) => { - context.push(MultipleSpacesAfterOperator, TextRange::at(token.end(), len)); + let mut diagnostic = Diagnostic::new( + MultipleSpacesAfterOperator, + TextRange::at(token.end(), len), + ); + diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement( + " ".to_string(), + TextRange::at(token.end(), len), + ))); + context.push_diagnostic(diagnostic); } _ => {} } diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E221_E22.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E221_E22.py.snap index d995e58ac9e60b..ec75229e10a6bd 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E221_E22.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E221_E22.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E22.py:3:6: E221 Multiple spaces before operator +E22.py:3:6: E221 [*] Multiple spaces before operator | 1 | #: E221 2 | a = 12 + 3 @@ -10,8 +10,18 @@ E22.py:3:6: E221 Multiple spaces before operator 4 | #: E221 E221 5 | x = 1 | + = help: Replace with single space -E22.py:5:2: E221 Multiple spaces before operator +ℹ Safe fix +1 1 | #: E221 +2 2 | a = 12 + 3 +3 |-b = 4 + 5 + 3 |+b = 4 + 5 +4 4 | #: E221 E221 +5 5 | x = 1 +6 6 | y = 2 + +E22.py:5:2: E221 [*] Multiple spaces before operator | 3 | b = 4 + 5 4 | #: E221 E221 @@ -20,8 +30,19 @@ E22.py:5:2: E221 Multiple spaces before operator 6 | y = 2 7 | long_variable = 3 | + = help: Replace with single space + +ℹ Safe fix +2 2 | a = 12 + 3 +3 3 | b = 4 + 5 +4 4 | #: E221 E221 +5 |-x = 1 + 5 |+x = 1 +6 6 | y = 2 +7 7 | long_variable = 3 +8 8 | #: E221 E221 -E22.py:6:2: E221 Multiple spaces before operator +E22.py:6:2: E221 [*] Multiple spaces before operator | 4 | #: E221 E221 5 | x = 1 @@ -30,8 +51,19 @@ E22.py:6:2: E221 Multiple spaces before operator 7 | long_variable = 3 8 | #: E221 E221 | + = help: Replace with single space -E22.py:9:5: E221 Multiple spaces before operator +ℹ Safe fix +3 3 | b = 4 + 5 +4 4 | #: E221 E221 +5 5 | x = 1 +6 |-y = 2 + 6 |+y = 2 +7 7 | long_variable = 3 +8 8 | #: E221 E221 +9 9 | x[0] = 1 + +E22.py:9:5: E221 [*] Multiple spaces before operator | 7 | long_variable = 3 8 | #: E221 E221 @@ -40,8 +72,19 @@ E22.py:9:5: E221 Multiple spaces before operator 10 | x[1] = 2 11 | long_variable = 3 | + = help: Replace with single space + +ℹ Safe fix +6 6 | y = 2 +7 7 | long_variable = 3 +8 8 | #: E221 E221 +9 |-x[0] = 1 + 9 |+x[0] = 1 +10 10 | x[1] = 2 +11 11 | long_variable = 3 +12 12 | #: E221 E221 -E22.py:10:5: E221 Multiple spaces before operator +E22.py:10:5: E221 [*] Multiple spaces before operator | 8 | #: E221 E221 9 | x[0] = 1 @@ -50,8 +93,19 @@ E22.py:10:5: E221 Multiple spaces before operator 11 | long_variable = 3 12 | #: E221 E221 | + = help: Replace with single space -E22.py:13:9: E221 Multiple spaces before operator +ℹ Safe fix +7 7 | long_variable = 3 +8 8 | #: E221 E221 +9 9 | x[0] = 1 +10 |-x[1] = 2 + 10 |+x[1] = 2 +11 11 | long_variable = 3 +12 12 | #: E221 E221 +13 13 | x = f(x) + 1 + +E22.py:13:9: E221 [*] Multiple spaces before operator | 11 | long_variable = 3 12 | #: E221 E221 @@ -60,8 +114,19 @@ E22.py:13:9: E221 Multiple spaces before operator 14 | y = long_variable + 2 15 | z = x[0] + 3 | + = help: Replace with single space + +ℹ Safe fix +10 10 | x[1] = 2 +11 11 | long_variable = 3 +12 12 | #: E221 E221 +13 |-x = f(x) + 1 + 13 |+x = f(x) + 1 +14 14 | y = long_variable + 2 +15 15 | z = x[0] + 3 +16 16 | #: E221:3:14 -E22.py:15:9: E221 Multiple spaces before operator +E22.py:15:9: E221 [*] Multiple spaces before operator | 13 | x = f(x) + 1 14 | y = long_variable + 2 @@ -70,8 +135,19 @@ E22.py:15:9: E221 Multiple spaces before operator 16 | #: E221:3:14 17 | text = """ | + = help: Replace with single space -E22.py:19:14: E221 Multiple spaces before operator +ℹ Safe fix +12 12 | #: E221 E221 +13 13 | x = f(x) + 1 +14 14 | y = long_variable + 2 +15 |-z = x[0] + 3 + 15 |+z = x[0] + 3 +16 16 | #: E221:3:14 +17 17 | text = """ +18 18 | bar + +E22.py:19:14: E221 [*] Multiple spaces before operator | 17 | text = """ 18 | bar @@ -80,5 +156,16 @@ E22.py:19:14: E221 Multiple spaces before operator 20 | #: Okay 21 | x = 1 | + = help: Replace with single space + +ℹ Safe fix +16 16 | #: E221:3:14 +17 17 | text = """ +18 18 | bar +19 |- foo %s""" % rofl + 19 |+ foo %s""" % rofl +20 20 | #: Okay +21 21 | x = 1 +22 22 | y = 2 diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E222_E22.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E222_E22.py.snap index ca4dac6d17a660..52ec6b7ed68315 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E222_E22.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E222_E22.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E22.py:28:8: E222 Multiple spaces after operator +E22.py:28:8: E222 [*] Multiple spaces after operator | 27 | #: E222 28 | a = a + 1 @@ -9,8 +9,19 @@ E22.py:28:8: E222 Multiple spaces after operator 29 | b = b + 10 30 | #: E222 E222 | + = help: Replace with single space -E22.py:31:4: E222 Multiple spaces after operator +ℹ Safe fix +25 25 | +26 26 | +27 27 | #: E222 +28 |-a = a + 1 + 28 |+a = a + 1 +29 29 | b = b + 10 +30 30 | #: E222 E222 +31 31 | x = -1 + +E22.py:31:4: E222 [*] Multiple spaces after operator | 29 | b = b + 10 30 | #: E222 E222 @@ -19,8 +30,19 @@ E22.py:31:4: E222 Multiple spaces after operator 32 | y = -2 33 | long_variable = 3 | + = help: Replace with single space + +ℹ Safe fix +28 28 | a = a + 1 +29 29 | b = b + 10 +30 30 | #: E222 E222 +31 |-x = -1 + 31 |+x = -1 +32 32 | y = -2 +33 33 | long_variable = 3 +34 34 | #: E222 E222 -E22.py:32:4: E222 Multiple spaces after operator +E22.py:32:4: E222 [*] Multiple spaces after operator | 30 | #: E222 E222 31 | x = -1 @@ -29,8 +51,19 @@ E22.py:32:4: E222 Multiple spaces after operator 33 | long_variable = 3 34 | #: E222 E222 | + = help: Replace with single space + +ℹ Safe fix +29 29 | b = b + 10 +30 30 | #: E222 E222 +31 31 | x = -1 +32 |-y = -2 + 32 |+y = -2 +33 33 | long_variable = 3 +34 34 | #: E222 E222 +35 35 | x[0] = 1 -E22.py:35:7: E222 Multiple spaces after operator +E22.py:35:7: E222 [*] Multiple spaces after operator | 33 | long_variable = 3 34 | #: E222 E222 @@ -39,8 +72,19 @@ E22.py:35:7: E222 Multiple spaces after operator 36 | x[1] = 2 37 | long_variable = 3 | + = help: Replace with single space -E22.py:36:7: E222 Multiple spaces after operator +ℹ Safe fix +32 32 | y = -2 +33 33 | long_variable = 3 +34 34 | #: E222 E222 +35 |-x[0] = 1 + 35 |+x[0] = 1 +36 36 | x[1] = 2 +37 37 | long_variable = 3 +38 38 | #: + +E22.py:36:7: E222 [*] Multiple spaces after operator | 34 | #: E222 E222 35 | x[0] = 1 @@ -49,5 +93,16 @@ E22.py:36:7: E222 Multiple spaces after operator 37 | long_variable = 3 38 | #: | + = help: Replace with single space + +ℹ Safe fix +33 33 | long_variable = 3 +34 34 | #: E222 E222 +35 35 | x[0] = 1 +36 |-x[1] = 2 + 36 |+x[1] = 2 +37 37 | long_variable = 3 +38 38 | #: +39 39 |