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 E252 #8142

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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, TextSize};
Expand Down Expand Up @@ -69,11 +69,15 @@ impl Violation for UnexpectedSpacesAroundKeywordParameterEquals {
#[violation]
pub struct MissingWhitespaceAroundParameterEquals;

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

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

fn is_in_def(tokens: &[LogicalLineToken]) -> bool {
Expand Down Expand Up @@ -131,7 +135,13 @@ pub(crate) fn whitespace_around_named_parameter_equals(
if annotated_func_arg && parens == 1 {
let start = token.start();
if start == prev_end && prev_end != TextSize::new(0) {
context.push(MissingWhitespaceAroundParameterEquals, token.range());
let mut diagnostic =
Diagnostic::new(MissingWhitespaceAroundParameterEquals, token.range);
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
" ".to_string(),
token.start(),
)));
context.push_diagnostic(diagnostic);
}

while let Some(next) = iter.peek() {
Expand All @@ -141,7 +151,15 @@ pub(crate) fn whitespace_around_named_parameter_equals(
let next_start = next.start();

if next_start == token.end() {
context.push(MissingWhitespaceAroundParameterEquals, token.range());
let mut diagnostic = Diagnostic::new(
MissingWhitespaceAroundParameterEquals,
token.range,
);
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(
" ".to_string(),
token.end(),
)));
context.push_diagnostic(diagnostic);
}
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
---
E25.py:46:15: E252 Missing whitespace around parameter equals
E25.py:46:15: E252 [*] Missing whitespace around parameter equals
|
44 | return a + b
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
Expand All @@ -10,8 +10,19 @@ E25.py:46:15: E252 Missing whitespace around parameter equals
47 | return a + b + c
48 | #: Okay
|
= help: Add missing whitespace

E25.py:46:15: E252 Missing whitespace around parameter equals
ℹ Fix
43 43 | async def add(a: int = 0, b: int = 0) -> int:
44 44 | return a + b
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
46 |+def add(a: int =0, b: int =0, c: int= 0) -> int:
47 47 | return a + b + c
48 48 | #: Okay
49 49 | def add(a: int = _default(name='f')):

E25.py:46:15: E252 [*] Missing whitespace around parameter equals
|
44 | return a + b
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
Expand All @@ -20,8 +31,19 @@ E25.py:46:15: E252 Missing whitespace around parameter equals
47 | return a + b + c
48 | #: Okay
|
= help: Add missing whitespace

ℹ Fix
43 43 | async def add(a: int = 0, b: int = 0) -> int:
44 44 | return a + b
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
46 |+def add(a: int= 0, b: int =0, c: int= 0) -> int:
47 47 | return a + b + c
48 48 | #: Okay
49 49 | def add(a: int = _default(name='f')):

E25.py:46:26: E252 Missing whitespace around parameter equals
E25.py:46:26: E252 [*] Missing whitespace around parameter equals
|
44 | return a + b
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
Expand All @@ -30,8 +52,19 @@ E25.py:46:26: E252 Missing whitespace around parameter equals
47 | return a + b + c
48 | #: Okay
|
= help: Add missing whitespace

E25.py:46:36: E252 Missing whitespace around parameter equals
ℹ Fix
43 43 | async def add(a: int = 0, b: int = 0) -> int:
44 44 | return a + b
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
46 |+def add(a: int=0, b: int = 0, c: int= 0) -> int:
47 47 | return a + b + c
48 48 | #: Okay
49 49 | def add(a: int = _default(name='f')):

E25.py:46:36: E252 [*] Missing whitespace around parameter equals
|
44 | return a + b
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
Expand All @@ -40,5 +73,16 @@ E25.py:46:36: E252 Missing whitespace around parameter equals
47 | return a + b + c
48 | #: Okay
|
= help: Add missing whitespace

ℹ Fix
43 43 | async def add(a: int = 0, b: int = 0) -> int:
44 44 | return a + b
45 45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
46 |-def add(a: int=0, b: int =0, c: int= 0) -> int:
46 |+def add(a: int=0, b: int =0, c: int = 0) -> int:
47 47 | return a + b + c
48 48 | #: Okay
49 49 | def add(a: int = _default(name='f')):


Loading