Skip to content

Commit

Permalink
Check indentation level when executing E231 (#3668)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoto7250 authored Mar 22, 2023
1 parent 242dd3d commit 8593739
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
5 changes: 5 additions & 0 deletions crates/ruff/resources/test/fixtures/pycodestyle/E23.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
'key1': 'value',
'key2': 'value',
}

def foo() -> None:
#: E231
if (1,2):
pass
4 changes: 3 additions & 1 deletion crates/ruff/src/checkers/logical_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ pub fn check_logical_lines(
#[cfg(not(debug_assertions))]
let should_fix = false;

for diagnostic in missing_whitespace(&line.text, start_loc.row(), should_fix) {
for diagnostic in
missing_whitespace(&line.text, start_loc.row(), should_fix, indent_level)
{
if settings.rules.enabled(diagnostic.kind.rule()) {
diagnostics.push(diagnostic);
}
Expand Down
24 changes: 20 additions & 4 deletions crates/ruff/src/rules/pycodestyle/rules/missing_whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ impl AlwaysAutofixableViolation for MissingWhitespace {

/// E231
#[cfg(debug_assertions)]
pub fn missing_whitespace(line: &str, row: usize, autofix: bool) -> Vec<Diagnostic> {
pub fn missing_whitespace(
line: &str,
row: usize,
autofix: bool,
indent_level: usize,
) -> Vec<Diagnostic> {
let mut diagnostics = vec![];
for (idx, char) in line.chars().enumerate() {
if idx + 1 == line.len() {
Expand Down Expand Up @@ -62,11 +67,17 @@ pub fn missing_whitespace(line: &str, row: usize, autofix: bool) -> Vec<Diagnost

let mut diagnostic = Diagnostic::new(
kind,
Range::new(Location::new(row, idx), Location::new(row, idx)),
Range::new(
Location::new(row, indent_level + idx),
Location::new(row, indent_level + idx),
),
);

if autofix {
diagnostic.amend(Fix::insertion(" ".to_string(), Location::new(row, idx + 1)));
diagnostic.amend(Fix::insertion(
" ".to_string(),
Location::new(row, indent_level + idx + 1),
));
}
diagnostics.push(diagnostic);
}
Expand All @@ -75,6 +86,11 @@ pub fn missing_whitespace(line: &str, row: usize, autofix: bool) -> Vec<Diagnost
}

#[cfg(not(debug_assertions))]
pub fn missing_whitespace(_line: &str, _row: usize, _autofix: bool) -> Vec<Diagnostic> {
pub fn missing_whitespace(
_line: &str,
_row: usize,
_autofix: bool,
indent_level: usize,
) -> Vec<Diagnostic> {
vec![]
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,24 @@ expression: diagnostics
row: 6
column: 10
parent: ~
- kind:
name: MissingWhitespace
body: "Missing whitespace after ','"
suggestion: "Added missing whitespace after ','"
fixable: true
location:
row: 19
column: 9
end_location:
row: 19
column: 9
fix:
content: " "
location:
row: 19
column: 10
end_location:
row: 19
column: 10
parent: ~

0 comments on commit 8593739

Please sign in to comment.