Skip to content

Commit

Permalink
Remove continuations before trailing semicolons (#5199)
Browse files Browse the repository at this point in the history
## Summary

Closes #4828.
  • Loading branch information
charliermarsh authored Jun 20, 2023
1 parent 8e06140 commit 64bd955
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
3 changes: 3 additions & 0 deletions crates/ruff/resources/test/fixtures/pycodestyle/E70.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ class Foo:
#: E702:2:4
while 1:
1;...
#: E703:2:1
0\
;
2 changes: 1 addition & 1 deletion crates/ruff/src/checkers/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
);
Expand Down
16 changes: 13 additions & 3 deletions crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,7 +99,12 @@ impl AlwaysAutofixableViolation for UselessSemicolon {
}

/// E701, E702, E703
pub(crate) fn compound_statements(lxr: &[LexResult], settings: &Settings) -> Vec<Diagnostic> {
pub(crate) fn compound_statements(
lxr: &[LexResult],
indexer: &Indexer,
locator: &Locator,
settings: &Settings,
) -> Vec<Diagnostic> {
let mut diagnostics = vec![];

// Track the last seen instance of a variety of tokens.
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\
|


Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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


0 comments on commit 64bd955

Please sign in to comment.