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 35002a642e075..f5baea15080dc 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::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, TextSize}; @@ -69,11 +69,15 @@ impl Violation for UnexpectedSpacesAroundKeywordParameterEquals { #[violation] pub struct MissingWhitespaceAroundParameterEquals; -impl Violation for MissingWhitespaceAroundParameterEquals { +impl AlwaysFixableViolation for MissingWhitespaceAroundParameterEquals { #[derive_message_formats] fn message(&self) -> String { format!("Missing whitespace around parameter equals") } + + fn fix_title(&self) -> String { + format!("Add missing whitespace") + } } fn is_in_def(tokens: &[LogicalLineToken]) -> bool { @@ -131,7 +135,13 @@ pub(crate) fn whitespace_around_named_parameter_equals( if annotated_func_arg && parens == 1 { let start = token.start(); if start == prev_end && prev_end != TextSize::new(0) { - context.push(MissingWhitespaceAroundParameterEquals, token.range()); + let mut diagnostic = + Diagnostic::new(MissingWhitespaceAroundParameterEquals, token.range); + diagnostic.set_fix(Fix::safe_edit(Edit::insertion( + " ".to_string(), + token.start(), + ))); + context.push_diagnostic(diagnostic); } while let Some(next) = iter.peek() { @@ -141,7 +151,15 @@ pub(crate) fn whitespace_around_named_parameter_equals( let next_start = next.start(); if next_start == token.end() { - context.push(MissingWhitespaceAroundParameterEquals, token.range()); + let mut diagnostic = Diagnostic::new( + MissingWhitespaceAroundParameterEquals, + token.range, + ); + diagnostic.set_fix(Fix::safe_edit(Edit::insertion( + " ".to_string(), + token.end(), + ))); + context.push_diagnostic(diagnostic); } break; } diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E252_E25.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E252_E25.py.snap index 04d099aa11a09..5d27cb5025172 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E252_E25.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E252_E25.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E25.py:46:15: E252 Missing whitespace around parameter equals +E25.py:46:15: E252 [*] Missing whitespace around parameter equals | 44 | return a + b 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 47 | return a + b + c 48 | #: Okay | + = help: Add missing whitespace -E25.py:46:15: E252 Missing whitespace around parameter equals +ℹ Fix +43 43 | async def add(a: int = 0, b: int = 0) -> int: +44 44 | return a + b +45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36 +46 |-def add(a: int=0, b: int =0, c: int= 0) -> int: + 46 |+def add(a: int =0, b: int =0, c: int= 0) -> int: +47 47 | return a + b + c +48 48 | #: Okay +49 49 | def add(a: int = _default(name='f')): + +E25.py:46:15: E252 [*] Missing whitespace around parameter equals | 44 | return a + b 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 47 | return a + b + c 48 | #: Okay | + = help: Add missing whitespace + +ℹ Fix +43 43 | async def add(a: int = 0, b: int = 0) -> int: +44 44 | return a + b +45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36 +46 |-def add(a: int=0, b: int =0, c: int= 0) -> int: + 46 |+def add(a: int= 0, b: int =0, c: int= 0) -> int: +47 47 | return a + b + c +48 48 | #: Okay +49 49 | def add(a: int = _default(name='f')): -E25.py:46:26: E252 Missing whitespace around parameter equals +E25.py:46:26: E252 [*] Missing whitespace around parameter equals | 44 | return a + b 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 47 | return a + b + c 48 | #: Okay | + = help: Add missing whitespace -E25.py:46:36: E252 Missing whitespace around parameter equals +ℹ Fix +43 43 | async def add(a: int = 0, b: int = 0) -> int: +44 44 | return a + b +45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36 +46 |-def add(a: int=0, b: int =0, c: int= 0) -> int: + 46 |+def add(a: int=0, b: int = 0, c: int= 0) -> int: +47 47 | return a + b + c +48 48 | #: Okay +49 49 | def add(a: int = _default(name='f')): + +E25.py:46:36: E252 [*] Missing whitespace around parameter equals | 44 | return a + b 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 47 | return a + b + c 48 | #: Okay | + = help: Add missing whitespace + +ℹ Fix +43 43 | async def add(a: int = 0, b: int = 0) -> int: +44 44 | return a + b +45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36 +46 |-def add(a: int=0, b: int =0, c: int= 0) -> int: + 46 |+def add(a: int=0, b: int =0, c: int = 0) -> int: +47 47 | return a + b + c +48 48 | #: Okay +49 49 | def add(a: int = _default(name='f')):