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):pythondef f(x): if x is None: raise ValueError("nope"); return x def g(y): return y;Run:
bashpyscn analyze --json --no-open semi_repro.pyActual output
Two dead-code findings (from the generated JSON report):
json{"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 — a that points at literal with a column range reads as a real defect when there is none. Bounded impact: only fires on the rare trailing-semicolon style.
Assistant
Let me analyze this issue step by step:
- The bug is about the dead-code detector reporting empty statements (trailing semicolons) as unreachable code warnings
- When Python code has
raise X;orreturn y;, the trailing;creates an empty statement that is technically unreachable - The fix should skip CFG nodes that correspond to empty statements when collecting unreachable-branch findings
Let me start by exploring the repository structure to understand how it's organized and find the relevant dead-code analyzer code.
Assistant
This is a Go-based project with Python bindings. Let me explore the structure more, particularly looking for the dead-code analyzer.
Assistant
Good, I found the dead code related files. Let me look at the main analyzer first.
Assistant
Now let me look at the reachability analyzer to understand how unreachable blocks are detected:
Assistant
Now let me look at the parser to understand how nodes are represented, especially the empty statement node:
Assistant
Now let me look at the BasicBlock structure to understand how IsEmpty() works:
Assistant
I need to see the IsEmpty function and how statements are represented. Let me look at the cfg.go file to understand the BasicBlock structure.
Assistant
Now I understand the structure. The
IsEmpty()function only checks if there are statements, but it doesn't check if those statements are empty statements. Let me first reproduce the bug by creating the test file:Assistant
Let me build the Go binary first:
Assistant
I need to set the GOCACHE environment variable as well.
Assistant
I confirmed the issue exists - it reports 2 dead code issues. Let me look at the JSON report to see the details:
Assistant
I successfully reproduced the issue. The JSON output confirms two findings with
"code": ";"and column range0-0.Now let me understand how the CFG builder handles empty statements:
Assistant
Let me see how the
processStatementmethod handles statements and if it creates blocks for empty statements:Assistant
Now let me search for how the trailing semicolon (empty statement) might be parsed:
Assistant
Let me check how the parser handles the Python AST and what node type an empty statement would be:
Assistant
Let me look at how the AST builder processes Python statements. When Python parses
raise X;, the trailing;results in an empty statement. Let me see how tree-sitter parses this:Assistant
I found the
expression_statementcase on line 108. Let me see what happens when an empty statement (from trailing semicolon) is parsed. I need to look at that case and see if it creates a pass-like node or something else.Assistant
Let me look at the
buildExpressionStatementfunction:Assistant
Now I understand! When there's an empty statement (trailing
;), thebuildExpressionStatementfunction finds no non-trivia children, so it creates aNodeExprnode with an empty location (0-0). This is what gets flagged as dead code.The fix should be in the dead code analyzer. There are a few places where I can implement this fix:
- In
analyzeDeadBlock- Skip blocks that only contain empty statements (emptyNodeExprwith 0-0 location) - In
GetUnreachableBlocksWithStatements- Filter out blocks with only empty statements - In
IsEmpty- Also consider blocks with only empty statements as empty
Let me look at what makes the most sense. The issue says to "Skip CFG nodes that correspond to empty statements." The most appropriate place seems to be in the
analyzeDeadBlockfunction indead_code.goor in theGetUnreachableBlocksWithStatementsfunction.Let me check if there's a way to identify an empty statement node:
- In
Assistant
Looking at the issue and code, I think the best approach is to:
- Add a helper function to check if a node is an empty statement (a
NodeExprwith 0-0 location) - Modify
GetUnreachableBlocksWithStatementsto filter out blocks that only contain empty statements
OR
- Modify the
IsEmptymethod to also consider blocks with only empty statements as "empty"
Let me check how
IsEmptyis used and what the best place is for this fix:- Add a helper function to check if a node is an empty statement (a
Assistant
I see
IsEmptyis used in:GetUnreachableBlocksWithStatements(reachability.go:144)HasUnreachableCode(reachability.go:163)
Both of these are relevant to filtering out dead code findings. The best approach would be to enhance
IsEmpty()to also consider blocks that only contain empty statements (like those created by trailing semicolons) as effectively empty.Let me first create a helper to check if a node is an empty statement:
Assistant
Now let me check what properties the empty statement node has. I'll write a quick debug script to understand how the parser represents these nodes:
Assistant
Now I can see the problem clearly:
- The trailing semicolon creates a node with
Type=;(literal semicolon character) - It's placed in an unreachable block (bb5 for function f, bb3 for function g)
- The block has 1 statement so
IsEmpty()returns false, but the statement is essentially an empty/no-op statement
The node type is
";"(the literal semicolon character as a NodeType string). This is the empty statement created by the trailing semicolon.Now I need to figure out where this node type is defined. Let me search for it:
- The trailing semicolon creates a node with
Assistant
Let me check how the semicolon is being parsed. It might be a default case or something else:
Assistant
Let me look at the default case of the switch statement:
Assistant
Now I understand! The semicolon is a tree-sitter node type
";"that falls through to the default case. It creates aNodewithType = ";"(NodeType) and no children.Now I need to decide on the best fix. The suggested fix says: "Skip CFG nodes that correspond to empty statements (Python
pass-equivalent / bare;separators) when collecting unreachable-branch findings in the dead-code analyzer."I have a few options:
- Add a constant
NodeEmptyStmtfor the semicolon type and check for it in the dead code analyzer - Modify
IsEmpty()on BasicBlock to also consider blocks with only empty statements - Add a filter in
GetUnreachableBlocksWithStatementsto exclude blocks with only empty statements
Looking at the code flow:
Detect()callsGetUnreachableBlocksWithStatements()which filters byblock.IsEmpty()- Then
analyzeDeadBlock()creates findings for each unreachable block
The cleanest fix would be to:
- Add a helper function to identify empty statement nodes (node type is
";")
- Add a constant
Assistant
Let me try the correct syntax:
Assistant
Now I'll modify the
IsEmptyfunction incfg.goto also consider blocks with only empty statements as effectively empty. First, let me add a helper function to check if a node is an empty statement:Assistant
I need to use the edit command with the correct syntax. Let me look at the exact format needed.
Assistant
Now let me verify the fix by rebuilding and running the test:
Assistant
The fix works. Now the dead code score is 100/100 with 0 issues. Let me verify the JSON output to confirm:
Assistant
Excellent! The fix is confirmed:
total_findings: 0- no more false positives for trailing semicolonsdead_blocks: 0- empty statement blocks are no longer counted
Now let me run the existing tests to make sure I haven't broken anything:
Assistant
The IsEmpty test passes. Let me run all the tests in the analyzer package:
Assistant
All analyzer tests pass. Let me also run the dead code specific tests:
Assistant
All tests pass. Now let me think about edge cases and test them:
- Multiple trailing semicolons:
return x;;- should still be filtered - Empty statement mixed with real code: Not likely from trailing semicolons, but should still detect real dead code
- Semicolon at module level: Should also be filtered
- Semicolon after break/continue: Should also be filtered
Let me test these edge cases:
- Multiple trailing semicolons:
Assistant
The file is being excluded. Let me check the exclude patterns:
Assistant
The edge_test.py is being excluded by
*_test.pypattern. Let me rename it:Assistant
The file has syntax errors (the
;;double semicolon is not valid Python). Let me fix the test file:Assistant
Hmm, the dead code detection shows 0 issues, but I expected it to detect the real dead code in
real_dead_code. Let me check:Assistant
Wait, that's strange - the real dead code in
real_dead_codeshould have been detected. Let me investigate why it's not being detected. Let me check the original behavior by reverting my change temporarily:Assistant
Let me test if the real dead code was being detected before my change. Let me revert and test:
Assistant
Before my fix, it detected 4 issues (1 critical). Let me check what those 4 issues are:
Assistant
Bash is interpreting the
!r}as a history expansion. Let me use a different approach with a Python script file.Assistant
Interesting! Before my fix:
- 4 issues were detected - but they're ALL semicolons (
;), not the real dead code inreal_dead_code - The real dead code (
print("this is dead")) was NOT detected even before my change
So my fix is correct - it removes all the false positive semicolon warnings. The real dead code not being detected seems to be a different issue (perhaps the function is structured differently or the dead code detector doesn't handle this pattern).
Let me restore my changes and verify again:
- 4 issues were detected - but they're ALL semicolons (
Assistant
My fix works correctly - no more false positive warnings for trailing semicolons.
Let me also verify the original repro case:
Assistant
The fix works. Now let me run all tests to make sure nothing is broken:
Assistant
All tests pass. Let me clean up the temporary files I created:
Assistant
Now let me verify my final change:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.