From be11cae619d5a24adb4da34e64d3c5f270f9727b Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 19 Jun 2023 00:19:41 -0400 Subject: [PATCH] Fix allowed-ellipsis detection (#5174) ## Summary We weren't resetting the `allow_ellipsis` flag properly, which ultimately caused us to treat the semicolon as "unnecessary" rather than "creating a multi-statement line". Closes #5154. --- .../resources/test/fixtures/pycodestyle/E70.py | 3 +++ .../rules/pycodestyle/rules/compound_statements.rs | 14 +++++++++----- ...ff__rules__pycodestyle__tests__E702_E70.py.snap | 8 ++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/pycodestyle/E70.py b/crates/ruff/resources/test/fixtures/pycodestyle/E70.py index bfbec79124..2fa6fa4813 100644 --- a/crates/ruff/resources/test/fixtures/pycodestyle/E70.py +++ b/crates/ruff/resources/test/fixtures/pycodestyle/E70.py @@ -60,3 +60,6 @@ match *0, 1, *2: #: class Foo: match: Optional[Match] = None +#: E702:2:4 +while 1: + 1;... diff --git a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs index 6f095d8272..fd9b57b6e9 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs @@ -145,6 +145,12 @@ pub(crate) fn compound_statements(lxr: &[LexResult], settings: &Settings) -> Vec Tok::Rbrace => { brace_count = brace_count.saturating_sub(1); } + Tok::Ellipsis => { + if allow_ellipsis { + allow_ellipsis = false; + continue; + } + } _ => {} } @@ -195,17 +201,15 @@ pub(crate) fn compound_statements(lxr: &[LexResult], settings: &Settings) -> Vec || with.is_some() { colon = Some((range.start(), range.end())); - allow_ellipsis = true; + + // Allow `class C: ...`-style definitions in stubs. + allow_ellipsis = class.is_some(); } } Tok::Semi => { semi = Some((range.start(), range.end())); } Tok::Comment(..) | Tok::Indent | Tok::Dedent | Tok::NonLogicalNewline => {} - Tok::Ellipsis if allow_ellipsis => { - // Allow `class C: ...`-style definitions in stubs. - allow_ellipsis = false; - } _ => { if let Some((start, end)) = semi { diagnostics.push(Diagnostic::new( diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E702_E70.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E702_E70.py.snap index 5bca9b1e53..20d62cd44b 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E702_E70.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E702_E70.py.snap @@ -61,4 +61,12 @@ E70.py:56:13: E702 Multiple statements on one line (semicolon) 58 | match *0, 1, *2: | +E70.py:65:4: E702 Multiple statements on one line (semicolon) + | +63 | #: E702:2:4 +64 | while 1: +65 | 1;... + | ^ E702 + | +