mirror of
https://github.com/ludo-technologies/pyscn.git
synced 2025-10-06 00:59:45 +03:00
Fixes #117 ## Problem Dead code detection failed to identify unreachable code after exhaustive if-elif-else chains where all branches terminate with return/raise/break/ continue statements. ## Root Cause 1. Parser wasn't properly handling elif_clause and else_clause nodes from tree-sitter, causing else clauses to be lost in elif chains 2. CFG builder always created fall-through edges to merge blocks even when all conditional branches terminated ## Solution ### Parser Fixes (ast_builder.go) - Added buildElifClause() to properly parse elif nodes with Test/Body/Orelse - Added buildElseClause() to parse else nodes with Body - Modified buildIfStatement() to collect all alternative branches and chain them correctly via attachElseToElifChain() - Tree-sitter returns both elif_clause and else_clause as "alternative" field siblings, now both are properly collected and linked ### CFG Builder Enhancements (cfg_builder.go) - Added blockTerminates() to check if block ends with terminating statements - Added allBranchesTerminate() to check if all conditional branches terminate - Modified processIfStatement() to detect exhaustive termination and create unreachable blocks instead of connecting to merge - Modified processIfStatementElif() with same termination detection - Updated convertElifClauseToIf() to use parser-populated fields - Both functions now handle else_clause nodes by extracting their Body ### Test Updates (dead_code_test.go) - Updated UnreachableElif: expectedDead 0 → 1 - Added ExhaustiveIfElseReturn test case - Added NestedExhaustiveReturn test case - Added MixedTerminators test case (return/raise mix) - Updated ComplexControlFlow: expectedDead 3 → 4 (more accurate) ## Testing ✅ All 18 dead code detection tests passing ✅ All analyzer tests passing (16.8s) ✅ All parser tests passing (0.26s) ## Pattern Follows existing unreachable block creation pattern used for return/break/ continue statements (lines 323-324, 738-740, 759-761 in cfg_builder.go). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>