Skip to content

Remove a useless pass separated from the next statement - #363

Open
Eljees wants to merge 1 commit into
PyCQA:mainfrom
Eljees:fix/65-pass-separated-by-blank-or-comment
Open

Remove a useless pass separated from the next statement#363
Eljees wants to merge 1 commit into
PyCQA:mainfrom
Eljees:fix/65-pass-separated-by-blank-or-comment

Conversation

@Eljees

@Eljees Eljees commented Aug 14, 2026

Copy link
Copy Markdown

Fixes #65.

useless_pass_line_numbers recognises a leading pass only when the next real token sits on the row immediately after it:

start_row - 1 == last_pass_row

A blank line or a comment shifts that row, so the pass is never marked and survives:

def foo():
    pass

    a = 1

The pending pass is now carried across non-ATOM tokens (NL, COMMENT, INDENT, DEDENT) and resolved on the first ATOM token. The pending state is cleared on any ATOM rather than only a matching one — otherwise an atom at the same indentation in a later, unrelated block would be taken for the continuation of this one.

Measured

python -m pytest (excluding test_fuzz.py, which needs a corpus): 174 passed before and after.

source before after
pass, blank line, statement not removed removed
pass, comment, statement not removed removed
pass immediately before a statement removed removed
pass as the only statement in a function kept kept

The comment case is what shows this is line arithmetic rather than anything specific to blank lines.

Two things worth saying plainly:

  • The blank line left behind where the pass was is not removed. autoflake doesn't manage blank lines and this change doesn't start; the issue text reads as if it should, so I'd rather name the difference than let it be discovered in review.
  • On the already-working adjacent case the old code yielded the same line number twice ([2, 2]); it now yields it once. Nothing visible changes, since the numbers feed a membership test.

This touches useless_pass_line_numbers only. #349 rewrites filter_useless_pass further down the same file; the hunks don't meet.

AI-assisted (LLM used for drafting); the runs above are mine.

useless_pass_line_numbers recognises a leading pass only when the next real
token sits on the row immediately after it. A blank line or a comment shifts
that row, so the pass is never marked and survives.

The pending pass is now carried across non-ATOM tokens and resolved on the
first ATOM token, with the pending state cleared on any ATOM rather than only
a matching one: otherwise an atom at the same indentation in a later,
unrelated block would be taken for the continuation of this one.

Fixes PyCQA#65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unused pass not being removed in certain cases

1 participant