From 8593739f88387a7c47ae0e39884d8cfb041ea64f Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Thu, 23 Mar 2023 01:32:00 +0900 Subject: [PATCH] Check indentation level when executing `E231` (#3668) --- .../test/fixtures/pycodestyle/E23.py | 5 ++++ crates/ruff/src/checkers/logical_lines.rs | 4 +++- .../pycodestyle/rules/missing_whitespace.rs | 24 +++++++++++++++---- ...ules__pycodestyle__tests__E231_E23.py.snap | 20 ++++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/pycodestyle/E23.py b/crates/ruff/resources/test/fixtures/pycodestyle/E23.py index e561fe795f9b4..f47b124ed66c0 100644 --- a/crates/ruff/resources/test/fixtures/pycodestyle/E23.py +++ b/crates/ruff/resources/test/fixtures/pycodestyle/E23.py @@ -13,3 +13,8 @@ 'key1': 'value', 'key2': 'value', } + +def foo() -> None: + #: E231 + if (1,2): + pass diff --git a/crates/ruff/src/checkers/logical_lines.rs b/crates/ruff/src/checkers/logical_lines.rs index 75eb4a9a37b9d..bdb8e19534b16 100644 --- a/crates/ruff/src/checkers/logical_lines.rs +++ b/crates/ruff/src/checkers/logical_lines.rs @@ -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); } diff --git a/crates/ruff/src/rules/pycodestyle/rules/missing_whitespace.rs b/crates/ruff/src/rules/pycodestyle/rules/missing_whitespace.rs index 323a31321d288..6aac1beed15f4 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/missing_whitespace.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/missing_whitespace.rs @@ -33,7 +33,12 @@ impl AlwaysAutofixableViolation for MissingWhitespace { /// E231 #[cfg(debug_assertions)] -pub fn missing_whitespace(line: &str, row: usize, autofix: bool) -> Vec { +pub fn missing_whitespace( + line: &str, + row: usize, + autofix: bool, + indent_level: usize, +) -> Vec { let mut diagnostics = vec![]; for (idx, char) in line.chars().enumerate() { if idx + 1 == line.len() { @@ -62,11 +67,17 @@ pub fn missing_whitespace(line: &str, row: usize, autofix: bool) -> Vec Vec Vec { +pub fn missing_whitespace( + _line: &str, + _row: usize, + _autofix: bool, + indent_level: usize, +) -> Vec { vec![] } diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E231_E23.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E231_E23.py.snap index ef428817c8d5b..06714dcb4c1f5 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E231_E23.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E231_E23.py.snap @@ -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: ~