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 72e4d0c53caa5a..476d325db04dcc 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::Violation; +use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, Violation}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_parser::TokenKind; use ruff_text_size::{Ranged, TextRange}; @@ -28,11 +28,15 @@ use super::{LogicalLine, Whitespace}; #[violation] pub struct TabBeforeOperator; -impl Violation for TabBeforeOperator { +impl AlwaysFixableViolation for TabBeforeOperator { #[derive_message_formats] fn message(&self) -> String { format!("Tab before operator") } + + fn fix_title(&self) -> String { + format!("Replaced tab before operator by whitespace") + } } /// ## What it does @@ -84,11 +88,15 @@ impl Violation for MultipleSpacesBeforeOperator { #[violation] pub struct TabAfterOperator; -impl Violation for TabAfterOperator { +impl AlwaysFixableViolation for TabAfterOperator { #[derive_message_formats] fn message(&self) -> String { format!("Tab after operator") } + + fn fix_title(&self) -> String { + format!("Replaced tab after operator by whitespace") + } } /// ## What it does @@ -181,10 +189,15 @@ pub(crate) fn space_around_operator(line: &LogicalLine, context: &mut LogicalLin if !after_operator { match line.leading_whitespace(token) { (Whitespace::Tab, offset) => { - context.push( + let mut diagnostic = Diagnostic::new( TabBeforeOperator, 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); } (Whitespace::Many, offset) => { context.push( @@ -198,7 +211,13 @@ pub(crate) fn space_around_operator(line: &LogicalLine, context: &mut LogicalLin match line.trailing_whitespace(token) { (Whitespace::Tab, len) => { - context.push(TabAfterOperator, TextRange::at(token.end(), len)); + let mut diagnostic = + Diagnostic::new(TabAfterOperator, 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); } (Whitespace::Many, len) => { context.push(MultipleSpacesAfterOperator, TextRange::at(token.end(), len)); diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E223_E22.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E223_E22.py.snap index 58f50d1a46000b..441a5437748985 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E223_E22.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E223_E22.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E22.py:43:2: E223 Tab before operator +E22.py:43:2: E223 [*] Tab before operator | 41 | #: E223 42 | foobart = 4 @@ -9,5 +9,16 @@ E22.py:43:2: E223 Tab before operator | ^^^ E223 44 | #: | + = help: Replaced tab before operator by whitespace + +ℹ Fix +40 40 | +41 41 | #: E223 +42 42 | foobart = 4 +43 |-a = 3 # aligned with tab + 43 |+a = 3 # aligned with tab +44 44 | #: +45 45 | +46 46 | diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E224_E22.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E224_E22.py.snap index 59b473485c80a3..498dd086f44ad4 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E224_E22.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E224_E22.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E22.py:48:5: E224 Tab after operator +E22.py:48:5: E224 [*] Tab after operator | 47 | #: E224 48 | a += 1 @@ -9,5 +9,16 @@ E22.py:48:5: E224 Tab after operator 49 | b += 1000 50 | #: | + = help: Replaced tab after operator by whitespace + +ℹ Fix +45 45 | +46 46 | +47 47 | #: E224 +48 |-a += 1 + 48 |+a += 1 +49 49 | b += 1000 +50 50 | #: +51 51 |