From abc5065fc7ed4f5e41354024fe06ee220d30301d Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 21 Aug 2023 20:47:20 -0400 Subject: [PATCH] Avoid E231 if comma is at end-of-line (#6747) ## Summary I don't know how this could come up in valid Python, but anyway... Closes https://github.com/astral-sh/ruff/issues/6738. --- crates/ruff/resources/test/fixtures/pycodestyle/E23.py | 3 +++ .../pycodestyle/rules/logical_lines/missing_whitespace.rs | 4 ++-- .../ruff__rules__pycodestyle__tests__E231_E23.py.snap | 2 ++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/pycodestyle/E23.py b/crates/ruff/resources/test/fixtures/pycodestyle/E23.py index e2c6b9fe66..d2bb50b05d 100644 --- a/crates/ruff/resources/test/fixtures/pycodestyle/E23.py +++ b/crates/ruff/resources/test/fixtures/pycodestyle/E23.py @@ -28,3 +28,6 @@ mdtypes_template = { 'tag_full': [('mdtype', 'u4'), ('byte_count', 'u4')], 'tag_smalldata':[('byte_count_mdtype', 'u4'), ('data', 'S4')], } + +#: Okay +a = (1, diff --git a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs index aac8295da1..6c90b80bc9 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/logical_lines/missing_whitespace.rs @@ -81,10 +81,10 @@ pub(crate) fn missing_whitespace( TokenKind::Comma | TokenKind::Semi | TokenKind::Colon => { let after = line.text_after(token); - if !after + if after .chars() .next() - .is_some_and(|c| char::is_whitespace(c) || c == '\\') + .is_some_and(|c| !(char::is_whitespace(c) || c == '\\')) { if let Some(next_token) = iter.peek() { match (kind, next_token.kind()) { 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 34bd1a6274..7a197e5247 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 @@ -98,5 +98,7 @@ E23.py:29:20: E231 [*] Missing whitespace after ':' 29 |- 'tag_smalldata':[('byte_count_mdtype', 'u4'), ('data', 'S4')], 29 |+ 'tag_smalldata': [('byte_count_mdtype', 'u4'), ('data', 'S4')], 30 30 | } +31 31 | +32 32 | #: Okay