Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(dead-code): drop residual unused-export false positives (#153)

Raghav Chamadiyaa566e9a4cad1
diff --git a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
index 89021af8f..3c5045370 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
@@ -305,6 +305,17 @@ class DeadCodeAnalyzer:
file_has_importers = self.graph.in_degree(node) > 0
+ # Function/method line ranges in this file — used to skip symbols
+ # whose definition is nested inside another function (closures,
+ # inner helpers). Such symbols are only reachable from their
+ # enclosing scope and are guaranteed false positives.
+ enclosing_ranges = [
+ (sym.get("start_line", 0), sym.get("end_line", 0))
+ for sym in symbols
+ if sym.get("kind") in ("function", "method", "async_function")
+ and sym.get("end_line", 0) > sym.get("start_line", 0)
+ ]
+
for sym in symbols:
if sym.get("visibility") != "public":
continue
@@ -319,9 +330,25 @@ class DeadCodeAnalyzer:
if sym_name.startswith("__") and sym_name.endswith("__"):
continue
+ # Skip nested defs: a symbol whose start_line falls strictly
+ # inside another function/method's body cannot be imported
+ # by name from outside the enclosing scope.
+ sym_start = sym.get("start_line", 0)
+ if any(
+ start < sym_start < end
+ for start, end in enclosing_ranges
+ if (start, end) != (sym_start, sym.get("end_line", 0))
+ ):
+ continue
+
+ # Decorators are stored with the leading "@" (e.g. "@app.route").
+ # _FRAMEWORK_DECORATORS entries are bare prefixes, so compare
+ # against the stripped form.
decorators = sym.get("decorators", [])
if any(
- d.startswith(prefix) for d in decorators for prefix in _FRAMEWORK_DECORATORS
+ d.lstrip("@").startswith(prefix)
+ for d in decorators
+ for prefix in _FRAMEWORK_DECORATORS
):
continue
diff --git a/packages/core/src/repowise/core/ingestion/graph.py b/packages/core/src/repowise/core/ingestion/graph.py
index 777f74a46..26202ea51 100644
--- a/packages/core/src/repowise/core/ingestion/graph.py
+++ b/packages/core/src/repowise/core/ingestion/graph.py
@@ -150,6 +150,7 @@ class GraphBuilder:
language=sym.language,
parent_name=sym.parent_name,
signature=sym.signature,
+ decorators=sym.decorators,
)
# DEFINES edge: file → symbol
diff --git a/tests/unit/test_dead_code.py b/tests/unit/test_dead_code.py
index 99e059c66..89e40b432 100644
--- a/tests/unit/test_dead_code.py
+++ b/tests/unit/test_dead_code.py
@@ -281,6 +281,105 @@ def test_dynamic_pattern_excluded():
assert "EventHandler" not in sym_names
+# ---------------------------------------------------------------------------
+# 6b. test_nested_function_not_flagged
+# ---------------------------------------------------------------------------
+
+
+def test_nested_function_not_flagged():
+ """Symbols defined inside another function's body cannot be imported by
+ name and must not be flagged as unused exports."""
+ g = _build_graph(
+ nodes={
+ "pkg/server.py": {
+ "is_entry_point": False,
+ "is_test": False,
+ "is_api_contract": False,
+ "symbol_count": 2,
+ "symbols": [
+ {
+ "name": "chat",
+ "kind": "function",
+ "visibility": "public",
+ "decorators": [],
+ "start_line": 10,
+ "end_line": 80,
+ },
+ # Nested closure / inner generator — line range strictly
+ # contained inside ``chat``.
+ {
+ "name": "event_stream",
+ "kind": "function",
+ "visibility": "public",
+ "decorators": [],
+ "start_line": 25,
+ "end_line": 60,
+ },
+ ],
+ },
+ },
+ )
+
+ analyzer = DeadCodeAnalyzer(g, git_meta_map={})
+ report = analyzer.analyze(
+ {
+ "detect_unreachable_files": False,
+ "detect_zombie_packages": False,
+ }
+ )
+ sym_names = [f.symbol_name for f in report.findings if f.kind == DeadCodeKind.UNUSED_EXPORT]
+ assert "event_stream" not in sym_names
+
+
+# ---------------------------------------------------------------------------
+# 6c. test_at_prefixed_decorator_excluded
+# ---------------------------------------------------------------------------
+
+
+def test_at_prefixed_decorator_excluded():
+ """Decorators stored with the leading '@' (as the parser emits them)
+ must still match the framework-decorator whitelist."""
+ g = _build_graph(
+ nodes={
+ "pkg/routes.py": {
+ "is_entry_point": False,
+ "is_test": False,
+ "is_api_contract": False,
+ "symbol_count": 2,
+ "symbols": [
+ {
+ "name": "list_items",
+ "kind": "function",
+ "visibility": "public",
+ "decorators": ["@router.get(\"/items\")"],
+ "start_line": 1,
+ "end_line": 8,
+ },
+ {
+ "name": "lifespan",
+ "kind": "function",
+ "visibility": "public",
+ "decorators": ["@asynccontextmanager"],
+ "start_line": 10,
+ "end_line": 20,
+ },
+ ],
+ },
+ },
+ )
+
+ analyzer = DeadCodeAnalyzer(g, git_meta_map={})
+ report = analyzer.analyze(
+ {
+ "detect_unreachable_files": False,
+ "detect_zombie_packages": False,
+ }
+ )
+ sym_names = [f.symbol_name for f in report.findings if f.kind == DeadCodeKind.UNUSED_EXPORT]
+ assert "list_items" not in sym_names
+ assert "lifespan" not in sym_names
+
+
# ---------------------------------------------------------------------------
# 7. test_confidence_low_for_recent_files
# ---------------------------------------------------------------------------