Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(dataflow): stop reading a may-def's bookkeeping use as an Extract Method param (#2959)
Raghav Chamadiya63ccfc0586d8
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/languages.py b/packages/core/src/repowise/core/analysis/health/complexity/languages.pyindex c1440393e..b07354930 100644--- a/packages/core/src/repowise/core/analysis/health/complexity/languages.py+++ b/packages/core/src/repowise/core/analysis/health/complexity/languages.py@@ -260,6 +260,11 @@ class LanguageNodeMap:# block's tail expression (the implicit value an extraction would# silently drop), so only truly expression-oriented grammars may map it.statement_wrapper_kinds: frozenset[str] = frozenset()+ # * ``value_passthrough_kinds`` -- nodes a block's value flows through+ # unchanged on its way out (Rust ``if`` / ``else`` / ``match`` arms), so+ # the slicer can tell whether an unterminated tail statement's value is+ # consumed. Only meaningful with ``statement_wrapper_kinds``.+ value_passthrough_kinds: frozenset[str] = frozenset()# -- Decorators / annotations (mock-saturation pass) ---------------------# * ``decorator_kinds`` -- the node a single ``@thing`` is parsed as.@@ -593,6 +598,16 @@ _RUST = LanguageNodeMap(# node, and the slicer uses this as the expression-oriented marker for# tail-expression suppression.statement_wrapper_kinds=frozenset({"expression_statement"}),+ value_passthrough_kinds=frozenset(+ {+ "if_expression",+ "else_clause",+ "match_expression",+ "match_block",+ "match_arm",+ "unsafe_block",+ }+ ),)diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.pyindex 2b82af357..a98395991 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/base.py@@ -62,6 +62,11 @@ class Occurrence:#: initializer) reads an outer variable of the same name. None for any#: other write, and for a declarator that ends on a later line.declared_at: int | None = None+ #: A use recorded only because the paired write may not happen (a write+ #: inside a ``switch`` / ``match`` arm the CFG keeps as one statement), not+ #: a read in the source. It keeps must-def proofs conservative; code that+ #: asks what a span actually reads skips it.+ echo: bool = False@dataclass(frozen=True)@@ -284,6 +289,31 @@ class BaseDefUseDialect:) -> StatementDefUse: # pragma: no cover - abstractraise NotImplementedError+ def _process(+ self, node: Node | None, defs: list[Occurrence], uses: list[Occurrence]+ ) -> None: # pragma: no cover - abstract+ raise NotImplementedError++ def _process_may_def(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:+ """Process *node* whose writes execute only on some path (a switch or+ match arm, an expression-position ``if`` / loop, a ``let-else`` arm).++ Each def found within is recorded as a def AND a use: the may-def keeps+ the variable in every "written in this region" set while its paired use+ stays upward-exposed, so a downstream must-def proof can only get more+ conservative, never less. The paired use is marked :attr:`Occurrence.echo`.+ """+ inner_defs: list[Occurrence] = []+ for child in node.named_children: # not the node itself: no re-dispatch+ self._process(child, inner_defs, uses)+ defs.extend(inner_defs)+ uses.extend(echoes(inner_defs))+++def echoes(defs: list[Occurrence]) -> list[Occurrence]:+ """The may-def uses paired with *defs* (see :attr:`Occurrence.echo`)."""+ return [replace(occ, echo=True) for occ in defs]+def _written_before_read(writes: list[Occurrence], reads: list[Occurrence]) -> set[str]:"""Names whose first write comes on an earlier line than their first read."""diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.pyindex 84219e526..7e888d5e4 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/cpp.py@@ -228,20 +228,6 @@ class CppDefUseDialect(BaseDefUseDialect):for child in node.named_children:self._process(child, defs, uses)- def _process_may_def(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:- """Process *node* whose writes execute only on some path (a switch arm).-- Each def found within is recorded as a def AND a use: the may-def keeps- the variable in every "written in this region" set while its paired use- stays upward-exposed, so a downstream must-def proof can only get more- conservative, never less.- """- inner_defs: list[Occurrence] = []- for child in node.named_children: # not the node itself: no re-dispatch- self._process(child, inner_defs, uses)- defs.extend(inner_defs)- uses.extend(inner_defs)-# -- write-target extraction ----------------------------------------------def _targets(self, node: Node | None, defs: list[Occurrence], uses: list[Occurrence]) -> None:diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.pyindex 1b0f38bf3..39f92235e 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/java.py@@ -188,20 +188,6 @@ class JavaDefUseDialect(BaseDefUseDialect):for child in node.named_children:self._process(child, defs, uses)- def _process_may_def(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:- """Process *node* whose writes execute only on some path (a switch arm).-- Each def found within is recorded as a def AND a use: the may-def keeps- the variable in every "written in this region" set while its paired use- stays upward-exposed, so a downstream must-def proof can only get more- conservative, never less.- """- inner_defs: list[Occurrence] = []- for child in node.named_children: # not the node itself: no re-dispatch- self._process(child, inner_defs, uses)- defs.extend(inner_defs)- uses.extend(inner_defs)-# -- write-target extraction ----------------------------------------------def _targets(self, node: Node | None, defs: list[Occurrence], uses: list[Occurrence]) -> None:diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.pyindex 6c1c1c3f6..9b87e32db 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/rust.py@@ -32,7 +32,7 @@ from __future__ import annotationsfrom typing import TYPE_CHECKING-from .base import BaseDefUseDialect, Occurrence, StatementDefUse+from .base import BaseDefUseDialect, Occurrence, StatementDefUse, echoesif TYPE_CHECKING:from tree_sitter import Node@@ -45,6 +45,7 @@ _ASSIGN_KINDS = frozenset({"assignment_expression"})_AUG_KINDS = frozenset({"compound_assignment_expr"})_LET_DECL = "let_declaration"_LET_CONDITION = "let_condition" # ``if let PAT = expr`` / ``while let ...``+_MATCH_ARM = "match_arm" # ``PAT [if guard] => value``# Pattern containers whose identifier leaves are binders (defs)._PATTERN_CONTAINERS = frozenset({@@ -168,7 +169,7 @@ class RustDefUseDialect(BaseDefUseDialect):binders: list[Occurrence] = []self._targets(node.child_by_field_name("pattern"), binders, uses)defs.extend(binders)- uses.extend(binders)+ uses.extend(echoes(binders))self._process(node.child_by_field_name("value"), defs, uses)else:self._process(node, defs, uses)@@ -204,6 +205,10 @@ class RustDefUseDialect(BaseDefUseDialect):if t == _LET_CONDITION:self._condition(node, defs, uses)return+ if t == _MATCH_ARM:+ self._arm_pattern(node.child_by_field_name("pattern"), defs, uses)+ self._process(node.child_by_field_name("value"), defs, uses)+ returnif t == _SCOPED_IDENTIFIER: # a path, never a local readreturnif t in self.member_access_kinds:@@ -218,18 +223,29 @@ class RustDefUseDialect(BaseDefUseDialect):for child in node.named_children:self._process(child, defs, uses)- def _process_may_def(self, node: Node, defs: list[Occurrence], uses: list[Occurrence]) -> None:- """Process *node* whose writes execute only on some path (a match arm,- an expression-position ``if`` / loop, a ``let-else`` arm).+ def _arm_pattern(+ self, pattern: Node | None, defs: list[Occurrence], uses: list[Occurrence]+ ) -> None:+ """A match arm's pattern binds the names it holds for the arm's value.- Each def found within is recorded as a def AND a use, so a downstream- must-def proof can only get more conservative, never less.+ The arm sits inside a ``match`` the CFG keeps as one statement, so these+ are may-defs like the arm's other writes. A bare capitalised name+ (``None``, ``MAX``) is a unit variant or a constant, not a binder: Rust+ lints a capitalised binding, so the convention holds. The guard+ (``y if y > 2``) only reads."""- inner_defs: list[Occurrence] = []- for child in node.named_children: # not the node itself: no re-dispatch- self._process(child, inner_defs, uses)- defs.extend(inner_defs)- uses.extend(inner_defs)+ if pattern is None:+ return+ guard = pattern.child_by_field_name("condition")+ binders: list[Occurrence] = []+ for child in pattern.named_children:+ if guard is None or child.id != guard.id:+ self._targets(child, binders, uses)+ # The names exist from the end of the pattern, so the arm's value on+ # the same line reads them, not an outer variable.+ self._declare(binders, 0, pattern)+ defs.extend(b for b in binders if not b.name[:1].isupper())+ self._process(guard, defs, uses)# -- write-target extraction (assignment LHS + binding patterns) ----------@@ -237,6 +253,8 @@ class RustDefUseDialect(BaseDefUseDialect):if node is None:returnt = node.type+ if t == _SCOPED_IDENTIFIER: # a path (``std::i32::MAX``) never binds+ returnif t in self.identifier_kinds or t == _SHORTHAND_BINDER:defs.append(self._occ(node))returndiff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice.pyindex 8ebf80397..7691fabe2 100644--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py@@ -142,6 +142,9 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[stmts = block.named_childrenn = len(stmts)is_body = block.id == body_container.id+ keeps_tail = tail_stmt_kinds is not None and _tail_is_block_value(+ stmts, tail_stmt_kinds, fn_node, lmap+ )# One subtree walk per statement, then O(1) metrics per span via# prefix sums. _span_metrics processes each span statement's subtree# independently, so a span's decision count is the sum over its@@ -176,11 +179,7 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[# Never extract the whole function body (that is not a split).if is_body and length == n:continue- if (- tail_stmt_kinds is not None- and j == n - 1- and stmts[j].type not in tail_stmt_kinds- ):+ if keeps_tail and j == n - 1:continuedecisions = dec_prefix[j + 1] - dec_prefix[i]has_jump = jump_prefix[j + 1] > jump_prefix[i]@@ -218,6 +217,58 @@ def find_extractions(analysis: FunctionAnalysis, lmap: LanguageNodeMap) -> list[return _sorted(out)+def _tail_is_block_value(+ stmts: list[Node], tail_stmt_kinds: frozenset[str], fn_node: Node, lmap: LanguageNodeMap+) -> bool:+ """True when the block's last statement is its value, which a span may not+ end on (lifting it would drop the value).++ That is a bare tail expression, and also an unterminated statement-wrapped+ one (Rust parses a tail ``if`` / ``match`` with no ``;`` as an+ ``expression_statement``) whose value something consumes: an ``else``+ block in a ``let`` initializer, the body of a function with a return type.+ A loop body's value, and that of a block in statement position, is+ discarded, so ending there stays allowed.+ """+ if not stmts:+ return False+ last = stmts[-1]+ if last.type not in tail_stmt_kinds:+ return True+ if last.type not in lmap.statement_wrapper_kinds or last.children[-1].type == ";":+ return False+ return _block_value_used(last.parent, fn_node, lmap)+++def _block_value_used(block: Node, fn_node: Node, lmap: LanguageNodeMap) -> bool:+ """Whether the value of *block* reaches anything, climbing through the+ conditional chain that carries it (``else`` / ``match`` arms)."""+ node = block+ while True:+ parent = node.parent+ if parent is None or parent.id == fn_node.id:+ return fn_node.child_by_field_name("return_type") is not None+ verdict = _value_hop(node, parent, lmap)+ if verdict is not None:+ return verdict+ node = parent+++def _value_hop(node: Node, parent: Node, lmap: LanguageNodeMap) -> bool | None:+ """One step of :func:`_block_value_used`: True or False when *parent*+ settles whether *node*'s value is used, None to keep climbing."""+ if parent.type in lmap.loop_kinds:+ return False # a loop body's value is discarded+ if parent.type in lmap.block_kinds:+ # Only the last statement carries a block's value onward.+ return None if parent.named_children[-1].id == node.id else False+ if parent.type in lmap.statement_wrapper_kinds:+ return False if parent.children[-1].type == ";" else None+ if parent.type in lmap.value_passthrough_kinds:+ return None+ return True # a let initializer, an argument, an operand++def _function_lines(fn_node: Node) -> list[str]:"""Source rows indexed by absolute row, rebuilt from the function's own text.@@ -253,7 +304,10 @@ def _var_lines(def_use: FunctionDefUse) -> tuple[dict[str, list[int]], dict[str,def_lines[d.var].append(d.line)for bdu in def_use.blocks.values():for u in bdu.uses:- use_lines[u.name].append(u.line)+ # A may-def's paired use is bookkeeping, not a read: counted, a+ # binder declared inside a ``match`` arm became its own parameter.+ if not u.echo:+ use_lines[u.name].append(u.line)# A closure's read counts where the closure is written: lifting the code# around it moves the read with it. Only names this function binds matter.for u in def_use.captured:@@ -281,6 +335,8 @@ def _declared_before_read(def_use: FunctionDefUse) -> dict[str, frozenset[int]]:first_read: dict[tuple[str, int], int] = {}for bdu in def_use.blocks.values():for u in bdu.uses:+ if u.echo:+ continuekey = (u.name, u.line)first_read[key] = min(u.column, first_read.get(key, u.column))declared: dict[str, set[int]] = defaultdict(set)diff --git a/tests/unit/health/test_dataflow_langs.py b/tests/unit/health/test_dataflow_langs.pyindex febf37cb8..4de4e00e1 100644--- a/tests/unit/health/test_dataflow_langs.py+++ b/tests/unit/health/test_dataflow_langs.py@@ -1880,3 +1880,149 @@ def test_rust_span_holding_an_exit_macro_is_not_offered():lmap = get_language_map("rust")fn = _first("rust", src)assert all(not (e.start_line <= 6 <= e.end_line) for e in find_extractions(fn, lmap))+++# == May-def bookkeeping is not a read ===========================================+++def test_rust_if_let_binder_inside_the_span_is_not_a_param():+ # Both ``if let`` arms bind their own ``var``; the first one's binding is+ # out of scope by the time the span runs, so it cannot be passed in.+ src = """+ fn flag_doc(flag: &Flag, out: &mut String) {+ if let Some(var) = flag.doc_variable() {+ out.push_str(var);+ }+ let name = flag.name_long();+ out.push_str(name);+ if let Some(var) = flag.doc_variable() {+ if var.len() > 3 {+ out.push_str(var);+ }+ }+ out.push_str("\n");+ let doc = flag.doc_long();+ if doc.len() > 10 {+ out.push_str(doc);+ }+ out.push_str("\n");+ out.push_str("\n");+ }+ """+ lmap = get_language_map("rust")+ spans = [e for e in find_extractions(_first("rust", src), lmap) if e.start_line <= 8 <= e.end_line]+ assert spans+ assert all("var" not in e.params for e in spans)+++def test_rust_let_inside_a_match_arm_is_not_a_param():+ src = """+ fn analyse(kind: &Kind, limit: usize) -> usize {+ match kind {+ Kind::One(items) => {+ let mut total = items.len();+ total+ }+ Kind::Many(items) => {+ let mut total = 0;+ for item in items.iter() {+ if item.len() > limit {+ total += limit;+ } else {+ total += item.len();+ }+ }+ total+ }+ }+ }+ """+ lmap = get_language_map("rust")+ fn = _first("rust", src)+ spans = [e for e in find_extractions(fn, lmap) if e.start_line == 9]+ assert spans+ assert all("total" not in e.params for e in spans)+++def test_rust_span_ending_on_a_consumed_tail_if_is_not_offered():+ # The ``else`` block's last ``if`` is its value, read by the ``let``.+ src = """+ fn matcher(names: &[String], dir: &str, case: bool) -> usize {+ let found = if names.is_empty() {+ 0+ } else {+ let wanted: Vec<&String> = names.iter().filter(|n| n.len() > 2).collect();+ log(dir);+ log(case);+ if wanted.is_empty() {+ 0+ } else {+ let m = wanted.len();+ m + 1+ }+ };+ found+ }+ """+ lmap = get_language_map("rust")+ fn = _first("rust", src)+ assert all(e.end_line != 14 for e in find_extractions(fn, lmap))+++def test_rust_span_ending_on_an_if_in_a_loop_body_is_still_offered():+ src = """+ fn tally(items: &[usize], limit: usize, out: &mut Vec<usize>) -> usize {+ let count = items.len();+ for item in items {+ let doubled = item * 2;+ log(doubled);+ log(limit);+ if doubled > limit {+ out.push(limit);+ } else {+ out.push(doubled);+ }+ }+ count+ }+ """+ lmap = get_language_map("rust")+ fn = _first("rust", src)+ assert any(e.end_line == 12 for e in find_extractions(fn, lmap))+++def test_rust_match_arm_binders_are_params_of_a_span_in_the_arm():+ src = """+ fn class_regex(re: &mut String, tokens: &[Token]) {+ for tok in tokens.iter() {+ match *tok {+ Token::Class { negated, ref ranges } => {+ re.push('[');+ if negated {+ re.push('^');+ }+ for r in ranges {+ if r.0 == r.1 {+ re.push(r.0);+ } else {+ re.push(r.1);+ }+ }+ re.push(']');+ }+ None => {}+ std::i32::MAX => {}+ consts::PI => {}+ y if y > 2 => {}+ }+ }+ }+ """+ fn = _first("rust", src)+ defs = {(d.var, d.line) for d in fn.def_use.definitions}+ assert ("negated", 5) in defs and ("ranges", 5) in defs+ bogus = {"None", "Token", "Class", "std", "i32", "MAX", "consts", "PI"}+ assert not any(var in bogus for var, _ in defs)+ spans = [e for e in find_extractions(fn, get_language_map("rust")) if e.start_line <= 7]+ assert spans+ assert all({"negated", "ranges"} <= set(e.params) for e in spans)