From bc84e93dc9647eb5ce31aa59898b28e9ea341531 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 25 Nov 2023 18:10:48 +0000 Subject: [PATCH] Allow space-before-colon after end-of-slice --- .../test/fixtures/pycodestyle/E20.py | 12 +++++++++++ .../logical_lines/extraneous_whitespace.rs | 19 ++++++++++++++++-- ...ules__pycodestyle__tests__E202_E20.py.snap | 20 +++++++++++++++++++ ...ules__pycodestyle__tests__E203_E20.py.snap | 20 +++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py index 84355d21e0e48..7c76aa6ed14b0 100644 --- a/crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py +++ b/crates/ruff_linter/resources/test/fixtures/pycodestyle/E20.py @@ -135,3 +135,15 @@ #: E203:1:20 ham[{lower + offset : upper + offset} : upper + offset] + +#: Okay +ham[upper:] + +#: Okay +ham[upper :] + +#: E202:1:12 +ham[upper : ] + +#: E203:1:10 +ham[upper :] diff --git a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs index bfc7bcaefd065..a4134e51cbb96 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/rules/logical_lines/extraneous_whitespace.rs @@ -213,13 +213,28 @@ pub(crate) fn extraneous_whitespace(line: &LogicalLine, context: &mut LogicalLin diagnostic.range(), ))); context.push_diagnostic(diagnostic); + } else if iter + .peek() + .is_some_and(|token| token.kind() == TokenKind::Rsqb) + { + // Allow `foo[1 :]`, but not `foo[1 :]`. + if let (Whitespace::Many | Whitespace::Tab, offset) = whitespace + { + let mut diagnostic = Diagnostic::new( + WhitespaceBeforePunctuation { symbol }, + TextRange::at(token.start() - offset, offset), + ); + diagnostic.set_fix(Fix::safe_edit(Edit::range_deletion( + diagnostic.range(), + ))); + context.push_diagnostic(diagnostic); + } } else { + // Allow, e.g., `foo[1:2]` or `foo[1 : 2]` or `foo[1 :: 2]`. let token = iter .peek() .filter(|next| matches!(next.kind(), TokenKind::Colon)) .unwrap_or(&token); - - // Allow, e.g., `foo[1:2]` or `foo[1 : 2]` or `foo[1 :: 2]`. if line.trailing_whitespace(token) != whitespace { let mut diagnostic = Diagnostic::new( WhitespaceBeforePunctuation { symbol }, diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E202_E20.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E202_E20.py.snap index 67d7bc6e28897..971a719f3da44 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E202_E20.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E202_E20.py.snap @@ -146,4 +146,24 @@ E20.py:90:18: E202 [*] Whitespace before ']' 92 92 | 93 93 | #: Okay +E20.py:146:12: E202 [*] Whitespace before ']' + | +145 | #: E202:1:12 +146 | ham[upper : ] + | ^ E202 +147 | +148 | #: E203:1:10 + | + = help: Remove whitespace before ']' + +ℹ Safe fix +143 143 | ham[upper :] +144 144 | +145 145 | #: E202:1:12 +146 |-ham[upper : ] + 146 |+ham[upper :] +147 147 | +148 148 | #: E203:1:10 +149 149 | ham[upper :] + diff --git a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E203_E20.py.snap b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E203_E20.py.snap index c81fa5d87894a..d92741af055a6 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E203_E20.py.snap +++ b/crates/ruff_linter/src/rules/pycodestyle/snapshots/ruff_linter__rules__pycodestyle__tests__E203_E20.py.snap @@ -211,6 +211,8 @@ E20.py:137:20: E203 [*] Whitespace before ':' 136 | #: E203:1:20 137 | ham[{lower + offset : upper + offset} : upper + offset] | ^ E203 +138 | +139 | #: Okay | = help: Remove whitespace before ':' @@ -220,5 +222,23 @@ E20.py:137:20: E203 [*] Whitespace before ':' 136 136 | #: E203:1:20 137 |-ham[{lower + offset : upper + offset} : upper + offset] 137 |+ham[{lower + offset: upper + offset} : upper + offset] +138 138 | +139 139 | #: Okay +140 140 | ham[upper:] + +E20.py:149:10: E203 [*] Whitespace before ':' + | +148 | #: E203:1:10 +149 | ham[upper :] + | ^^ E203 + | + = help: Remove whitespace before ':' + +ℹ Safe fix +146 146 | ham[upper : ] +147 147 | +148 148 | #: E203:1:10 +149 |-ham[upper :] + 149 |+ham[upper:]