Skip to content

fix(deadcode): false positive on early return + multi-line dict return (closes #105)#138

Merged
Wolfvin merged 1 commit into
mainfrom
fix/issue-105-deadcode-false-positive-early-return
Jul 3, 2026
Merged

fix(deadcode): false positive on early return + multi-line dict return (closes #105)#138
Wolfvin merged 1 commit into
mainfrom
fix/issue-105-deadcode-false-positive-early-return

Conversation

@Wolfvin

@Wolfvin Wolfvin commented Jul 1, 2026

Copy link
Copy Markdown
Owner

Closes #105

Root cause

When _detect_unreachable_code encountered a multi-line return { (open brace > close brace on the start line), it skipped the line via continue without resetting found_terminal. If a previous if x: return None had set the terminal flag, the multi-line return body (e.g. dict literals) was falsely flagged as unreachable code.

Example false positive (the exact pattern from scripts/commands/analyze.py before this fix):

def _detect_vulns(workspace, max_items):
    ...
    if total == 0:
        return None
    return {                       # ← multiline return start, was skipped
        "category": "vulnerabilities",  # ← falsely flagged as unreachable
        "total": total,
    }

Fix

In the multi-line return skip path (Python branch), check the current line's indent vs the previous terminal's indent. If the current return is in an outer scope (lower indent), the previous terminal is no longer relevant — reset found_terminal before continuing.

Also reverts the antipattern if x: return None / else: return {...} introduced in PR #96 across 8 functions in scripts/commands/analyze.py back to the PEP 8-friendly if x: return None / return {...} form. The antipattern was only there to satisfy the buggy scanner; with the fix the PEP 8 form scans cleanly.

Tests

5 new regression tests in tests/test_deadcode_engine.py:

  • test_issue_105_early_return_then_final_return — simple if x: return None / return y
  • test_issue_105_multiline_return_after_early_return — multi-line dict return after early return (exact repro from issue)
  • test_issue_105_chained_early_returns — multiple if guards + final return
  • test_issue_105_nested_if_early_return — nested if/return + outer returns
  • test_issue_105_genuinely_unreachable_still_detected — sanity check that genuinely unreachable code is still reported

All 14 deadcode tests pass (9 existing + 5 new). No regressions in the broader test suite — verified zero false positives when scanning 8 major CodeLens Python files (analyze.py, deadcode_engine.py, codelens.py, scan.py, query.py, secrets_engine.py, ast_taint_engine.py, vulnscan_engine.py).

closes #105)

Root cause: when a multi-line  (open brace > close brace on the
start line) was encountered, the scanner skipped the line via
WITHOUT resetting . If a previous  had
set the terminal flag, the multi-line return body (e.g. dict literals)
was falsely flagged as unreachable code.

Example false positive (the exact pattern from analyze.py before this fix):

    def _detect_vulns(workspace, max_items):
        ...
        if total == 0:
            return None
        return {                       # <- multiline start, was skipped
            "category": "vulns",        # <- falsely flagged as unreachable
            "total": total,
        }

Fix: in the multi-line return skip path (Python), check the current line's
indent vs the previous terminal's indent. If the current return is in an
outer scope (lower indent), the previous terminal is no longer relevant —
reset found_terminal before continuing.

Also reverts the antipattern
introduced in PR #96 across 8 functions in scripts/commands/analyze.py
back to the PEP 8-friendly  form. The
antipattern was only there to satisfy the buggy scanner; with the fix the
PEP 8 form scans cleanly.

Tests: 5 new regression tests in tests/test_deadcode_engine.py covering
simple/chained/nested early returns + multi-line dict return + a sanity
check that genuinely unreachable code is still detected. All 14 deadcode
tests pass.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@sonarqubecloud

sonarqubecloud Bot commented Jul 1, 2026

Copy link
Copy Markdown

@Wolfvin Wolfvin merged commit d262d83 into main Jul 3, 2026
5 of 11 checks passed
@Wolfvin Wolfvin deleted the fix/issue-105-deadcode-false-positive-early-return branch July 3, 2026 02:15
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.

[BUG] dead-code scanner false positive — reports reachable code after early return as unreachable

1 participant