From 9e45424ed6f16fbc77bd7b709cc8751b51529674 Mon Sep 17 00:00:00 2001 From: Simon Brugman Date: Sat, 18 Feb 2023 00:52:42 +0100 Subject: [PATCH] [`pycodestyle`] autofix useless semicolons (#3001) --- crates/ruff/src/checkers/tokens.rs | 2 +- .../pycodestyle/rules/compound_statements.rs | 26 ++++++++++++---- ...ules__pycodestyle__tests__E703_E70.py.snap | 30 +++++++++++++++++-- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/crates/ruff/src/checkers/tokens.rs b/crates/ruff/src/checkers/tokens.rs index d717cad113..5c4d337f72 100644 --- a/crates/ruff/src/checkers/tokens.rs +++ b/crates/ruff/src/checkers/tokens.rs @@ -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(tokens, settings, autofix) .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 a38b608e1f..29d07bf979 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs @@ -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::registry::Diagnostic; -use crate::violation::Violation; +use crate::fix::Fix; +use crate::registry::{Diagnostic, Rule}; +use crate::settings::{flags, Settings}; +use crate::violation::{AlwaysAutofixableViolation, Violation}; define_violation!( pub struct MultipleStatementsOnOneLineColon; @@ -29,14 +31,22 @@ 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 { +pub fn compound_statements( + lxr: &[LexResult], + settings: &Settings, + autofix: flags::Autofix, +) -> Vec { let mut diagnostics = vec![]; // Track the last seen instance of a variety of tokens. @@ -92,7 +102,13 @@ pub fn compound_statements(lxr: &[LexResult]) -> Vec { 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) + && settings.rules.should_fix(&Rule::UselessSemicolon) + { + diagnostic.amend(Fix::deletion(start, end)); + }; + diagnostics.push(diagnostic); } // Reset. 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 50f0b62125..696c228245 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 @@ -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: ~ @@ -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: ~ @@ -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: ~