mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 20:39:13 +00:00
Remove continuations before trailing semicolons (#5199)
## Summary Closes #4828.
This commit is contained in:
parent
8e06140d1d
commit
64bd955c58
5 changed files with 39 additions and 7 deletions
|
@ -63,3 +63,6 @@ class Foo:
|
||||||
#: E702:2:4
|
#: E702:2:4
|
||||||
while 1:
|
while 1:
|
||||||
1;...
|
1;...
|
||||||
|
#: E703:2:1
|
||||||
|
0\
|
||||||
|
;
|
||||||
|
|
|
@ -141,7 +141,7 @@ pub(crate) fn check_tokens(
|
||||||
// E701, E702, E703
|
// E701, E702, E703
|
||||||
if enforce_compound_statements {
|
if enforce_compound_statements {
|
||||||
diagnostics.extend(
|
diagnostics.extend(
|
||||||
pycodestyle::rules::compound_statements(tokens, settings)
|
pycodestyle::rules::compound_statements(tokens, indexer, locator, settings)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|diagnostic| settings.rules.enabled(diagnostic.kind.rule())),
|
.filter(|diagnostic| settings.rules.enabled(diagnostic.kind.rule())),
|
||||||
);
|
);
|
||||||
|
|
|
@ -5,6 +5,8 @@ use rustpython_parser::Tok;
|
||||||
use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
|
use ruff_diagnostics::{AlwaysAutofixableViolation, Violation};
|
||||||
use ruff_diagnostics::{Diagnostic, Edit, Fix};
|
use ruff_diagnostics::{Diagnostic, Edit, Fix};
|
||||||
use ruff_macros::{derive_message_formats, violation};
|
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::registry::Rule;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
@ -97,7 +99,12 @@ impl AlwaysAutofixableViolation for UselessSemicolon {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// E701, E702, E703
|
/// 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![];
|
let mut diagnostics = vec![];
|
||||||
|
|
||||||
// Track the last seen instance of a variety of tokens.
|
// 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 =
|
let mut diagnostic =
|
||||||
Diagnostic::new(UselessSemicolon, TextRange::new(start, end));
|
Diagnostic::new(UselessSemicolon, TextRange::new(start, end));
|
||||||
if settings.rules.should_fix(Rule::UselessSemicolon) {
|
if settings.rules.should_fix(Rule::UselessSemicolon) {
|
||||||
#[allow(deprecated)]
|
diagnostic.set_fix(Fix::automatic(Edit::deletion(
|
||||||
diagnostic.set_fix(Fix::unspecified(Edit::deletion(start, end)));
|
helpers::preceded_by_continuations(start, locator, indexer)
|
||||||
|
.unwrap_or(start),
|
||||||
|
end,
|
||||||
|
)));
|
||||||
};
|
};
|
||||||
diagnostics.push(diagnostic);
|
diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,6 +67,8 @@ E70.py:65:4: E702 Multiple statements on one line (semicolon)
|
||||||
64 | while 1:
|
64 | while 1:
|
||||||
65 | 1;...
|
65 | 1;...
|
||||||
| ^ E702
|
| ^ E702
|
||||||
|
66 | #: E703:2:1
|
||||||
|
67 | 0\
|
||||||
|
|
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ E70.py:10:13: E703 [*] Statement ends with an unnecessary semicolon
|
||||||
|
|
|
|
||||||
= help: Remove unnecessary semicolon
|
= help: Remove unnecessary semicolon
|
||||||
|
|
||||||
ℹ Suggested fix
|
ℹ Fix
|
||||||
7 7 | #: E702:1:17
|
7 7 | #: E702:1:17
|
||||||
8 8 | import bdist_egg; bdist_egg.write_safety_flag(cmd.egg_info, safe)
|
8 8 | import bdist_egg; bdist_egg.write_safety_flag(cmd.egg_info, safe)
|
||||||
9 9 | #: E703:1:13
|
9 9 | #: E703:1:13
|
||||||
|
@ -33,7 +33,7 @@ E70.py:12:23: E703 [*] Statement ends with an unnecessary semicolon
|
||||||
|
|
|
|
||||||
= help: Remove unnecessary semicolon
|
= help: Remove unnecessary semicolon
|
||||||
|
|
||||||
ℹ Suggested fix
|
ℹ Fix
|
||||||
9 9 | #: E703:1:13
|
9 9 | #: E703:1:13
|
||||||
10 10 | import shlex;
|
10 10 | import shlex;
|
||||||
11 11 | #: E702:1:9 E703:1:23
|
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
|
= help: Remove unnecessary semicolon
|
||||||
|
|
||||||
ℹ Suggested fix
|
ℹ Fix
|
||||||
22 22 | while all is round:
|
22 22 | while all is round:
|
||||||
23 23 | def f(x): return 2*x
|
23 23 | def f(x): return 2*x
|
||||||
24 24 | #: E704:1:8 E702:1:11 E703:1:14
|
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
|
27 27 | if True: lambda a: b
|
||||||
28 28 | #: E701:1:10
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue