diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs index 6e58faf9c1b88..314693446aa86 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs @@ -47,8 +47,7 @@ impl AlwaysFixableViolation for MissingWhitespace { } fn fix_title(&self) -> String { - let token = self.token_text(); - format!("Added missing whitespace after '{token}'") + format!("Add missing whitespace") } } diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs index 0dc26221e8b52..7aa8cfc24764a 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/missing_whitespace_after_keyword.rs @@ -1,4 +1,4 @@ -use ruff_diagnostics::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; @@ -29,11 +29,15 @@ use crate::rules::pycodestyle::rules::logical_lines::LogicalLine; #[violation] pub struct MissingWhitespaceAfterKeyword; -impl Violation for MissingWhitespaceAfterKeyword { +impl AlwaysFixableViolation for MissingWhitespaceAfterKeyword { #[derive_message_formats] fn message(&self) -> String { format!("Missing whitespace after keyword") } + + fn fix_title(&self) -> String { + format!("Added missing whitespace after keyword") + } } /// E275 @@ -59,7 +63,9 @@ pub(crate) fn missing_whitespace_after_keyword( )) && tok0.end() == tok1.start() { - context.push(MissingWhitespaceAfterKeyword, tok0.range()); + let mut diagnostic = Diagnostic::new(MissingWhitespaceAfterKeyword, tok0.range()); + diagnostic.set_fix(Fix::safe_edit(Edit::insertion(" ".to_string(), tok0.end()))); + context.push_diagnostic(diagnostic); } } } diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E231_E23.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E231_E23.py.snap index 357649e567888..d0839a77a266f 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E231_E23.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E231_E23.py.snap @@ -9,7 +9,7 @@ E23.py:2:7: E231 [*] Missing whitespace after ',' 3 | #: E231 4 | a[b1,:] | - = help: Added missing whitespace after ',' + = help: Add missing whitespace ℹ Fix 1 1 | #: E231 @@ -28,7 +28,7 @@ E23.py:4:5: E231 [*] Missing whitespace after ',' 5 | #: E231 6 | a = [{'a':''}] | - = help: Added missing whitespace after ',' + = help: Add missing whitespace ℹ Fix 1 1 | #: E231 @@ -49,7 +49,7 @@ E23.py:6:10: E231 [*] Missing whitespace after ':' 7 | #: Okay 8 | a = (4,) | - = help: Added missing whitespace after ':' + = help: Add missing whitespace ℹ Fix 3 3 | #: E231 @@ -69,7 +69,7 @@ E23.py:19:10: E231 [*] Missing whitespace after ',' | ^ E231 20 | pass | - = help: Added missing whitespace after ',' + = help: Add missing whitespace ℹ Fix 16 16 | @@ -89,7 +89,7 @@ E23.py:29:20: E231 [*] Missing whitespace after ':' | ^ E231 30 | } | - = help: Added missing whitespace after ':' + = help: Add missing whitespace ℹ Fix 26 26 | #: E231:2:20 @@ -109,7 +109,7 @@ E23.py:33:6: E231 [*] Missing whitespace after ',' 34 | 35 | # Okay because it's hard to differentiate between the usages of a colon in a f-string | - = help: Added missing whitespace after ',' + = help: Add missing whitespace ℹ Fix 30 30 | } diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E275_E27.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E275_E27.py.snap index 3c73cb2654d3d..8e4502513a9a3 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E275_E27.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E275_E27.py.snap @@ -1,7 +1,7 @@ --- source: crates/ruff_linter/src/rules/pycodestyle/mod.rs --- -E27.py:37:8: E275 Missing whitespace after keyword +E27.py:37:8: E275 [*] Missing whitespace after keyword | 35 | from w import (e, f) 36 | #: E275 @@ -10,8 +10,19 @@ E27.py:37:8: E275 Missing whitespace after keyword 38 | #: E275 39 | from importable.module import(e, f) | + = help: Added missing whitespace after keyword -E27.py:39:24: E275 Missing whitespace after keyword +ℹ Fix +34 34 | #: E271 +35 35 | from w import (e, f) +36 36 | #: E275 +37 |-from w import(e, f) + 37 |+from w import (e, f) +38 38 | #: E275 +39 39 | from importable.module import(e, f) +40 40 | #: E275 + +E27.py:39:24: E275 [*] Missing whitespace after keyword | 37 | from w import(e, f) 38 | #: E275 @@ -20,8 +31,19 @@ E27.py:39:24: E275 Missing whitespace after keyword 40 | #: E275 41 | try: | + = help: Added missing whitespace after keyword + +ℹ Fix +36 36 | #: E275 +37 37 | from w import(e, f) +38 38 | #: E275 +39 |-from importable.module import(e, f) + 39 |+from importable.module import (e, f) +40 40 | #: E275 +41 41 | try: +42 42 | from importable.module import(e, f) -E27.py:42:28: E275 Missing whitespace after keyword +E27.py:42:28: E275 [*] Missing whitespace after keyword | 40 | #: E275 41 | try: @@ -30,8 +52,19 @@ E27.py:42:28: E275 Missing whitespace after keyword 43 | except ImportError: 44 | pass | + = help: Added missing whitespace after keyword + +ℹ Fix +39 39 | from importable.module import(e, f) +40 40 | #: E275 +41 41 | try: +42 |- from importable.module import(e, f) + 42 |+ from importable.module import (e, f) +43 43 | except ImportError: +44 44 | pass +45 45 | #: E275 -E27.py:46:1: E275 Missing whitespace after keyword +E27.py:46:1: E275 [*] Missing whitespace after keyword | 44 | pass 45 | #: E275 @@ -40,8 +73,19 @@ E27.py:46:1: E275 Missing whitespace after keyword 47 | pass 48 | else: | + = help: Added missing whitespace after keyword -E27.py:54:5: E275 Missing whitespace after keyword +ℹ Fix +43 43 | except ImportError: +44 44 | pass +45 45 | #: E275 +46 |-if(foo): + 46 |+if (foo): +47 47 | pass +48 48 | else: +49 49 | pass + +E27.py:54:5: E275 [*] Missing whitespace after keyword | 52 | #: E275:2:11 53 | if True: @@ -50,5 +94,16 @@ E27.py:54:5: E275 Missing whitespace after keyword 55 | #: Okay 56 | def f(): | + = help: Added missing whitespace after keyword + +ℹ Fix +51 51 | matched = {"true": True, "false": False} +52 52 | #: E275:2:11 +53 53 | if True: +54 |- assert(1) + 54 |+ assert (1) +55 55 | #: Okay +56 56 | def f(): +57 57 | print((yield))