From c174ac3a88a08bd5ea6e3f7aa9f32826f436510b Mon Sep 17 00:00:00 2001 From: Weijie Guo Date: Mon, 23 Oct 2023 20:19:25 +0800 Subject: [PATCH 1/2] add auto-fix for E252 --- ...hitespace_around_named_parameter_equals.rs | 26 ++++++++-- ...ules__pycodestyle__tests__E252_E25.py.snap | 52 +++++++++++++++++-- 2 files changed, 70 insertions(+), 8 deletions(-) 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..736f5a530e829 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!("Added missing whitespace around parameter equals") + } } 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..a505ca0d3393b 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: Added missing whitespace around parameter equals -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: Added 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: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: Added missing whitespace around parameter equals -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: Added 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')): From c0e29d38b391b493baefcb6e5a2401d83a9df772 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 23 Oct 2023 14:47:35 -0400 Subject: [PATCH 2/2] Tweak message --- .../whitespace_around_named_parameter_equals.rs | 2 +- ...ff_linter__rules__pycodestyle__tests__E252_E25.py.snap | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) 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 736f5a530e829..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 @@ -76,7 +76,7 @@ impl AlwaysFixableViolation for MissingWhitespaceAroundParameterEquals { } fn fix_title(&self) -> String { - format!("Added missing whitespace around parameter equals") + format!("Add missing whitespace") } } 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 a505ca0d3393b..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 @@ -10,7 +10,7 @@ E25.py:46:15: E252 [*] Missing whitespace around parameter equals 47 | return a + b + c 48 | #: Okay | - = help: Added missing whitespace around parameter equals + = help: Add missing whitespace ℹ Fix 43 43 | async def add(a: int = 0, b: int = 0) -> int: @@ -31,7 +31,7 @@ E25.py:46:15: E252 [*] Missing whitespace around parameter equals 47 | return a + b + c 48 | #: Okay | - = help: Added missing whitespace around parameter equals + = help: Add missing whitespace ℹ Fix 43 43 | async def add(a: int = 0, b: int = 0) -> int: @@ -52,7 +52,7 @@ E25.py:46:26: E252 [*] Missing whitespace around parameter equals 47 | return a + b + c 48 | #: Okay | - = help: Added missing whitespace around parameter equals + = help: Add missing whitespace ℹ Fix 43 43 | async def add(a: int = 0, b: int = 0) -> int: @@ -73,7 +73,7 @@ E25.py:46:36: E252 [*] Missing whitespace around parameter equals 47 | return a + b + c 48 | #: Okay | - = help: Added missing whitespace around parameter equals + = help: Add missing whitespace ℹ Fix 43 43 | async def add(a: int = 0, b: int = 0) -> int: