Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add auto-fix for E223,224,242 #8143

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_diagnostics::Violation;
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_parser::TokenKind;
use ruff_text_size::{Ranged, TextRange};
Expand Down Expand Up @@ -28,11 +28,15 @@ use super::{LogicalLine, Whitespace};
#[violation]
pub struct TabBeforeOperator;

impl Violation for TabBeforeOperator {
impl AlwaysFixableViolation for TabBeforeOperator {
#[derive_message_formats]
fn message(&self) -> String {
format!("Tab before operator")
}

fn fix_title(&self) -> String {
format!("Replaced tab before operator by whitespace")
}
}

/// ## What it does
Expand Down Expand Up @@ -84,11 +88,15 @@ impl Violation for MultipleSpacesBeforeOperator {
#[violation]
pub struct TabAfterOperator;

impl Violation for TabAfterOperator {
impl AlwaysFixableViolation for TabAfterOperator {
#[derive_message_formats]
fn message(&self) -> String {
format!("Tab after operator")
}

fn fix_title(&self) -> String {
format!("Replaced tab after operator by whitespace")
}
}

/// ## What it does
Expand Down Expand Up @@ -138,11 +146,15 @@ impl Violation for MultipleSpacesAfterOperator {
#[violation]
pub struct TabAfterComma;

impl Violation for TabAfterComma {
impl AlwaysFixableViolation for TabAfterComma {
#[derive_message_formats]
fn message(&self) -> String {
format!("Tab after comma")
}

fn fix_title(&self) -> String {
format!("Replaced tab after comma by whitespace")
}
}

/// ## What it does
Expand Down Expand Up @@ -181,10 +193,15 @@ pub(crate) fn space_around_operator(line: &LogicalLine, context: &mut LogicalLin
if !after_operator {
match line.leading_whitespace(token) {
(Whitespace::Tab, offset) => {
context.push(
let mut diagnostic = Diagnostic::new(
TabBeforeOperator,
TextRange::at(token.start() - offset, offset),
);
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
" ".to_string(),
TextRange::at(token.start() - offset, offset),
)));
context.push_diagnostic(diagnostic);
}
(Whitespace::Many, offset) => {
context.push(
Expand All @@ -198,7 +215,13 @@ pub(crate) fn space_around_operator(line: &LogicalLine, context: &mut LogicalLin

match line.trailing_whitespace(token) {
(Whitespace::Tab, len) => {
context.push(TabAfterOperator, TextRange::at(token.end(), len));
let mut diagnostic =
Diagnostic::new(TabAfterOperator, TextRange::at(token.end(), len));
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
" ".to_string(),
TextRange::at(token.end(), len),
)));
context.push_diagnostic(diagnostic);
}
(Whitespace::Many, len) => {
context.push(MultipleSpacesAfterOperator, TextRange::at(token.end(), len));
Expand All @@ -217,7 +240,13 @@ pub(crate) fn space_after_comma(line: &LogicalLine, context: &mut LogicalLinesCo
if matches!(token.kind(), TokenKind::Comma) {
match line.trailing_whitespace(token) {
(Whitespace::Tab, len) => {
context.push(TabAfterComma, TextRange::at(token.end(), len));
let mut diagnostic =
Diagnostic::new(TabAfterComma, TextRange::at(token.end(), len));
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
" ".to_string(),
TextRange::at(token.end(), len),
)));
context.push_diagnostic(diagnostic);
}
(Whitespace::Many, len) => {
context.push(MultipleSpacesAfterComma, TextRange::at(token.end(), len));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
E22.py:43:2: E223 Tab before operator
E22.py:43:2: E223 [*] Tab before operator
|
41 | #: E223
42 | foobart = 4
43 | a = 3 # aligned with tab
| ^^^ E223
44 | #:
|
= help: Replaced tab before operator by whitespace

ℹ Fix
40 40 |
41 41 | #: E223
42 42 | foobart = 4
43 |-a = 3 # aligned with tab
43 |+a = 3 # aligned with tab
44 44 | #:
45 45 |
46 46 |


Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
E22.py:48:5: E224 Tab after operator
E22.py:48:5: E224 [*] Tab after operator
|
47 | #: E224
48 | a += 1
| ^^^^ E224
49 | b += 1000
50 | #:
|
= help: Replaced tab after operator by whitespace

ℹ Fix
45 45 |
46 46 |
47 47 | #: E224
48 |-a += 1
48 |+a += 1
49 49 | b += 1000
50 50 | #:
51 51 |


Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
E24.py:6:8: E242 Tab after comma
E24.py:6:8: E242 [*] Tab after comma
|
4 | b = (1, 20)
5 | #: E242
Expand All @@ -10,5 +10,16 @@ E24.py:6:8: E242 Tab after comma
7 | #: Okay
8 | b = (1, 20) # space before 20
|
= help: Replaced tab after comma by whitespace

ℹ Fix
3 3 | #: Okay
4 4 | b = (1, 20)
5 5 | #: E242
6 |-a = (1, 2) # tab before 2
6 |+a = (1, 2) # tab before 2
7 7 | #: Okay
8 8 | b = (1, 20) # space before 20
9 9 | #: E241 E241 E241


Loading