Skip to content

Commit

Permalink
add auto-fix for E225,226,227,228 (#8136)
Browse files Browse the repository at this point in the history
## Summary

Introduce auto fix for `E225`,`E226`,`E227`,`E228`. This partially
address #8121.

## Test Plan

Already covered.
  • Loading branch information
reswqa committed Oct 23, 2023
1 parent 5a95b25 commit 7100e12
Show file tree
Hide file tree
Showing 6 changed files with 770 additions and 191 deletions.
12 changes: 8 additions & 4 deletions crates/ruff_cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,9 @@ fn nursery_group_selector_preview_enabled() {
exit_code: 1
----- stdout -----
-:1:1: CPY001 Missing copyright notice at top of file
-:1:2: E225 Missing whitespace around operator
-:1:2: E225 [*] Missing whitespace around operator
Found 2 errors.
[*] 1 fixable with the `--fix` option.
----- stderr -----
warning: The `NURSERY` selector has been deprecated.
Expand All @@ -655,8 +656,9 @@ fn preview_enabled_prefix() {
exit_code: 1
----- stdout -----
-:1:1: E741 Ambiguous variable name: `I`
-:1:2: E225 Missing whitespace around operator
-:1:2: E225 [*] Missing whitespace around operator
Found 2 errors.
[*] 1 fixable with the `--fix` option.
----- stderr -----
"###);
Expand All @@ -675,8 +677,9 @@ fn preview_enabled_all() {
-:1:1: E741 Ambiguous variable name: `I`
-:1:1: D100 Missing docstring in public module
-:1:1: CPY001 Missing copyright notice at top of file
-:1:2: E225 Missing whitespace around operator
-:1:2: E225 [*] Missing whitespace around operator
Found 4 errors.
[*] 1 fixable with the `--fix` option.
----- stderr -----
warning: `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible. Ignoring `one-blank-line-before-class`.
Expand All @@ -695,8 +698,9 @@ fn preview_enabled_direct() {
success: false
exit_code: 1
----- stdout -----
-:1:2: E225 Missing whitespace around operator
-:1:2: E225 [*] Missing whitespace around operator
Found 1 error.
[*] 1 fixable with the `--fix` option.
----- stderr -----
"###);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_diagnostics::{DiagnosticKind, Violation};
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, DiagnosticKind, Edit, Fix};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_parser::TokenKind;
use ruff_text_size::Ranged;
Expand Down Expand Up @@ -30,11 +30,15 @@ use crate::rules::pycodestyle::rules::logical_lines::LogicalLine;
#[violation]
pub struct MissingWhitespaceAroundOperator;

impl Violation for MissingWhitespaceAroundOperator {
impl AlwaysFixableViolation for MissingWhitespaceAroundOperator {
#[derive_message_formats]
fn message(&self) -> String {
format!("Missing whitespace around operator")
}

fn fix_title(&self) -> String {
format!("Add missing whitespace")
}
}

/// ## What it does
Expand All @@ -59,11 +63,15 @@ impl Violation for MissingWhitespaceAroundOperator {
#[violation]
pub struct MissingWhitespaceAroundArithmeticOperator;

impl Violation for MissingWhitespaceAroundArithmeticOperator {
impl AlwaysFixableViolation for MissingWhitespaceAroundArithmeticOperator {
#[derive_message_formats]
fn message(&self) -> String {
format!("Missing whitespace around arithmetic operator")
}

fn fix_title(&self) -> String {
format!("Add missing whitespace")
}
}

/// ## What it does
Expand All @@ -88,11 +96,15 @@ impl Violation for MissingWhitespaceAroundArithmeticOperator {
#[violation]
pub struct MissingWhitespaceAroundBitwiseOrShiftOperator;

impl Violation for MissingWhitespaceAroundBitwiseOrShiftOperator {
impl AlwaysFixableViolation for MissingWhitespaceAroundBitwiseOrShiftOperator {
#[derive_message_formats]
fn message(&self) -> String {
format!("Missing whitespace around bitwise or shift operator")
}

fn fix_title(&self) -> String {
format!("Add missing whitespace")
}
}

/// ## What it does
Expand All @@ -117,11 +129,15 @@ impl Violation for MissingWhitespaceAroundBitwiseOrShiftOperator {
#[violation]
pub struct MissingWhitespaceAroundModuloOperator;

impl Violation for MissingWhitespaceAroundModuloOperator {
impl AlwaysFixableViolation for MissingWhitespaceAroundModuloOperator {
#[derive_message_formats]
fn message(&self) -> String {
format!("Missing whitespace around modulo operator")
}

fn fix_title(&self) -> String {
format!("Add missing whitespace")
}
}

/// E225, E226, E227, E228
Expand Down Expand Up @@ -211,15 +227,35 @@ pub(crate) fn missing_whitespace_around_operator(

match (has_leading_trivia, has_trailing_trivia) {
// Operator with trailing but no leading space, enforce consistent spacing.
(false, true) |
(false, true) => {
let mut diagnostic =
Diagnostic::new(diagnostic_kind_for_operator(kind), token.range());
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
" ".to_string(),
token.start(),
)));
context.push_diagnostic(diagnostic);
}
// Operator with leading but no trailing space, enforce consistent spacing.
(true, false) => {
context.push(MissingWhitespaceAroundOperator, token.range());
let mut diagnostic =
Diagnostic::new(diagnostic_kind_for_operator(kind), token.range());
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
" ".to_string(),
token.end(),
)));
context.push_diagnostic(diagnostic);
}
// Operator with no space, require spaces if it is required by the operator.
(false, false) => {
if needs_space == NeedsSpace::Yes {
context.push(diagnostic_kind_for_operator(kind), token.range());
let mut diagnostic =
Diagnostic::new(diagnostic_kind_for_operator(kind), token.range());
diagnostic.set_fix(Fix::safe_edits(
Edit::insertion(" ".to_string(), token.start()),
[Edit::insertion(" ".to_string(), token.end())],
));
context.push_diagnostic(diagnostic);
}
}
(true, true) => {
Expand Down
Loading

0 comments on commit 7100e12

Please sign in to comment.