add auto-fix for E252 (#8142)

## Summary

Introduce auto fix for `E252`. This partially address #8121.

## Test Plan

Already covered.
This commit is contained in:
Weijie Guo 2023-10-24 02:57:58 +08:00 committed by GitHub
parent 833814384a
commit 5a95b25aa8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 8 deletions

View file

@ -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};
@ -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 {
@ -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() {
@ -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;
}

View file

@ -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
@ -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
@ -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
E25.py:46:26: 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:26: E252 [*] Missing whitespace around parameter equals
|
44 | return a + b
45 | #: E252:1:15 E252:1:16 E252:1:27 E252:1:36
@ -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
@ -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')):