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

Check indentation level when executing E231 #3668

Merged
merged 2 commits into from
Mar 22, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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: ~