fix(deadcode): false positive on early return + multi-line dict return (closes #105)#138
Merged
Conversation
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.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Closes #105
Root cause
When
_detect_unreachable_codeencountered a multi-linereturn {(open brace > close brace on the start line), it skipped the line viacontinuewithout resettingfound_terminal. If a previousif x: return Nonehad 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.pybefore this fix):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_terminalbefore continuing.Also reverts the antipattern
if x: return None / else: return {...}introduced in PR #96 across 8 functions inscripts/commands/analyze.pyback to the PEP 8-friendlyif 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— simpleif x: return None / return ytest_issue_105_multiline_return_after_early_return— multi-line dict return after early return (exact repro from issue)test_issue_105_chained_early_returns— multipleifguards + final returntest_issue_105_nested_if_early_return— nested if/return + outer returnstest_issue_105_genuinely_unreachable_still_detected— sanity check that genuinely unreachable code is still reportedAll 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).