Skip to content

Commit c582e05

Browse files
fix: correct escape handling and string closure check in _line_self_contained by reviewer-B
1 parent d874785 commit c582e05

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

src/deadcode/cli.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,16 @@ def _line_self_contained(text: str) -> bool:
3939
"""
4040
depth = 0
4141
in_str: str | None = None
42-
prev = ""
42+
escaped = False
4343
for ch in text:
44+
if escaped:
45+
escaped = False
46+
continue
47+
if ch == "\\":
48+
escaped = True
49+
continue
4450
if in_str is not None:
45-
if ch == in_str and prev != "\\":
51+
if ch == in_str:
4652
in_str = None
4753
elif ch in ("'", '"', "`"):
4854
in_str = ch
@@ -52,8 +58,7 @@ def _line_self_contained(text: str) -> bool:
5258
depth -= 1
5359
if depth < 0: # closes something opened on an earlier line
5460
return False
55-
prev = ch
56-
return depth == 0
61+
return depth == 0 and in_str is None
5762

5863

5964
@click.group()

0 commit comments

Comments
 (0)