Skip to content

Commit

Permalink
[pycodestyle] autofix useless semicolons
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrugman committed Feb 17, 2023
1 parent a10a500 commit e832879
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/ruff/src/checkers/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn check_tokens(
// E701, E702, E703
if enforce_compound_statements {
diagnostics.extend(
pycodestyle::rules::compound_statements(tokens)
pycodestyle::rules::compound_statements(autofix, tokens)
.into_iter()
.filter(|diagnostic| settings.rules.enabled(diagnostic.kind.rule())),
);
Expand Down
18 changes: 14 additions & 4 deletions crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use rustpython_parser::lexer::{LexResult, Tok};
use ruff_macros::{define_violation, derive_message_formats};

use crate::ast::types::Range;
use crate::fix::Fix;
use crate::registry::Diagnostic;
use crate::violation::Violation;
use crate::settings::flags;
use crate::violation::{AlwaysAutofixableViolation, Violation};

define_violation!(
pub struct MultipleStatementsOnOneLineColon;
Expand All @@ -29,14 +31,18 @@ impl Violation for MultipleStatementsOnOneLineSemicolon {
define_violation!(
pub struct UselessSemicolon;
);
impl Violation for UselessSemicolon {
impl AlwaysAutofixableViolation for UselessSemicolon {
#[derive_message_formats]
fn message(&self) -> String {
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(autofix: flags::Autofix, lxr: &[LexResult]) -> Vec<Diagnostic> {
let mut diagnostics = vec![];

// Track the last seen instance of a variety of tokens.
Expand Down Expand Up @@ -92,7 +98,11 @@ pub fn compound_statements(lxr: &[LexResult]) -> Vec<Diagnostic> {
match tok {
Tok::Newline => {
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) {
diagnostic.amend(Fix::deletion(start, end));
};
diagnostics.push(diagnostic);
}

// Reset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ expression: diagnostics
end_location:
row: 10
column: 13
fix: ~
fix:
content:
- ""
location:
row: 10
column: 12
end_location:
row: 10
column: 13
parent: ~
- kind:
UselessSemicolon: ~
Expand All @@ -20,7 +28,15 @@ expression: diagnostics
end_location:
row: 12
column: 23
fix: ~
fix:
content:
- ""
location:
row: 12
column: 22
end_location:
row: 12
column: 23
parent: ~
- kind:
UselessSemicolon: ~
Expand All @@ -30,6 +46,14 @@ expression: diagnostics
end_location:
row: 25
column: 14
fix: ~
fix:
content:
- ""
location:
row: 25
column: 13
end_location:
row: 25
column: 14
parent: ~

0 comments on commit e832879

Please sign in to comment.