add auto-fix for E275 (#8133)

## Summary

First time contribute to `ruff`, so If there are low-level errors,
please forgive me. 🙇

Introduce auto fix for `E275`, this partially address #8121.

## Test Plan

Already coverd.
This commit is contained in:
Weijie Guo 2023-10-24 02:57:23 +08:00 committed by GitHub
parent 39e45aa06f
commit 833814384a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 16 deletions

View file

@ -47,8 +47,7 @@ impl AlwaysFixableViolation for MissingWhitespace {
} }
fn fix_title(&self) -> String { fn fix_title(&self) -> String {
let token = self.token_text(); format!("Add missing whitespace")
format!("Added missing whitespace after '{token}'")
} }
} }

View file

@ -1,4 +1,4 @@
use ruff_diagnostics::Violation; use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix};
use ruff_macros::{derive_message_formats, violation}; use ruff_macros::{derive_message_formats, violation};
use ruff_python_parser::TokenKind; use ruff_python_parser::TokenKind;
use ruff_text_size::Ranged; use ruff_text_size::Ranged;
@ -29,11 +29,15 @@ use crate::rules::pycodestyle::rules::logical_lines::LogicalLine;
#[violation] #[violation]
pub struct MissingWhitespaceAfterKeyword; pub struct MissingWhitespaceAfterKeyword;
impl Violation for MissingWhitespaceAfterKeyword { impl AlwaysFixableViolation for MissingWhitespaceAfterKeyword {
#[derive_message_formats] #[derive_message_formats]
fn message(&self) -> String { fn message(&self) -> String {
format!("Missing whitespace after keyword") format!("Missing whitespace after keyword")
} }
fn fix_title(&self) -> String {
format!("Added missing whitespace after keyword")
}
} }
/// E275 /// E275
@ -59,7 +63,9 @@ pub(crate) fn missing_whitespace_after_keyword(
)) ))
&& tok0.end() == tok1.start() && tok0.end() == tok1.start()
{ {
context.push(MissingWhitespaceAfterKeyword, tok0.range()); let mut diagnostic = Diagnostic::new(MissingWhitespaceAfterKeyword, tok0.range());
diagnostic.set_fix(Fix::safe_edit(Edit::insertion(" ".to_string(), tok0.end())));
context.push_diagnostic(diagnostic);
} }
} }
} }

View file

@ -9,7 +9,7 @@ E23.py:2:7: E231 [*] Missing whitespace after ','
3 | #: E231 3 | #: E231
4 | a[b1,:] 4 | a[b1,:]
| |
= help: Added missing whitespace after ',' = help: Add missing whitespace
Fix Fix
1 1 | #: E231 1 1 | #: E231
@ -28,7 +28,7 @@ E23.py:4:5: E231 [*] Missing whitespace after ','
5 | #: E231 5 | #: E231
6 | a = [{'a':''}] 6 | a = [{'a':''}]
| |
= help: Added missing whitespace after ',' = help: Add missing whitespace
Fix Fix
1 1 | #: E231 1 1 | #: E231
@ -49,7 +49,7 @@ E23.py:6:10: E231 [*] Missing whitespace after ':'
7 | #: Okay 7 | #: Okay
8 | a = (4,) 8 | a = (4,)
| |
= help: Added missing whitespace after ':' = help: Add missing whitespace
Fix Fix
3 3 | #: E231 3 3 | #: E231
@ -69,7 +69,7 @@ E23.py:19:10: E231 [*] Missing whitespace after ','
| ^ E231 | ^ E231
20 | pass 20 | pass
| |
= help: Added missing whitespace after ',' = help: Add missing whitespace
Fix Fix
16 16 | 16 16 |
@ -89,7 +89,7 @@ E23.py:29:20: E231 [*] Missing whitespace after ':'
| ^ E231 | ^ E231
30 | } 30 | }
| |
= help: Added missing whitespace after ':' = help: Add missing whitespace
Fix Fix
26 26 | #: E231:2:20 26 26 | #: E231:2:20
@ -109,7 +109,7 @@ E23.py:33:6: E231 [*] Missing whitespace after ','
34 | 34 |
35 | # Okay because it's hard to differentiate between the usages of a colon in a f-string 35 | # Okay because it's hard to differentiate between the usages of a colon in a f-string
| |
= help: Added missing whitespace after ',' = help: Add missing whitespace
Fix Fix
30 30 | } 30 30 | }

View file

@ -1,7 +1,7 @@
--- ---
source: crates/ruff_linter/src/rules/pycodestyle/mod.rs source: crates/ruff_linter/src/rules/pycodestyle/mod.rs
--- ---
E27.py:37:8: E275 Missing whitespace after keyword E27.py:37:8: E275 [*] Missing whitespace after keyword
| |
35 | from w import (e, f) 35 | from w import (e, f)
36 | #: E275 36 | #: E275
@ -10,8 +10,19 @@ E27.py:37:8: E275 Missing whitespace after keyword
38 | #: E275 38 | #: E275
39 | from importable.module import(e, f) 39 | from importable.module import(e, f)
| |
= help: Added missing whitespace after keyword
E27.py:39:24: E275 Missing whitespace after keyword Fix
34 34 | #: E271
35 35 | from w import (e, f)
36 36 | #: E275
37 |-from w import(e, f)
37 |+from w import (e, f)
38 38 | #: E275
39 39 | from importable.module import(e, f)
40 40 | #: E275
E27.py:39:24: E275 [*] Missing whitespace after keyword
| |
37 | from w import(e, f) 37 | from w import(e, f)
38 | #: E275 38 | #: E275
@ -20,8 +31,19 @@ E27.py:39:24: E275 Missing whitespace after keyword
40 | #: E275 40 | #: E275
41 | try: 41 | try:
| |
= help: Added missing whitespace after keyword
E27.py:42:28: E275 Missing whitespace after keyword Fix
36 36 | #: E275
37 37 | from w import(e, f)
38 38 | #: E275
39 |-from importable.module import(e, f)
39 |+from importable.module import (e, f)
40 40 | #: E275
41 41 | try:
42 42 | from importable.module import(e, f)
E27.py:42:28: E275 [*] Missing whitespace after keyword
| |
40 | #: E275 40 | #: E275
41 | try: 41 | try:
@ -30,8 +52,19 @@ E27.py:42:28: E275 Missing whitespace after keyword
43 | except ImportError: 43 | except ImportError:
44 | pass 44 | pass
| |
= help: Added missing whitespace after keyword
E27.py:46:1: E275 Missing whitespace after keyword Fix
39 39 | from importable.module import(e, f)
40 40 | #: E275
41 41 | try:
42 |- from importable.module import(e, f)
42 |+ from importable.module import (e, f)
43 43 | except ImportError:
44 44 | pass
45 45 | #: E275
E27.py:46:1: E275 [*] Missing whitespace after keyword
| |
44 | pass 44 | pass
45 | #: E275 45 | #: E275
@ -40,8 +73,19 @@ E27.py:46:1: E275 Missing whitespace after keyword
47 | pass 47 | pass
48 | else: 48 | else:
| |
= help: Added missing whitespace after keyword
E27.py:54:5: E275 Missing whitespace after keyword Fix
43 43 | except ImportError:
44 44 | pass
45 45 | #: E275
46 |-if(foo):
46 |+if (foo):
47 47 | pass
48 48 | else:
49 49 | pass
E27.py:54:5: E275 [*] Missing whitespace after keyword
| |
52 | #: E275:2:11 52 | #: E275:2:11
53 | if True: 53 | if True:
@ -50,5 +94,16 @@ E27.py:54:5: E275 Missing whitespace after keyword
55 | #: Okay 55 | #: Okay
56 | def f(): 56 | def f():
| |
= help: Added missing whitespace after keyword
Fix
51 51 | matched = {"true": True, "false": False}
52 52 | #: E275:2:11
53 53 | if True:
54 |- assert(1)
54 |+ assert (1)
55 55 | #: Okay
56 56 | def f():
57 57 | print((yield))