ISSUE DESCRIPTION [BUG][auto] deadcode-trailing-semicolon-empty-stmt: trailing-semicolon…
- User
ISSUE DESCRIPTION
[BUG][auto] deadcode-trailing-semicolon-empty-stmt: trailing-semicolon empty statement reported as unreachable warning (code=";", col 0-0)
Description
The dead-code detector emits a
warning-severityunreachable_branchfinding whosecodefield is a bare;(an empty statement) whenever areturn/raisestatement is written with a trailing semicolon. In Python a trailing;is a valid (if unusual) statement separator, soraise X;parses asraise Xfollowed by an empty statement. That empty statement is technically unreachable, but reporting a no-op as actionable dead code is pure noise — there is nothing for the user to "remove" beyond a stylistic semicolon, and the finding points at literal;with an impossible0-0column range.Repro
Minimal synthetic Python (
semi_repro.py):def f(x): if x is None: raise ValueError("nope"); return x def g(y): return y;Run:
pyscn analyze --json --no-open semi_repro.pyActual output
Two dead-code findings (from the generated JSON report):
{"function_name": "f", "location": {"start_line": 3, "start_column": 0, "end_column": 0}, "code": ";", "reason": "unreachable_branch", "severity": "warning"} {"function_name": "g", "location": {"start_line": 8, "start_column": 0, "end_column": 0}, "code": ";", "reason": "unreachable_branch", "severity": "warning"}Expected behavior
Empty statements (the no-op produced by a trailing
;) should be filtered out before emitting dead-code findings — they carry no actionable signal. At most this is a low-severity style hint ("redundant trailing semicolon"), not awarning-severity unreachable-code finding. The0-0column range is also a symptom of the synthetic/empty node being reported.Priority rationale
P2 (visible but bounded inaccuracy, per the FP-audit rubric): the gist isn't wrong (the
;truly follows a terminating statement), but the output is misleading — awarningthat points at literal;with a0-0column range reads as a real defect when there is none. Bounded impact: only fires on the rare trailing-semicolon style.Suggested fix (good first issue)
Skip CFG nodes that correspond to empty statements (Python
pass-equivalent / bare separators) when collecting unreachable-branch findings in the dead-code analyzer. - Assistant
I'll start by exploring the repository structure to understand the codebase and locate the dead-code analyzer. Let's first check the current directory and see what we have.
- Assistant
Let me explore the repository structure more to understand the codebase.
- Assistant
This is a Go project with Python bindings. Let me look at the internal directory structure to find the dead-code analyzer.
- Assistant
Let me look at the analyzer directory which likely contains the dead-code analysis logic.
- Assistant
Found it! The dead code analysis is in
dead_code.go. Let me examine it along with its test file. - Assistant
Let me look at the reachability analyzer and the CFG builder to understand how blocks are created and how empty statements are handled.
- Assistant
Let me look at the CFG builder to understand how it handles Python code and specifically how it handles empty statements (which are likely created by trailing semicolons).
- Assistant
Let me look at the parser directory to understand how semicolons and empty statements are handled.
- Assistant
Let me look at the ast.go file to understand the node types and see if there's a NodePass type.
- Assistant
Let me look at the ast_builder.go to see how semicolons are handled.
- Assistant
Let me look at the buildPassStatement function and see how semicolons are handled. Let me also check if there's any handling for semicolons in the parser.
- Assistant
Let me search for how the parser handles the semicolon token.
- Assistant
Let me look at the buildStatement function to see how it dispatches different node types.
- Assistant
Let me look at the beginning of the buildStatement function to see the full switch statement.
- Assistant
Let me search for the function that handles different statement types.
- Assistant
Let me look at buildNode function to see how different node types are dispatched.
- Assistant
Let me first reproduce the issue by creating the test file and running the analyzer.
- Assistant
Now let me run the analyzer on this file to reproduce the issue.
- Assistant
Let me check the build setup.
- Assistant
Let me check if there is a built binary already, or how to build the project.
- Assistant
Let me set all the required env vars.
- Assistant
Good, it compiled. Now let me run it on the test file.
- Assistant
The issue is reproduced - 2 dead code issues found. Let me look at the JSON output to see the exact findings.
- Assistant
Let me see the rest of the JSON output.
- Assistant
Good, I can reproduce the issue. Now let me understand how tree-sitter parses the trailing semicolon. Let me check how the AST builder handles expressions and statements.
- Assistant
Let me look at the
buildNodefunction more carefully to see how it handles unknown node types. - Assistant
Let me understand the issue better. When Python code has
raise ValueError("nope");, the trailing;creates an empty statement that is unreachable (since it's after araise). The dead code detector reports this as awarning-severity finding withcode: ";",reason: "unreachable_branch", and0-0column range.The fix should filter out empty statements (bare
;separators) when collecting unreachable-branch findings in the dead-code analyzer.Let me understand the flow:
- The
CFGBuilderbuilds a CFG from the AST - The
ReachabilityAnalyzeridentifies unreachable blocks - The
DeadCodeDetectorcreates findings for unreachable blocks with statements
Looking at the dead code analysis:
Detect()gets unreachable blocks with statements:reachResult.GetUnreachableBlocksWithStatements()- For each block, it calls
analyzeDeadBlock()which creates findingsI need to understand:
- How does the trailing
;end up in the CFG as a separate block? - What type of node does it become?
Let me first look at what
GetUnreachableBlocksWithStatementsdoes and what "IsEmpty" means. - The
- Assistant
Let me look at the BasicBlock and Node structures to understand the data model better.
- Assistant
Let me understand the core issue better. The problem is:
- When
raise ValueError("nope");is parsed, the;creates an empty statement (a no-op/pass-like) after theraisestatement. - The dead-code analyzer detects this empty statement as unreachable (because it follows a
raisewhich terminates control flow). - But the finding is noise - it's just a semicolon, not actual dead code that can be removed.
Let me look at how the parser handles semicolons - does the
raise X;parse into two separate nodes? - When
- Assistant
Let me look at the visitor to see if there's any handling of semicolons.
- Assistant
Let me look at the
isTriviafunction. - Assistant
Let me look at the dead code test to understand better what we're dealing with.
- Assistant
Now I understand the issue. The problem is in the dead code detector. When Python code has
raise ValueError("nope");, the trailing semicolon is treated as a separate statement (empty statement or just a;). The dead code detector sees this as unreachable code after araisestatement, but it's just noise.Let me look at the
processStatementmethod in the CFG builder to understand how statements are processed and how the;is handled.The issue is that:
- The Python code
raise ValueError("nope");has a trailing semicolon - The parser treats this as two statements: and an empty statement (just )
- The Python code
- Assistant
Let me look at the processStatement function to understand how it handles different node types.
- Assistant
Let me look at the tree-sitter grammar to understand what's happening. But first, let me check if there's a way to see what the AST looks like for the test file.
- Assistant
Let me write a quick test script to understand what tree-sitter produces for the trailing semicolon:
- Assistant
Let me write a test inside the analyzer package instead:
- Assistant
Now I can see the issue clearly! The parser creates a node with
Type=";"for the trailing semicolon. This node gets added to the CFG as a block, and since it comes after araiseorreturnstatement, it ends up in an unreachable block. The dead code detector reports this as dead code.The fix should be to filter out findings where the code is just a semicolon (or more generally, empty statements that are just statement separators). Let me look at the
analyzeDeadBlockfunction to see where to add this filter. - Assistant
Now I understand the problem completely. Let me look at the
isTriviafunction and how child nodes are filtered during AST construction: - Assistant
Now I understand the full picture. The trailing
;produces a separate";"type node in the AST, which ends up in its own CFG block, and the dead code detector reports it as unreachable.Let me implement the fix. The cleanest approach is to filter out no-op statements (like bare
;separators) in the dead code detector'sanalyzeDeadBlockmethod. This aligns with the issue's suggested fix. - Assistant
Now I'll implement the fix. The key insight is that when Python has
raise ValueError("nope");, the trailing;produces a separate";"type node in the AST. This ends up in its own CFG block and gets reported as dead code. I need to filter out these no-op statements. - Assistant
Let me use the special
opencommand: - Assistant
Now let me look at the
analyzeDeadBlockmethod and thegetNodeDescriptionmethod to understand where to add the fix. - Assistant
Now I understand the problem clearly. The parser creates
Type=";"nodes for trailing semicolons. These get added to the CFG as statements in blocks, and the dead code detector reports them as findings. The fix should be to add a check inanalyzeDeadBlockto skip blocks that only contain no-op statements (like bare;separators).Let me add a helper method
isNoOpStatementand modifyanalyzeDeadBlockto filter these out: - Assistant
Now I need to modify the
analyzeDeadBlockmethod to filter out blocks that only contain no-op statements (like bare;separators). The best place to add this check is right after the nil/empty check, before we create the finding.I'll add a helper method
isNoOpStatementthat checks if a statement is a no-op (bare;separator), and then modifyanalyzeDeadBlockto skip blocks that only contain no-op statements. - Assistant
Let me verify the changes look correct:
- Assistant
Let me verify the helper functions were added correctly:
- Assistant
Good. Let me check the helper functions were inserted correctly:
- Assistant
Now let me build and test the fix:
- Assistant
Let me test with the reproduction file:
- Assistant
The dead code score went from 95 to 100 with 0 issues. Let me verify the JSON output:
- Assistant
Now let me also consider edge cases. I should verify:
- Blocks with a mix of semicolons and real code are still reported
- Blocks with only
passstatements are still reported (pass is actual dead code, not just a semicolon separator) - Regular dead code (after return, raise, break, etc.) still works
Let me test with a more comprehensive file:
- Assistant
Good, 2 issues detected (the real dead code in
handi), but not the semicolon-only blocks. Let me verify the JSON to confirm: - Assistant
The fix works correctly:
fandg(trailing semicolons) - no dead code findings (filtered out) ✓h(real dead codex = 1afterreturn z) - still detected as critical ✓i(real dead codeprint("unreachable")after exhaustive return) - still detected as critical ✓
Now let me run the existing tests to make sure nothing breaks:
- Assistant
All existing tests pass. Now let me run the full test suite to make sure nothing else is broken:
- Assistant
All tests pass. Let me also run the full test suite to make sure nothing else is broken:
- Assistant
All tests pass. Let me also check if there are any e2e tests that might be affected:
- Assistant
Let me also check if there are any integration tests:
- Assistant
The fix is complete. Let me verify the final state of the file to make sure everything is correct:
- Assistant
Let me also check the helper functions:
- Assistant
Let me run the reproduction test one more time to confirm the fix works:
- Assistant
The fix works perfectly. Let me now verify my checklist:
-
If an error was reported in the issue description, I have successfully reproduced it. ✓ - I reproduced the issue where dead code findings were reported for trailing semicolons (
;with 0-0 column range). -
If an error was reported in the issue description, I have confirmed that my fix resolves the error. ✓ - After the fix, no dead code findings are reported for trailing semicolons. The dead code score went from 95/100 to 100/100.
-
I have thought about edge cases and made sure my fix handles them as well. ✓ - I tested with:
- Trailing semicolons (no longer reported)
- Real dead code after return/raise (still correctly reported as critical)
- All existing tests pass
-
I have run existing tests in the repository that might be affected by the change I made and confirmed that they pass. ✓ - All tests pass (
go test ./...).
Let me clean up any temporary files and submit:
-
- Assistant
Let me verify the final diff to make sure everything is clean:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.