From 0bc1f68111fbf926a5aa56b171741b3d794b1eb5 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Fri, 10 Feb 2023 15:21:06 -0500 Subject: [PATCH] Only trigger compound statements after select keywords (#2737) --- .../test/fixtures/pycodestyle/E70.py | 23 +++++- .../pycodestyle/rules/compound_statements.rs | 79 +++++++++++++++++- ...ules__pycodestyle__tests__E701_E70.py.snap | 80 +++++++++++++++++++ 3 files changed, 180 insertions(+), 2 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/pycodestyle/E70.py b/crates/ruff/resources/test/fixtures/pycodestyle/E70.py index b03f2c8ecd..d58be6a5c5 100644 --- a/crates/ruff/resources/test/fixtures/pycodestyle/E70.py +++ b/crates/ruff/resources/test/fixtures/pycodestyle/E70.py @@ -21,5 +21,26 @@ def f(x): return 2*x #: E704:2:5 E226:2:23 while all is round: def f(x): return 2*x -#: +#: E704:1:8 E702:1:11 E703:1:14 if True: x; y; +#: E701:1:8 +if True: lambda a: b +#: E701:1:10 +if a := 1: pass +# E701:1:4 E701:2:18 E701:3:8 +try: lambda foo: bar +except ValueError: pass +finally: pass +# E701:1:7 +class C: pass +# E701:1:7 +with C(): pass +# E701:1:14 +async with C(): pass +#: +lambda a: b +#: +a: List[str] = [] +#: +if a := 1: + pass diff --git a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs index 102c7bc0d3..105fa432dd 100644 --- a/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs +++ b/crates/ruff/src/rules/pycodestyle/rules/compound_statements.rs @@ -53,6 +53,16 @@ pub fn compound_statements(lxr: &[LexResult]) -> Vec { let mut def = None; let mut colon = None; let mut semi = None; + let mut class = None; + let mut elif = None; + let mut else_ = None; + let mut except = None; + let mut finally = None; + let mut for_ = None; + let mut if_ = None; + let mut try_ = None; + let mut while_ = None; + let mut with = None; // Track the bracket depth. let mut par_count = 0; @@ -96,12 +106,35 @@ pub fn compound_statements(lxr: &[LexResult]) -> Vec { def = None; colon = None; semi = None; + class = None; + elif = None; + else_ = None; + except = None; + finally = None; + for_ = None; + if_ = None; + try_ = None; + while_ = None; + with = None; } Tok::Def => { def = Some((start, end)); } Tok::Colon => { - colon = Some((start, end)); + if def.is_some() + || class.is_some() + || elif.is_some() + || else_.is_some() + || except.is_some() + || finally.is_some() + || for_.is_some() + || if_.is_some() + || try_.is_some() + || while_.is_some() + || with.is_some() + { + colon = Some((start, end)); + } } Tok::Semi => { semi = Some((start, end)); @@ -134,9 +167,53 @@ pub fn compound_statements(lxr: &[LexResult]) -> Vec { // Reset. def = None; colon = None; + class = None; + elif = None; + else_ = None; + except = None; + finally = None; + for_ = None; + if_ = None; + try_ = None; + while_ = None; + with = None; } } } + + match tok { + Tok::If => { + if_ = Some((start, end)); + } + Tok::While => { + while_ = Some((start, end)); + } + Tok::For => { + for_ = Some((start, end)); + } + Tok::Try => { + try_ = Some((start, end)); + } + Tok::Except => { + except = Some((start, end)); + } + Tok::Finally => { + finally = Some((start, end)); + } + Tok::Elif => { + elif = Some((start, end)); + } + Tok::Else => { + else_ = Some((start, end)); + } + Tok::Class => { + class = Some((start, end)); + } + Tok::With => { + with = Some((start, end)); + } + _ => {} + }; } diagnostics diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E701_E70.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E701_E70.py.snap index 31f61c5df0..8df83c5877 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E701_E70.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E701_E70.py.snap @@ -32,4 +32,84 @@ expression: diagnostics column: 8 fix: ~ parent: ~ +- kind: + MultipleStatementsOnOneLineColon: ~ + location: + row: 27 + column: 7 + end_location: + row: 27 + column: 8 + fix: ~ + parent: ~ +- kind: + MultipleStatementsOnOneLineColon: ~ + location: + row: 29 + column: 9 + end_location: + row: 29 + column: 10 + fix: ~ + parent: ~ +- kind: + MultipleStatementsOnOneLineColon: ~ + location: + row: 31 + column: 3 + end_location: + row: 31 + column: 4 + fix: ~ + parent: ~ +- kind: + MultipleStatementsOnOneLineColon: ~ + location: + row: 32 + column: 17 + end_location: + row: 32 + column: 18 + fix: ~ + parent: ~ +- kind: + MultipleStatementsOnOneLineColon: ~ + location: + row: 33 + column: 7 + end_location: + row: 33 + column: 8 + fix: ~ + parent: ~ +- kind: + MultipleStatementsOnOneLineColon: ~ + location: + row: 35 + column: 7 + end_location: + row: 35 + column: 8 + fix: ~ + parent: ~ +- kind: + MultipleStatementsOnOneLineColon: ~ + location: + row: 37 + column: 8 + end_location: + row: 37 + column: 9 + fix: ~ + parent: ~ +- kind: + MultipleStatementsOnOneLineColon: ~ + location: + row: 39 + column: 14 + end_location: + row: 39 + column: 15 + fix: ~ + parent: ~