mirror of https://github.com/astral-sh/ruff
[`pycodestyle`] autofix useless semicolons (#3001)
This commit is contained in:
parent
db7f16e276
commit
9e45424ed6
|
|
@ -117,7 +117,7 @@ pub 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)
|
pycodestyle::rules::compound_statements(tokens, settings, autofix)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|diagnostic| settings.rules.enabled(diagnostic.kind.rule())),
|
.filter(|diagnostic| settings.rules.enabled(diagnostic.kind.rule())),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,10 @@ use rustpython_parser::lexer::{LexResult, Tok};
|
||||||
use ruff_macros::{define_violation, derive_message_formats};
|
use ruff_macros::{define_violation, derive_message_formats};
|
||||||
|
|
||||||
use crate::ast::types::Range;
|
use crate::ast::types::Range;
|
||||||
use crate::registry::Diagnostic;
|
use crate::fix::Fix;
|
||||||
use crate::violation::Violation;
|
use crate::registry::{Diagnostic, Rule};
|
||||||
|
use crate::settings::{flags, Settings};
|
||||||
|
use crate::violation::{AlwaysAutofixableViolation, Violation};
|
||||||
|
|
||||||
define_violation!(
|
define_violation!(
|
||||||
pub struct MultipleStatementsOnOneLineColon;
|
pub struct MultipleStatementsOnOneLineColon;
|
||||||
|
|
@ -29,14 +31,22 @@ impl Violation for MultipleStatementsOnOneLineSemicolon {
|
||||||
define_violation!(
|
define_violation!(
|
||||||
pub struct UselessSemicolon;
|
pub struct UselessSemicolon;
|
||||||
);
|
);
|
||||||
impl Violation for UselessSemicolon {
|
impl AlwaysAutofixableViolation for UselessSemicolon {
|
||||||
#[derive_message_formats]
|
#[derive_message_formats]
|
||||||
fn message(&self) -> String {
|
fn message(&self) -> String {
|
||||||
format!("Statement ends with an unnecessary semicolon")
|
format!("Statement ends with an unnecessary semicolon")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn autofix_title(&self) -> String {
|
||||||
|
format!("Remove unnecessary semicolon")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compound_statements(lxr: &[LexResult]) -> Vec<Diagnostic> {
|
pub fn compound_statements(
|
||||||
|
lxr: &[LexResult],
|
||||||
|
settings: &Settings,
|
||||||
|
autofix: flags::Autofix,
|
||||||
|
) -> 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.
|
||||||
|
|
@ -92,7 +102,13 @@ pub fn compound_statements(lxr: &[LexResult]) -> Vec<Diagnostic> {
|
||||||
match tok {
|
match tok {
|
||||||
Tok::Newline => {
|
Tok::Newline => {
|
||||||
if let Some((start, end)) = semi {
|
if let Some((start, end)) = semi {
|
||||||
diagnostics.push(Diagnostic::new(UselessSemicolon, Range::new(start, end)));
|
let mut diagnostic = Diagnostic::new(UselessSemicolon, Range::new(start, end));
|
||||||
|
if matches!(autofix, flags::Autofix::Enabled)
|
||||||
|
&& settings.rules.should_fix(&Rule::UselessSemicolon)
|
||||||
|
{
|
||||||
|
diagnostic.amend(Fix::deletion(start, end));
|
||||||
|
};
|
||||||
|
diagnostics.push(diagnostic);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset.
|
// Reset.
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,15 @@ expression: diagnostics
|
||||||
end_location:
|
end_location:
|
||||||
row: 10
|
row: 10
|
||||||
column: 13
|
column: 13
|
||||||
fix: ~
|
fix:
|
||||||
|
content:
|
||||||
|
- ""
|
||||||
|
location:
|
||||||
|
row: 10
|
||||||
|
column: 12
|
||||||
|
end_location:
|
||||||
|
row: 10
|
||||||
|
column: 13
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
UselessSemicolon: ~
|
UselessSemicolon: ~
|
||||||
|
|
@ -20,7 +28,15 @@ expression: diagnostics
|
||||||
end_location:
|
end_location:
|
||||||
row: 12
|
row: 12
|
||||||
column: 23
|
column: 23
|
||||||
fix: ~
|
fix:
|
||||||
|
content:
|
||||||
|
- ""
|
||||||
|
location:
|
||||||
|
row: 12
|
||||||
|
column: 22
|
||||||
|
end_location:
|
||||||
|
row: 12
|
||||||
|
column: 23
|
||||||
parent: ~
|
parent: ~
|
||||||
- kind:
|
- kind:
|
||||||
UselessSemicolon: ~
|
UselessSemicolon: ~
|
||||||
|
|
@ -30,6 +46,14 @@ expression: diagnostics
|
||||||
end_location:
|
end_location:
|
||||||
row: 25
|
row: 25
|
||||||
column: 14
|
column: 14
|
||||||
fix: ~
|
fix:
|
||||||
|
content:
|
||||||
|
- ""
|
||||||
|
location:
|
||||||
|
row: 25
|
||||||
|
column: 13
|
||||||
|
end_location:
|
||||||
|
row: 25
|
||||||
|
column: 14
|
||||||
parent: ~
|
parent: ~
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue