From 88b984e8856b45a4b15aadc776440220c4a623cb Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Tue, 1 Aug 2023 00:20:29 -0400 Subject: [PATCH] Avoid detecting continuations at non-start-of-line (#6219) ## Summary Previously, given: ```python a = \ 5; ``` When detecting continuations starting at the offset of the `;`, we'd flag the previous line as a continuation. We should only flag a continuation if there isn't leading content prior to the offset. Closes https://github.com/astral-sh/ruff/issues/6214 --- .../test/fixtures/pycodestyle/E70.py | 3 +++ ...ules__pycodestyle__tests__E703_E70.py.snap | 21 +++++++++++++++++++ crates/ruff_python_index/src/indexer.rs | 14 +++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/ruff/resources/test/fixtures/pycodestyle/E70.py b/crates/ruff/resources/test/fixtures/pycodestyle/E70.py index 9b9fde824c..861a99cb9d 100644 --- a/crates/ruff/resources/test/fixtures/pycodestyle/E70.py +++ b/crates/ruff/resources/test/fixtures/pycodestyle/E70.py @@ -66,3 +66,6 @@ while 1: #: E703:2:1 0\ ; +#: E701:2:3 +a = \ + 5; diff --git a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E703_E70.py.snap b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E703_E70.py.snap index a612503351..f3bc1f206a 100644 --- a/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E703_E70.py.snap +++ b/crates/ruff/src/rules/pycodestyle/snapshots/ruff__rules__pycodestyle__tests__E703_E70.py.snap @@ -70,6 +70,8 @@ E70.py:68:1: E703 [*] Statement ends with an unnecessary semicolon 67 | 0\ 68 | ; | ^ E703 +69 | #: E701:2:3 +70 | a = \ | = help: Remove unnecessary semicolon @@ -80,5 +82,24 @@ E70.py:68:1: E703 [*] Statement ends with an unnecessary semicolon 67 |-0\ 68 |-; 67 |+0 +69 68 | #: E701:2:3 +70 69 | a = \ +71 70 | 5; + +E70.py:71:4: E703 [*] Statement ends with an unnecessary semicolon + | +69 | #: E701:2:3 +70 | a = \ +71 | 5; + | ^ E703 + | + = help: Remove unnecessary semicolon + +ℹ Fix +68 68 | ; +69 69 | #: E701:2:3 +70 70 | a = \ +71 |- 5; + 71 |+ 5 diff --git a/crates/ruff_python_index/src/indexer.rs b/crates/ruff_python_index/src/indexer.rs index 2bbb6de303..222e58fca0 100644 --- a/crates/ruff_python_index/src/indexer.rs +++ b/crates/ruff_python_index/src/indexer.rs @@ -228,8 +228,18 @@ impl Indexer { offset: TextSize, locator: &Locator, ) -> Option { - // Find the first preceding continuation. - let mut continuation = self.find_continuation(locator.line_start(offset), locator)?; + // Find the first preceding continuation. If the offset isn't the first non-whitespace + // character on the line, then we can't have a continuation. + let previous_line_end = locator.line_start(offset); + if !locator + .slice(TextRange::new(previous_line_end, offset)) + .chars() + .all(is_python_whitespace) + { + return None; + } + + let mut continuation = self.find_continuation(previous_line_end, locator)?; // Continue searching for continuations, in the unlikely event that we have multiple // continuations in a row.