Skip to content

Commit

Permalink
add auto-fix for E275 (#8133)
Browse files Browse the repository at this point in the history
## 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.
  • Loading branch information
reswqa authored Oct 23, 2023
1 parent 39e45aa commit 8338143
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ impl AlwaysFixableViolation for MissingWhitespace {
}

fn fix_title(&self) -> String {
let token = self.token_text();
format!("Added missing whitespace after '{token}'")
format!("Add missing whitespace")
}
}

Expand Down
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};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_parser::TokenKind;
use ruff_text_size::Ranged;
Expand Down Expand Up @@ -29,11 +29,15 @@ use crate::rules::pycodestyle::rules::logical_lines::LogicalLine;
#[violation]
pub struct MissingWhitespaceAfterKeyword;

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

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

/// E275
Expand All @@ -59,7 +63,9 @@ pub(crate) fn missing_whitespace_after_keyword(
))
&& 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ E23.py:2:7: E231 [*] Missing whitespace after ','
3 | #: E231
4 | a[b1,:]
|
= help: Added missing whitespace after ','
= help: Add missing whitespace

Fix
1 1 | #: E231
Expand All @@ -28,7 +28,7 @@ E23.py:4:5: E231 [*] Missing whitespace after ','
5 | #: E231
6 | a = [{'a':''}]
|
= help: Added missing whitespace after ','
= help: Add missing whitespace

Fix
1 1 | #: E231
Expand All @@ -49,7 +49,7 @@ E23.py:6:10: E231 [*] Missing whitespace after ':'
7 | #: Okay
8 | a = (4,)
|
= help: Added missing whitespace after ':'
= help: Add missing whitespace

Fix
3 3 | #: E231
Expand All @@ -69,7 +69,7 @@ E23.py:19:10: E231 [*] Missing whitespace after ','
| ^ E231
20 | pass
|
= help: Added missing whitespace after ','
= help: Add missing whitespace

Fix
16 16 |
Expand All @@ -89,7 +89,7 @@ E23.py:29:20: E231 [*] Missing whitespace after ':'
| ^ E231
30 | }
|
= help: Added missing whitespace after ':'
= help: Add missing whitespace

Fix
26 26 | #: E231:2:20
Expand All @@ -109,7 +109,7 @@ E23.py:33:6: E231 [*] Missing whitespace after ','
34 |
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
30 30 | }
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
---
E27.py:37:8: E275 Missing whitespace after keyword
E27.py:37:8: E275 [*] Missing whitespace after keyword
|
35 | from w import (e, f)
36 | #: E275
Expand All @@ -10,8 +10,19 @@ E27.py:37:8: E275 Missing whitespace after keyword
38 | #: E275
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)
38 | #: E275
Expand All @@ -20,8 +31,19 @@ E27.py:39:24: E275 Missing whitespace after keyword
40 | #: E275
41 | try:
|
= help: Added 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
E27.py:42:28: E275 [*] Missing whitespace after keyword
|
40 | #: E275
41 | try:
Expand All @@ -30,8 +52,19 @@ E27.py:42:28: E275 Missing whitespace after keyword
43 | except ImportError:
44 | pass
|
= help: Added 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
E27.py:46:1: E275 [*] Missing whitespace after keyword
|
44 | pass
45 | #: E275
Expand All @@ -40,8 +73,19 @@ E27.py:46:1: E275 Missing whitespace after keyword
47 | pass
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
53 | if True:
Expand All @@ -50,5 +94,16 @@ E27.py:54:5: E275 Missing whitespace after keyword
55 | #: Okay
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))


0 comments on commit 8338143

Please sign in to comment.