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 list files.
- Assistant
Let me explore the repository structure more to understand the codebase.
- Assistant
This appears to be a Go project with Python bindings. Let me look at the internal directory structure since that's likely where the deadcode analysis lives.
- Assistant
Let me search for where deadcode is implemented.
- Assistant
Let me look at the dead code analyzer and the reachability module, as those are likely where the issue lies.
- Assistant
Let me look at the reachability analyzer to understand how unreachable blocks are detected.
- Assistant
Now let me look at the CFG builder to understand how blocks are created and how empty statements might be handled:
- Assistant
Let me search for how the CFG builder processes statements and look for how it handles empty statements or semicolons.
- Assistant
Let me look at the processStatement function more closely, especially how it handles NodeExpr (which might be where empty statements from semicolons are processed).
- Assistant
Let me look at the full processStatement function to understand how statements are handled.
- Assistant
Let me check the parser to understand what node types are used for empty statements.
- Assistant
Let me check the parser to see what node types are defined.
- Assistant
Let me check the parser AST to understand node types.
- Assistant
Let me first reproduce the issue as described. First, let me create the reproduction script:
- Assistant
Let me check if pyscn binary is available and try to reproduce the issue:
- Assistant
I need to set up Go properly.
- Assistant
Let me set up the proper Go environment.
- Assistant
Good, now let me try to reproduce the issue:
- Assistant
I see the output mentions "Dead Code: 95/100 (2 issues, 0 critical)". Let me look at the JSON report to see the actual findings.
- Assistant
Good, I've reproduced the issue. Now let me understand how the parser handles trailing semicolons. Let me look at the parser to see what node types are created for semicolons:
- Assistant
I see there's a
NodePassnode type. Let me check what happens when we have a trailing semicolon - is it parsed as aNodeExprwith no value, or something else? Let me look at the parser to understand how it handles semicolons: - Assistant
Let me look at the ast_builder to see how it handles the
Exprnode type. Let me also check if there's a tree-sitter grammar being used: - Assistant
Let me look at the ast_builder to understand how Expr nodes are handled, and look for how empty statements (like a bare
;) are handled: - Assistant
Now let me look at the
buildExpressionStatementfunction to understand how empty statements (bare;) are handled: - Assistant
I see! When there's a trailing semicolon, the tree-sitter parser creates an
expression_statementnode, but it has no real content (just the;). ThebuildExpressionStatementfunction returns aNodeExprwith no value (because there are no non-trivia children).Let me trace the issue more carefully:
- Assistant
Let me look at how the CFG builder handles NodeExpr (line 384 we saw earlier) and understand what happens with empty expression statements:
- Assistant
Let me write a quick test to understand what the parser produces for a trailing semicolon:
- Assistant
Let me check the Node structure to understand what empty expressions look like:
- Assistant
Let me look at how the CFG builder handles
raisestatements to understand the flow: - Assistant
Let me look at the processRaiseStatement function:
- Assistant
Let me check how the block code is extracted from the dead code detector:
- Assistant
Let me write a quick test to see what the parser produces for a trailing semicolon. First, let me check if there are any existing tests:
- Assistant
Let me understand the problem better. The issue is that when Python has a trailing semicolon (
raise ValueError("nope");), the parser creates an expression statement node for the;part. This is an empty/no-op statement that comes after a terminating statement (raise/return). The dead code detector then reports this empty statement as unreachable dead code.The fix should be to filter out empty statements (that are just
;orpass-like) from the dead code findings.Let me look at the
analyzeDeadBlockmethod to see where we can add this filtering: - Assistant
Let me look at the
isTriviafunction and understand how semicolons are handled: - Assistant
Now I understand the problem. The issue is in
isTrivia- semicolons are NOT treated as trivia, so they might end up in the AST as an empty NodeExpr. But wait, the report showscode: ";"which is the actual source code representation, not just "expression". Let me check if there's something else going on.Let me look at how the formatter or service layer gets the code for the dead code findings:
- Assistant
Let me look at the dead_code_formatter to understand how it formats the output:
- Assistant
Let me write a simple Go program to see what tree-sitter produces for the semicolon:
- Assistant
Let me fix the test program.
- Assistant
Let me check the tree-sitter Go library API.
- Assistant
Let me look at how the parser is called in the existing code.
- Assistant
Let me fix the test code:
- Assistant
Now I can see the tree structure. The semicolon is a child of the
if_statementblock (not theraise_statement). It's a separate child of the block, after theraise_statement. Let me check how the AST builder handles this.In
buildExpressionStatement, it iterates through children of the expression_statement node, skipping trivia. But the semicolon is a child of theblocknode, not anexpression_statement. Theblocknode has two children: theraise_statementand the;token.When the CFG builder processes the block's children, it calls
processStatementfor each child. For the semicolon token, it's not a named node, so it might be treated as a statement somehow.Let me check how the AST builder handles the semicolon in a block:
- Assistant
Let me look at how the
buildBlockfunction handles the semicolon: - Assistant
Let me look at what happens when
buildNodeprocesses a semicolon: - Assistant
I see. When
buildNodeencounters a semicolon token (;), it doesn't match any specific case, so it falls through to the default case. The default case creates a generic node with type;. This generic node gets added to the block's body. - Assistant
Let me fix the test file and run it again.
- Assistant
Now I see exactly what's happening. The tree-sitter parser creates
raise ValueError("nope");as a block with two children:raise_statement(named=true);(named=false)
The
;token is added to the block's body throughbuildNode's default case. Then when the CFG builder processes this block, it first processes theraise_statementand creates an unreachable block, then processes the;token which goes into that unreachable block.Let me look at the full flow to understand how to fix this:
- Assistant
Let me now understand the problem more deeply. Let me check what the semicolon node looks like after AST building:
- Assistant
Let me check how the buildBlock function handles the
;node in the AST builder. The issue is that;is not considered trivia, so it gets added to the AST body as a separate statement. - Assistant
Let me understand the full flow. The
;(semicolon) node is created bybuildNode's default case and added to the block body. The fix should be to handle this properly. Let me look at how to best fix this:Option 1: Treat
;as trivia inisTrivia(at the AST level) Option 2: Filter out empty/no-op statements in the dead code detector (at the analysis level) Option 3: Skip;nodes in the CFG builder (at the CFG level)Let me first check if there's an existing empty/no-op check in the CFG builder or dead code detector:
- Assistant
Let me look at the
analyzeDeadBlockfunction more closely and understand how to fix this. The issue is that the;token creates an empty statement that gets added to the unreachable block. The fix should filter out such empty/no-op statements from dead code findings. - Assistant
Let me now create a test to understand exactly what the AST and CFG look like for the trailing semicolon case:
- Assistant
Let me check the existing tests for dead code to understand the pattern better:
- Assistant
Let me look at the existing dead code tests:
- Assistant
Now I have a clear understanding of the issue. Let me trace the flow:
- Tree-sitter parses
raise ValueError("nope");→ creates a block with children:raise_statementand;(non-named) buildBlockiterates over all non-trivia children (sinceisTriviaonly checks for "comment" and "line_continuation")buildNodeis called for;→ falls to default case → creates a Node with Type";"- CFG builder processes the raise statement, creates an unreachable block, then adds the
;to that unreachable block - Dead code detector reports this unreachable block with
code: ";"
The fix should be in the dead code detector to filter out empty/no-op statements. Let me implement the fix:
- Tree-sitter parses
- Assistant
Now let me look at the
processStatementdefault case to understand how the;node is handled: - Assistant
Now I understand the full flow. The
;token is not treated as trivia, so it gets added to the AST body. Then when the CFG builder processes the block, theraisestatement creates an unreachable block, and the;statement ends up in that unreachable block. The dead code detector then reports this unreachable block.The fix should be to filter out empty/no-op statements from the dead code findings. Let me implement this fix:
- Assistant
Let me understand the full flow. The
;token creates a node with Type=";" that gets added to the unreachable block. I need to fix this in the dead code detector. Let me add a helper function to check if statements are empty/no-ops: - Assistant
Now I understand the problem completely. The
;token gets added to the AST as a node with Type=";", then ends up in an unreachable block after theraisestatement. The dead code detector reports this unreachable block.The fix should be to filter out empty/no-op statements from dead code findings. Let me implement the fix in the dead code detector:
- Assistant
Now I understand the full picture. The
;token creates a node with Type=";" that gets added to the block body. When the CFG builder processes the block, the statement creates an unreachable block, and the statement ends up in that unreachable block. The dead code detector then reports this unreachable block. - Assistant
The fix should be to filter out blocks that contain only empty/no-op statements in
analyzeDeadBlock. Let me implement this: - Assistant
Now I have a clear picture. The fix should be to add filtering in the dead code analyzer to skip blocks that contain only empty/no-op statements. Let me implement this:
- Assistant
Let me open the file and navigate to the relevant section.
- Assistant
Now I understand the full flow. The
;token is not treated as trivia, so it gets added to the AST body. When the CFG builder processes the block, theraisestatement creates an unreachable block, and the;statement ends up in that unreachable block. The dead code detector then reports this unreachable block.The fix should be to filter out empty/no-op statements from dead code findings. I have two options:
Option 1: Fix at the dead code detector level - Add a check in
analyzeDeadBlockto skip blocks that contain only empty/no-op statementsOption 2: Fix at the AST builder level - Treat
;as trivia so it doesn't get added to the ASTThe issue description suggests: "Skip CFG nodes that correspond to empty statements (Python
pass-equivalent / bare;separators) when collecting unreachable-branch findings in the dead-code analyzer."So the preferred fix is at the dead code detector level. Let me implement that.
The fix should:
- Add a helper function
isNoOpStatementthat checks if a statement is a no-op (like bare;or empty expression) - In
analyzeDeadBlock, check if all statements in the block are no-ops, and skip the block if so
- Add a helper function
- Assistant
Good, now I need to add the
blockContainsOnlyNoOpshelper function. Let me add it: - Assistant
Now let me add the
blockContainsOnlyNoOpsfunction. I'll add it after thegetBlockContextfunction: - Assistant
Good, now I need to also make sure the
DeadBlockscount is updated correctly. Let me look at theDetectfunction to see howDeadBlocksis counted: - Assistant
I see the issue. The
DeadBlockscount is set on line 136 based on the number of unreachable blocks with statements. But if we filter out no-op blocks, theDeadBlockscount should also be updated.Let me update the
Detectfunction to account for the no-op filtering: - Assistant
Good. Now let me verify the code compiles and test it against the reproduction case:
- Assistant
Good, the code compiles. Now let me run the existing tests to make sure I haven't broken anything:
- Assistant
All existing dead code tests pass. Now let me rebuild the pyscn binary and test against the reproduction case:
- Assistant
Good, now let me test against the reproduction case:
- Assistant
The fix works! The dead code score is now 100/100 with 0 issues, compared to the previous 95/100 with 2 issues. The trailing semicolons are no longer being reported as dead code.
Let me verify the JSON output to make sure there are no dead code findings:
- Assistant
The fix works! The dead code findings are now 0, and the dead code score is 100/100. The trailing semicolons are no longer being reported as dead code.
Let me now make sure I haven't broken any existing tests in the repository. Let me run all the tests:
- Assistant
All tests pass. Let me also run the service-level tests that might be affected:
- Assistant
All tests pass. Let me also run the integration and app tests to make sure everything is working correctly:
- Assistant
Let me also run the integration tests:
- Assistant
Let me run all integration tests to make sure I haven't broken anything:
- Assistant
All tests pass. Now let me also test with a more complex edge case - a file that has both real dead code and trailing semicolons to make sure the fix doesn't accidentally filter out real dead code:
- Assistant
Let me check the available commands:
- Assistant
Let me run the analyze command instead:
- Assistant
Good, the dead code score is 95/100 with 1 issue (the real dead code in function
h). Let me verify the JSON output to make sure only the real dead code is reported: - Assistant
The fix works correctly:
- ✅ The trailing semicolons (
;) in functionsf,g, andkare no longer reported as dead code findings. - ✅ The real dead code (
x = 1afterreturn zin functionh) is still correctly detected with severity "critical" and reason "unreachable_after_return".
Let me now run the full test suite to make sure nothing is broken:
- ✅ The trailing semicolons (
- Assistant
All tests pass! The fix is complete and working correctly. Let me now review the changes I made:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.