diff --git a/crates/ruff/resources/test/fixtures/pycodestyle/E70.py b/crates/ruff/resources/test/fixtures/pycodestyle/E70.py index 2fa6fa4813..9b9fde824c 100644 --- a/crates/ruff/resources/test/fixtures/pycodestyle/E70.py +++ b/crates/ruff/resources/test/fixtures/pycodestyle/E70.py @@ -63,3 +63,6 @@ class Foo: #: E702:2:4 while 1: 1;... +#: E703:2:1 +0\ +; diff --git a/crates/ruff/src/checkers/tokens.rs b/crates/ruff/src/checkers/tokens.rs index 4f71610a7c..07bc525a4a 100644 --- a/crates/ruff/src/checkers/tokens.rs +++ b/crates/ruff/src/checkers/tokens.rs @@ -141,7 +141,7 @@ pub(crate) fn check_tokens( // E701, E702, E703 if enforce_compound_statements { diagnostics.extend( - pycodestyle::rules::compound_statements(tokens, settings) + pycodestyle::rules::compound_statements(tokens, indexer, locator, settings) .into_iter() .filter(|diagnostic| settings.rules.enabled(diagnostic.kind.rule())), ); diff --git a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs index fd9b57b6e9..b484d9cd09 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs @@ -5,6 +5,8 @@ use rustpython_parser::Tok; use ruff_diagnostics::{AlwaysAutofixableViolation, Violation}; use ruff_diagnostics::{Diagnostic, Edit, Fix}; use ruff_macros::{derive_message_formats, violation}; +use ruff_python_ast::helpers; +use ruff_python_ast::source_code::{Indexer, Locator}; use crate::registry::Rule; use crate::settings::Settings; @@ -97,7 +99,12 @@ impl AlwaysAutofixableViolation for UselessSemicolon { } /// E701, E702, E703 -pub(crate) fn compound_statements(lxr: &[LexResult], settings: &Settings) -> Vec { +pub(crate) fn compound_statements( + lxr: &[LexResult], + indexer: &Indexer, + locator: &Locator, + settings: &Settings, +) -> Vec { let mut diagnostics = vec![]; // Track the last seen instance of a variety of tokens. @@ -164,8 +171,11 @@ pub(crate) fn compound_statements(lxr: &[LexResult], settings: &Settings) -> Vec let mut diagnostic = Diagnostic::new(UselessSemicolon, TextRange::new(start, end)); if settings.rules.should_fix(Rule::UselessSemicolon) { - #[allow(deprecated)] - diagnostic.set_fix(Fix::unspecified(Edit::deletion(start, end))); + diagnostic.set_fix(Fix::automatic(Edit::deletion( + helpers::preceded_by_continuations(start, locator, indexer) + .unwrap_or(start), + end, + ))); }; diagnostics.push(diagnostic); } diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E702_E70.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E702_E70.py.snap index 20d62cd44b..a0e09543c1 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E702_E70.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E702_E70.py.snap @@ -67,6 +67,8 @@ E70.py:65:4: E702 Multiple statements on one line (semicolon) 64 | while 1: 65 | 1;... | ^ E702 +66 | #: E703:2:1 +67 | 0\ | diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E703_E70.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E703_E70.py.snap index 6e5b240773..a612503351 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E703_E70.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E703_E70.py.snap @@ -12,7 +12,7 @@ E70.py:10:13: E703 [*] Statement ends with an unnecessary semicolon | = help: Remove unnecessary semicolon -ℹ Suggested fix +ℹ Fix 7 7 | #: E702:1:17 8 8 | import bdist_egg; bdist_egg.write_safety_flag(cmd.egg_info, safe) 9 9 | #: E703:1:13 @@ -33,7 +33,7 @@ E70.py:12:23: E703 [*] Statement ends with an unnecessary semicolon | = help: Remove unnecessary semicolon -ℹ Suggested fix +ℹ Fix 9 9 | #: E703:1:13 10 10 | import shlex; 11 11 | #: E702:1:9 E703:1:23 @@ -54,7 +54,7 @@ E70.py:25:14: E703 [*] Statement ends with an unnecessary semicolon | = help: Remove unnecessary semicolon -ℹ Suggested fix +ℹ Fix 22 22 | while all is round: 23 23 | def f(x): return 2*x 24 24 | #: E704:1:8 E702:1:11 E703:1:14 @@ -64,4 +64,21 @@ E70.py:25:14: E703 [*] Statement ends with an unnecessary semicolon 27 27 | if True: lambda a: b 28 28 | #: E701:1:10 +E70.py:68:1: E703 [*] Statement ends with an unnecessary semicolon + | +66 | #: E703:2:1 +67 | 0\ +68 | ; + | ^ E703 + | + = help: Remove unnecessary semicolon + +ℹ Fix +64 64 | while 1: +65 65 | 1;... +66 66 | #: E703:2:1 +67 |-0\ +68 |-; + 67 |+0 +