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): a use from inside a symbol's own span is not a use of it (#2863)

Ishita Singhe0dc66125f6f
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 d6f1dfe85..bf3a1ff08 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
@@ -1830,21 +1830,57 @@ class DeadCodeAnalyzer:
)
return self._overload_units.get(base_symbol_id(node), [])
- def _has_inbound_use(self, node: str) -> bool:
+ def _has_inbound_use(self, node: str, _visiting: frozenset[str] = frozenset()) -> bool:
"""Whether any reachability-use edge lands on *node* or its overload set."""
members = self._overload_unit(node)
if members:
- return any(self._lands_on(member) for member in members)
- return self._lands_on(node)
+ return any(self._lands_on(member, _visiting) for member in members)
+ return self._lands_on(node, _visiting)
- def _lands_on(self, node: str) -> bool:
- """Whether any reachability-use edge lands on *node* (False if absent)."""
+ def _lands_on(self, node: str, _visiting: frozenset[str] = frozenset()) -> bool:
+ """Whether any reachability-use edge lands on *node* (False if absent).
+
+ An edge from inside *node*'s own span only counts when that caller is
+ itself live. *_visiting* holds the nodes being resolved up the call
+ chain, so a cycle of contained callers cannot recurse forever.
+ """
if not self.graph.has_node(node):
return False
- return any(
- self.graph.get_edge_data(pred, node, {}).get("edge_type")
- in REACHABILITY_USE_EDGE_TYPES
- for pred in self.graph.predecessors(node)
+ visiting = _visiting | {node}
+ for pred in self.graph.predecessors(node):
+ if (
+ self.graph.get_edge_data(pred, node, {}).get("edge_type")
+ not in REACHABILITY_USE_EDGE_TYPES
+ ):
+ continue
+ if not self._is_self_use(pred, node):
+ return True
+ if pred != node and pred not in visiting and self._has_inbound_use(pred, visiting):
+ return True
+ return False
+
+ def _is_self_use(self, pred: str, node: str) -> bool:
+ """Whether *pred* sits inside *node*'s own span, so its edge is no outside use.
+
+ A self-loop, or a caller declared within the symbol's own body, does
+ not keep the symbol alive on its own; such a caller decides that only
+ if it is itself used from elsewhere (see ``_lands_on``).
+
+ A predecessor whose span is missing or invalid, or that lives in
+ another file, is not treated as inside, so
+ the edge keeps counting as a use.
+ """
+ if pred == node:
+ return True
+ pred_data, node_data = self.graph.nodes[pred], self.graph.nodes[node]
+ if pred_data.get("file_path") != node_data.get("file_path"):
+ return False
+ pred_span, node_span = _symbol_span(pred_data), _symbol_span(node_data)
+ if pred_span["start_line"] is None or node_span["start_line"] is None:
+ return False
+ return (
+ node_span["start_line"] <= pred_span["start_line"]
+ and pred_span["end_line"] <= node_span["end_line"]
)
def _is_internal_candidate(
diff --git a/tests/unit/dead_code/test_unused_internal_self_use.py b/tests/unit/dead_code/test_unused_internal_self_use.py
new file mode 100644
index 000000000..574a80800
--- /dev/null
+++ b/tests/unit/dead_code/test_unused_internal_self_use.py
@@ -0,0 +1,182 @@
+"""Regression tests for self-use and nested liveness in dead-code analysis."""
+
+from __future__ import annotations
+
+from repowise.core.analysis.dead_code import DeadCodeAnalyzer, DeadCodeKind
+from tests.unit.dead_code._helpers import _build_graph
+
+
+def _private_nested_classes(edges: list) -> set[str]:
+ """Unused-internal names for a C# file holding private nested ``Inner``/``Spare``."""
+ g = _build_graph(
+ nodes={
+ "src/Outer.cs": {
+ "language": "csharp",
+ "symbols": [
+ {
+ "name": name,
+ "kind": "class",
+ "language": "csharp",
+ "visibility": "private",
+ "decorators": [],
+ "start_line": line,
+ "end_line": line + 4,
+ }
+ for name, line in (("Inner", 3), ("Spare", 9))
+ ],
+ },
+ },
+ edges=edges,
+ )
+ report = DeadCodeAnalyzer(g, git_meta_map={}).analyze(
+ {
+ "detect_unreachable_files": False,
+ "detect_unused_exports": False,
+ "detect_zombie_packages": False,
+ "min_confidence": 0.0,
+ }
+ )
+ return {f.symbol_name for f in report.findings if f.kind == DeadCodeKind.UNUSED_INTERNAL}
+
+def test_a_constructor_calling_itself_does_not_rescue_its_class():
+ """A self-loop ``calls`` edge on the constructor is no use from outside."""
+ ctor = "src/Outer.cs::Inner::Inner"
+ edges = [(ctor, ctor, {"edge_type": "calls"})]
+ assert _private_nested_classes(edges) == {"Inner", "Spare"}
+
+
+def test_a_caller_inside_the_symbols_own_span_does_not_rescue_it():
+ edges = [("src/Outer.cs::Inner", "src/Outer.cs::Inner", {"edge_type": "calls"})]
+ assert _private_nested_classes(edges) == {"Inner", "Spare"}
+
+
+def _two_private_functions(edges: list) -> set[str]:
+ nodes = {
+ "pkg/mod.py": {
+ "symbols": [
+ {
+ "name": name,
+ "kind": "function",
+ "visibility": "private",
+ "decorators": [],
+ "start_line": start,
+ "end_line": end,
+ }
+ for name, start, end in (("_outer", 1, 10), ("_inner", 3, 5))
+ ],
+ },
+ }
+ g = _build_graph(nodes=nodes, edges=edges)
+ report = DeadCodeAnalyzer(g, git_meta_map={}).analyze(
+ {
+ "detect_unreachable_files": False,
+ "detect_unused_exports": False,
+ "detect_zombie_packages": False,
+ "min_confidence": 0.0,
+ }
+ )
+ return {f.symbol_name for f in report.findings if f.kind == DeadCodeKind.UNUSED_INTERNAL}
+
+
+def test_a_predecessor_declared_inside_the_candidate_span_is_not_a_use():
+ edges = [("pkg/mod.py::_inner", "pkg/mod.py::_outer", {"edge_type": "calls"})]
+ assert "_outer" in _two_private_functions(edges)
+
+
+def test_a_caller_outside_the_candidate_span_still_counts_as_a_use():
+ edges = [("pkg/mod.py::_outer", "pkg/mod.py::_inner", {"edge_type": "calls"})]
+ assert "_inner" not in _two_private_functions(edges)
+
+
+def test_a_caller_with_no_span_still_counts_as_a_use():
+ g_edges = [("pkg/other.py::run", "pkg/mod.py::_outer", {"edge_type": "calls"})]
+ assert "_outer" not in _two_private_functions(g_edges)
+
+
+def _flagged(kind: DeadCodeKind, symbols: list, edges: list, other: bool = False) -> set[str]:
+ """Findings of *kind* for ``pkg/mod.py`` holding *symbols* ``(name, vis, kind, start, end)``."""
+ nodes = {
+ "pkg/mod.py": {
+ "symbols": [
+ {
+ "name": name,
+ "kind": sym_kind,
+ "visibility": vis,
+ "decorators": [],
+ "start_line": start,
+ "end_line": end,
+ }
+ for name, vis, sym_kind, start, end in symbols
+ ],
+ },
+ }
+ if other:
+ nodes["pkg/other.py"] = {
+ "symbols": [
+ {
+ "name": "run",
+ "kind": "function",
+ "visibility": "public",
+ "decorators": [],
+ "start_line": 1,
+ "end_line": 3,
+ }
+ ],
+ }
+ g = _build_graph(nodes=nodes, edges=edges)
+ report = DeadCodeAnalyzer(g, git_meta_map={}).analyze(
+ {
+ "detect_unreachable_files": False,
+ "detect_unused_exports": True,
+ "detect_zombie_packages": False,
+ "min_confidence": 0.0,
+ }
+ )
+ return {f.symbol_name for f in report.findings if f.kind == kind}
+
+
+def test_a_live_nested_subclass_keeps_its_enclosing_class_used():
+ symbols = [("_Node", "private", "class", 1, 20), ("_Leaf", "private", "class", 5, 10)]
+ edges = [
+ ("pkg/mod.py::_Leaf", "pkg/mod.py::_Node", {"edge_type": "extends"}),
+ ("pkg/other.py::run", "pkg/mod.py::_Leaf", {"edge_type": "calls"}),
+ ]
+ assert "_Node" not in _flagged(DeadCodeKind.UNUSED_INTERNAL, symbols, edges, other=True)
+
+
+def test_a_dead_nested_subclass_does_not_keep_its_enclosing_class_used():
+ symbols = [("_Node", "private", "class", 1, 20), ("_Leaf", "private", "class", 5, 10)]
+ edges = [("pkg/mod.py::_Leaf", "pkg/mod.py::_Node", {"edge_type": "extends"})]
+ assert "_Node" in _flagged(DeadCodeKind.UNUSED_INTERNAL, symbols, edges)
+
+
+def test_a_live_method_constructing_its_class_keeps_the_class_used():
+ symbols = [("_K", "private", "class", 1, 20), ("make", "private", "function", 5, 8)]
+ edges = [
+ ("pkg/mod.py::make", "pkg/mod.py::_K", {"edge_type": "calls"}),
+ ("pkg/other.py::run", "pkg/mod.py::make", {"edge_type": "calls"}),
+ ]
+ assert "_K" not in _flagged(DeadCodeKind.UNUSED_INTERNAL, symbols, edges, other=True)
+
+
+def test_contained_symbols_with_the_same_span_calling_each_other_terminate():
+ symbols = [
+ ("_K", "private", "class", 1, 30),
+ ("a", "private", "function", 5, 8),
+ ("b", "private", "function", 5, 8),
+ ]
+ edges = [
+ ("pkg/mod.py::a", "pkg/mod.py::b", {"edge_type": "calls"}),
+ ("pkg/mod.py::b", "pkg/mod.py::a", {"edge_type": "calls"}),
+ ("pkg/mod.py::a", "pkg/mod.py::_K", {"edge_type": "calls"}),
+ ]
+ assert "_K" in _flagged(DeadCodeKind.UNUSED_INTERNAL, symbols, edges)
+
+
+def test_a_live_nested_public_subclass_keeps_its_exported_class_used():
+ symbols = [("Node", "public", "class", 1, 20), ("Leaf", "public", "class", 5, 10)]
+ edges = [
+ ("pkg/mod.py::Leaf", "pkg/mod.py::Node", {"edge_type": "extends"}),
+ ("pkg/other.py::run", "pkg/mod.py::Leaf", {"edge_type": "calls"}),
+ ]
+ assert "Node" not in _flagged(DeadCodeKind.UNUSED_EXPORT, symbols, edges, other=True)