fix(cleanup): remove unreachable code blocks (closes #93)#96
Merged
Conversation
|
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.



Problem
CodeLens' own dead-code scanner reported 18 unreachable code blocks across 8 production scripts. These were all
return {...}multi-line dict literals placed after an earlyreturn None(or similar terminal statement), which the scanner flagged as unreachable. Closes #93.Root cause analysis
Of the 18 reported blocks, none were genuine unreachable code — they were all false positives from the dead-code detector (
scripts/deadcode_engine.py). The detector has two known limitations that combined to produce these false positives:returnat the start of a line inside a docstring is treated as a realreturnstatement. (1 case:ast_taint_engine.py.)return { ... }dict literals are skipped viacontinuewhenopen_count > close_count, butfound_terminalfrom a previous line is NOT reset. So any non-returnline that follows (e.g."status": "ok",) gets flagged as unreachable. (17 cases.)The detector's
continueon multi-line returns also skips the indent-based reset check that would normally clearfound_terminalwhen control flow exits the previous block. This is a detector bug, but fixing the detector is out of scope for this issue (it would change other test expectations and is a separate concern).Approach
Chosen: Restructure each false-positive site so the detector no longer flags it, while preserving 100% of the original runtime behaviour. No code was deleted as "dead" — every change is a structural rewrite that produces identical control flow.
This was preferred over:
deadcode_engine.pyand risk changing other test expectations.Per-file changes
scripts/commands/analyze.pyreturn {...}in anelse:block afterif ...: return None. Theelsekeyword resetsfound_terminalin the detector, so the multi-line return is no longer flagged. Behaviour identical.scripts/autofix_engine.pyreturn { ... }dict into a single-linereturn { ... }soopen_count == close_counton the same line and the detector does not skip it.scripts/ast_taint_engine.pyreturn(changed "Returns a" → "Yields a" and "return expressions" → "return-expressions").scripts/callgraph_engine.pyif not isinstance(...): return False+return (...)into a single multi-linereturn isinstance(...) and ....scripts/circular_engine.pyreturn ... or \ any(...)in parens so it is a single multi-linereturn (expression (which the detector correctly skips viaopen > close).scripts/dashboard_engine.pytry/except IOError: return {...}early-return into a multi-linereturn { ... }so the terminal flag is not set on line 76. (2) Assigned the multi-line f-stringreturn f"""..."""to a localhtmlvariable first, thenreturn html.scripts/mcp_server.pyerror_responsefirst, thenreturn error_response.scripts/plugin_system.pyPluginRule(...)constructor call torulefirst, thenreturn rule.Blocks deleted vs moved
Verification
Unreachable count for the 8 target files: 0 (was 18).
No regressions. (87 skips are pre-existing platform-optional tests unrelated to this change.)
Out-of-scope notes (for follow-up)
The dead-code detector itself has two bugs that produced these false positives:
return-like tokens inside Python docstrings / triple-quoted strings.return {viacontinue, it fails to run the indent-based reset that would normally clearfound_terminalfor the parent block.Two additional unreachable reports exist outside the 8 target files of this issue and are left untouched here:
tests/test_deadcode_engine.py:128(test fixture, intentionally unreachable to test the detector)scripts/commands/history.py:77(not in the issue's file list)Fixing the detector itself should be tracked as a separate issue.