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(health): return Extract Method values a later conditional write does not kill (#3384)

Raghav Chamadiyaf20ae063f1d9
diff --git a/docs/layers/REFACTORING.md b/docs/layers/REFACTORING.md
index 9b9a704db..8e55596f3 100644
--- a/docs/layers/REFACTORING.md
+++ b/docs/layers/REFACTORING.md
@@ -213,7 +213,9 @@ Most plans answer a health finding, so per-path marker rules in
- Extract Method offers a span only when it can show the extraction keeps behavior
(every returned value written on every path, no state carried across loop
iterations, no local a closure shares across the span's edge). Spans it cannot
- prove are dropped, so it under-reports by design.
+ prove are dropped, so it under-reports by design. A value the span writes is
+ returned whenever any path can carry it to a later read, including past a later
+ write in an `else` arm or a `try` body that may raise first.
- Extract Method skips spans too small to matter and spans that would carry the
original finding into the helper. A component whose branching is mostly in its
markup gets no extraction.
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/cfg.py b/packages/core/src/repowise/core/analysis/health/dataflow/cfg.py
index 35689af24..004b7571e 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/cfg.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/cfg.py
@@ -34,6 +34,14 @@ children, while the C-family (Go / TS / Java / Rust) hangs the else arm off an
``alternative`` field -- the builder recognises both. A language that maps no
``if_kinds`` / ``block_kinds`` simply yields a straight-line CFG (degrade to
silence), the same precision-first default as the def/use dialect.
+
+**Exceptions.** A ``try`` body, ``else`` clause or handler is joined to the
+handlers / ``finally`` it can raise into from the state before it and at the
+end of each of its blocks, not between two statements of one block: a write
+that a later statement of the same block overwrites never reaches the handler
+(a ``try`` body ``x = 1; y = g(); x = 2`` hides ``x = 1`` from an
+``except`` that reads ``x``). Splitting every protected statement into its own block would close
+that gap at the cost of a much larger graph.
"""
from __future__ import annotations
@@ -223,6 +231,10 @@ class _CFGBuilder:
if src.id not in dst.predecessors:
dst.predecessors.append(src.id)
+ def _edges_to(self, sources: list[BasicBlock], dst: BasicBlock) -> None:
+ for src in sources:
+ self._edge(src, dst)
+
def _record_stmt(self, block: BasicBlock, node: Node) -> None:
block.statements.append(
Statement(node.type, node.start_point[0] + 1, node.end_point[0] + 1, node=node)
@@ -480,46 +492,49 @@ class _CFGBuilder:
self._edge(fin_out, join)
normal_join = fin_entry
- # try body (the protected region)
+ # try body (the protected region). An exception may leave it before
+ # its first statement completes or from any block inside it, so the
+ # state before the body and at the end of every body block reaches the
+ # handlers and the ``finally`` (which also runs on a body ``return``).
+ # Blocks are numbered in creation order: a region's blocks are the
+ # ones created from its first id on.
+ first = len(self.blocks)
body_entry = self._new()
self._edge(cur, body_entry)
- # The finally body runs on every path out of the protected region,
- # including abrupt exits (a body / handler ``return`` or ``raise``)
- # that never reach the normal join. Approximate with an edge from the
- # region entry, mirroring the handler edges below; without it a
- # ``finally`` after an always-returning body is flagged unreachable.
- if finally_clause is not None:
- self._edge(body_entry, normal_join)
body_out = self._process_seq(
self._body_stmts(try_node.child_by_field_name("body")), body_entry
)
+ protected = [cur, *self.blocks[first:]]
+ if finally_clause is not None:
+ self._edges_to(protected, normal_join)
- # ``else`` runs only when the body completed without an exception.
+ # ``else`` runs only when the body completed without an exception; one
+ # raised inside it skips the handlers but still runs the ``finally``.
else_clause = next((c for c in try_node.children if c.type == "else_clause"), None)
if else_clause is not None and body_out is not None:
+ first = len(self.blocks)
else_entry = self._new()
self._edge(body_out, else_entry)
else_out = self._process_seq(self._body_stmts(self._block_of(else_clause)), else_entry)
if else_out is not None:
self._edge(else_out, normal_join)
+ if finally_clause is not None:
+ self._edges_to(self.blocks[first:], normal_join)
elif body_out is not None:
self._edge(body_out, normal_join)
- # except handlers are reachable from the protected region (an exception
- # may escape any statement in the body -> approximate with an edge from
- # the body entry to each handler).
for handler in (c for c in try_node.children if c.type in self.lmap.catch_kinds):
+ first = len(self.blocks)
handler_entry = self._new("handler")
- self._edge(body_entry, handler_entry)
- if finally_clause is not None:
- # Finally also runs when a handler returns or re-raises. This
- # edge lets handler definitions reach reads in the finally body.
- self._edge(handler_entry, normal_join)
+ self._edges_to(protected, handler_entry)
handler_out = self._process_seq(
self._body_stmts(self._block_of(handler)), handler_entry
)
if handler_out is not None:
self._edge(handler_out, normal_join)
+ # A handler that returns or raises part-way still runs ``finally``.
+ if finally_clause is not None:
+ self._edges_to(self.blocks[first:], normal_join)
return join
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/facts.py b/packages/core/src/repowise/core/analysis/health/dataflow/facts.py
index a684cf8c4..8d4c6c9c5 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/facts.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/facts.py
@@ -40,6 +40,7 @@ from .analyze import FunctionAnalysis
from .dialects.base import get_defuse_dialect
from .gating import is_flagged
from .parsing import function_metrics, parse_source
+from .reaching import observed_definitions
if TYPE_CHECKING:
from collections.abc import Iterable
@@ -155,42 +156,13 @@ def derive_facts(analysis: FunctionAnalysis) -> DataflowFacts:
def _live_definition_indices(analysis: FunctionAnalysis) -> set[int]:
"""Definition indices some read can observe (conservative: over-marks live).
- A use of ``v`` at line ``L`` in block ``b`` observes:
-
- - the latest same-block definition(s) of ``v`` strictly before ``L``, or,
- when none exists, every definition of ``v`` reaching ``IN[b]``;
- - plus, conservatively, any same-block definition of ``v`` AT line ``L``
- (multiple statements on one line cannot be ordered by line numbers alone,
- so ambiguity counts as live -- fewer dead stores, never a false one).
-
- Anything the rules cannot prove observed stays potentially dead; the caller
- subtracts this set from all body definitions to get the dead stores.
+ Anything :func:`observed_definitions` cannot prove observed stays
+ potentially dead; the caller subtracts this set from all body definitions
+ to get the dead stores.
"""
- def_use = analysis.def_use
- reaching = analysis.reaching
live: set[int] = set()
-
- for bid, bdu in def_use.blocks.items():
- if not bdu.uses:
- continue
- in_defs = reaching.in_sets.get(bid, frozenset())
- in_by_var: dict[str, list[int]] = {}
- for i in in_defs:
- in_by_var.setdefault(reaching.definitions[i].var, []).append(i)
- local_by_var: dict[str, list] = {}
- for d in bdu.defs:
- local_by_var.setdefault(d.var, []).append(d)
-
- for use in bdu.uses:
- local = local_by_var.get(use.name, [])
- earlier = [d for d in local if d.line < use.line]
- if earlier:
- latest = max(d.line for d in earlier)
- live.update(d.index for d in earlier if d.line == latest)
- else:
- live.update(in_by_var.get(use.name, ()))
- live.update(d.index for d in local if d.line == use.line)
-
+ for _use, seen in observed_definitions(analysis.def_use, analysis.reaching):
+ live.update(seen)
return live
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/reaching.py b/packages/core/src/repowise/core/analysis/health/dataflow/reaching.py
index 740f8f16d..9988adb2a 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/reaching.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/reaching.py
@@ -29,8 +29,11 @@ from dataclasses import dataclass
from typing import TYPE_CHECKING
if TYPE_CHECKING:
+ from collections.abc import Iterable, Iterator
+
from .cfg import CFG
from .defuse import Definition, FunctionDefUse
+ from .dialects.base import Occurrence
@dataclass
@@ -126,3 +129,51 @@ def compute_reaching(
definitions=fdu.definitions,
converged=converged,
)
+
+
+def observed_definitions(
+ fdu: FunctionDefUse, reaching: ReachingDefinitions
+) -> Iterator[tuple[Occurrence, list[int]]]:
+ """Each read in *fdu* with the definition indices it may observe
+ (:func:`_observe`)."""
+ for bid, bdu in fdu.blocks.items():
+ if not bdu.uses:
+ continue
+ in_by_var: dict[str, list[int]] = {}
+ for i in reaching.in_sets.get(bid, frozenset()):
+ in_by_var.setdefault(reaching.definitions[i].var, []).append(i)
+ local_by_var: dict[str, list[Definition]] = {}
+ for d in bdu.defs:
+ local_by_var.setdefault(d.var, []).append(d)
+ for use in bdu.uses:
+ seen = _observe(local_by_var.get(use.name, []), in_by_var.get(use.name, ()), use.line)
+ yield use, seen
+
+
+def definitions_at(
+ fdu: FunctionDefUse, reaching: ReachingDefinitions, block_id: int, var: str, line: int
+) -> list[int]:
+ """The definition indices of *var* a read at *line* in *block_id* may
+ observe (:func:`_observe`)."""
+ bdu = fdu.block(block_id)
+ local = [d for d in bdu.defs if d.var == var] if bdu is not None else []
+ entering = [
+ i for i in reaching.in_sets.get(block_id, ()) if reaching.definitions[i].var == var
+ ]
+ return _observe(local, entering, line)
+
+
+def _observe(local: list[Definition], entering: Iterable[int], line: int) -> list[int]:
+ """A read at *line* observes the latest same-block definitions (*local*)
+ strictly before it or, when there is none, every definition *entering* the
+ block; plus any same-block definition at *line* itself, since lines alone
+ cannot order a write and a read sharing one. Over-marking is the safe side
+ for every consumer."""
+ earlier = [d for d in local if d.line < line]
+ if earlier:
+ latest = max(d.line for d in earlier)
+ seen = [d.index for d in earlier if d.line == latest]
+ else:
+ seen = list(entering)
+ seen.extend(d.index for d in local if d.line == line)
+ return seen
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
index 47d6e0912..593a5a58b 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
@@ -7,8 +7,8 @@ helper's signature:
- **IN (parameters)** -- variables the span *reads* whose value is produced
before the span (defined-before, used-inside).
-- **OUT (return)** -- variables the span *defines* that are *used after* it,
- with no intervening redefinition (so the helper returns the live value).
+- **OUT (return)** -- variables the span *defines* whose value may reach a
+ read after it along some CFG path (so the helper returns the live value).
**Extractability predicate (precision-first).** A span is a candidate only when
it cuts at statement boundaries within a single block (so never a partial
@@ -19,11 +19,11 @@ matter without being nearly the whole body, and has at most one return and a
small parameter list. Everything else is suppressed -- ten great extractions,
not two hundred maybes.
-Line-based liveness over D2's def/use occurrences realises the IN/OUT inference;
-the CFG/jump scan realises the single-exit predicate. The jump and nested-scope
-node kinds come from the language's ``LanguageNodeMap`` (the same source the CFG
-builder uses), so every full-tier language whose map populates them is served by
-this one slicer.
+Line-based liveness over D2's def/use occurrences realises the IN inference,
+reaching definitions over the CFG the OUT inference, and the CFG/jump scan the
+single-exit predicate. The jump and nested-scope node kinds come from the
+language's ``LanguageNodeMap`` (the same source the CFG builder uses), so every
+full-tier language whose map populates them is served by this one slicer.
"""
from __future__ import annotations
@@ -37,12 +37,14 @@ from ..complexity.ast_utils import member_object, self_member_name
from ..complexity.body_facts import BodyTally, FactKinds, is_exit, step
from ..complexity.nloc import _code_line_numbers
from .dialects.base import SUPER, mentions_receiver
+from .reaching import definitions_at, observed_definitions
if TYPE_CHECKING:
from tree_sitter import Node
from ..complexity.languages import LanguageNodeMap
from .analyze import FunctionAnalysis
+ from .cfg import CFG
from .defuse import FunctionDefUse
from .dialects.base import Receiver
@@ -170,6 +172,8 @@ def find_extractions(
def_lines, use_lines = _var_lines(analysis.def_use)
declared_first = _declared_before_read(analysis.def_use)
+ # Built on the first span that clears the cheap gates.
+ reads: dict[str, list[tuple[int, frozenset[int]]]] | None = None
hoisted = _hoisted_bindings(def_lines, use_lines)
decl_lines = _declaration_lines(analysis.def_use)
shared = _closure_state(analysis.def_use, def_lines, use_lines)
@@ -220,7 +224,11 @@ def find_extractions(
span = stmts[i : j + 1]
s = span[0].start_point[0] + 1
e = span[-1].end_point[0] + 1
- params, returns = _infer_in_out(def_lines, use_lines, s, e, declared_first)
+ if reads is None:
+ reads = _reads_observing(analysis, def_lines)
+ params, returns = _infer_in_out(
+ def_lines, use_lines, s, e, declared_first, reads=reads
+ )
if len(params) > _MAX_PARAMS or len(returns) > _MAX_RETURNS:
continue
if not _outs_definitely_assigned(span, returns, def_lines, lmap):
@@ -581,12 +589,66 @@ def _declared_before_read(def_use: FunctionDefUse) -> dict[str, frozenset[int]]:
return {var: frozenset(lines) for var, lines in declared.items()}
+def _reads_observing(
+ analysis: FunctionAnalysis, def_lines: dict[str, list[int]]
+) -> dict[str, list[tuple[int, frozenset[int]]]]:
+ """Per variable, each read's line with the def lines it may observe.
+
+ Reads take reaching definitions over the CFG, so a later write on a path
+ that need not run (an ``else`` arm, a ``try`` body that may raise first)
+ does not hide the span's value from the read. A closure's read observes
+ what reaches the statement that creates the closure, or, where no recorded
+ statement covers it, the nearest def above it: a read is never dropped,
+ since a dropped read can only drop a return. Only names in *def_lines*
+ count for closures, as in :func:`_var_lines`.
+ """
+ def_use, reaching = analysis.def_use, analysis.reaching
+ defs = reaching.definitions
+ reads: dict[str, list[tuple[int, frozenset[int]]]] = defaultdict(list)
+ for use, seen in observed_definitions(def_use, reaching):
+ if not use.echo:
+ reads[use.name].append((use.line, frozenset(defs[i].line for i in seen)))
+ statements: dict[int, tuple[int, int]] | None = None
+ for u in def_use.captured.reads:
+ dl = def_lines.get(u.name)
+ if not dl:
+ continue
+ if statements is None:
+ statements = _statements_by_line(analysis.cfg)
+ at = statements.get(u.line)
+ if at is not None:
+ observed = frozenset(
+ defs[i].line for i in definitions_at(def_use, reaching, at[0], u.name, at[1])
+ )
+ else:
+ k = bisect_left(dl, u.line)
+ observed = frozenset(dl[k - 1 : k])
+ reads[u.name].append((u.line, observed))
+ return reads
+
+
+def _statements_by_line(cfg: CFG) -> dict[int, tuple[int, int]]:
+ """Per line, (block id, start line) of the innermost recorded statement
+ covering it."""
+ best: dict[int, tuple[int, int, int]] = {}
+ for block in cfg.blocks:
+ for st in block.statements:
+ width = st.end_line - st.start_line
+ for line in range(st.start_line, st.end_line + 1):
+ held = best.get(line)
+ if held is None or width < held[0]:
+ best[line] = (width, block.id, st.start_line)
+ return {line: (bid, start) for line, (_w, bid, start) in best.items()}
+
+
def _infer_in_out(
def_lines: dict[str, list[int]],
use_lines: dict[str, list[int]],
s: int,
e: int,
- declared_first: dict[str, frozenset[int]] | None = None,
+ declared_first: dict[str, frozenset[int]],
+ *,
+ reads: dict[str, list[tuple[int, frozenset[int]]]],
) -> tuple[tuple[str, ...], tuple[str, ...]]:
"""Infer IN (parameters) and OUT (return) variables for span ``[s, e]``.
@@ -594,8 +656,11 @@ def _infer_in_out(
an in-span write, and which has a definition before the span (a parameter or
an earlier assignment). A write on the same line as that read precedes it
only where *declared_first* (:func:`_declared_before_read`) says the line
- declares the name first. OUT: a variable written in the span and read after
- it, with no redefinition between the span and that first later read.
+ declares the name first. OUT: a variable written in the span with a read
+ after it that may observe one of those writes (*reads*,
+ :func:`_reads_observing`). A read before the span that a write in it
+ reaches around a loop is not an OUT: :func:`_loop_carry_free` refuses
+ those spans.
"""
params: list[str] = []
returns: list[str] = []
@@ -607,17 +672,14 @@ def _infer_in_out(
if in_uses and any(ln < s for ln in dl):
first_use = in_uses[0]
- declared = first_use in (declared_first or {}).get(var, ())
+ declared = first_use in declared_first.get(var, ())
if not declared and not any(ln < first_use for ln in in_defs):
params.append(var)
- if in_defs:
- after_uses = [ln for ln in ul if ln > e]
- if after_uses:
- first_after = after_uses[0]
- redefined = any(e < ln < first_after for ln in dl)
- if not redefined:
- returns.append(var)
+ if in_defs and any(
+ ln > e and any(s <= d <= e for d in seen) for ln, seen in reads.get(var, ())
+ ):
+ returns.append(var)
return tuple(params), tuple(returns)
diff --git a/tests/unit/health/test_dataflow_facts.py b/tests/unit/health/test_dataflow_facts.py
index dba8a8a9b..20193c795 100644
--- a/tests/unit/health/test_dataflow_facts.py
+++ b/tests/unit/health/test_dataflow_facts.py
@@ -300,3 +300,87 @@ def test_health_pass_releases_each_file_after_evaluating_it(
else:
analyzer.analyze()
assert sorted(released) == sorted(str(pf.file_info.abs_path) for pf in parsed)
+
+
+# ---------------------------------------------------------------------------
+# Exception paths: a protected region reaches its handlers and ``finally``
+# from the state before it and from every block inside it.
+# ---------------------------------------------------------------------------
+
+
+def _dead(src: str) -> list[tuple[str, int]]:
+ return [(d.var, d.line) for d in _facts(src, 2).dead_stores]
+
+
+def test_a_write_before_try_is_live_when_the_body_may_raise_first():
+ # ``g()`` may raise before ``x = 2``, so the handler reads ``x = 1``.
+ dead = _dead(
+ """
+ def f(g, use):
+ x = 1
+ try:
+ y = g()
+ x = 2
+ except Exception:
+ use(x)
+ return x, y
+ """
+ )
+ assert ("x", 3) not in dead
+
+
+def test_finally_reads_the_write_before_a_body_that_may_raise():
+ dead = _dead(
+ """
+ def f(g, use):
+ x = 1
+ try:
+ x = g()
+ finally:
+ use(x)
+ return x
+ """
+ )
+ assert ("x", 3) not in dead
+
+
+def test_finally_reads_a_handler_write_before_the_handler_returns():
+ dead = _dead(
+ """
+ def f(g, use):
+ x = 0
+ try:
+ x = g()
+ except Exception:
+ if g():
+ x = 1
+ return None
+ x = 2
+ finally:
+ use(x)
+ return x
+ """
+ )
+ assert ("x", 8) not in dead
+
+
+def test_finally_reads_an_else_write_before_the_else_returns():
+ dead = _dead(
+ """
+ def f(g, use):
+ x = 0
+ try:
+ y = g()
+ except Exception:
+ y = None
+ else:
+ if y:
+ x = 2
+ return y
+ x = 3
+ finally:
+ use(x)
+ return x
+ """
+ )
+ assert ("x", 10) not in dead
diff --git a/tests/unit/health/test_dataflow_langs.py b/tests/unit/health/test_dataflow_langs.py
index 08ef87481..5128b18d1 100644
--- a/tests/unit/health/test_dataflow_langs.py
+++ b/tests/unit/health/test_dataflow_langs.py
@@ -2097,10 +2097,16 @@ def test_go_name_declared_afresh_after_the_span_does_not_refuse_it():
def _rust_in_out(src: str, s: int, e: int):
from repowise.core.analysis.health.dataflow import slice as slicing
- def_use = _first("rust", src).def_use
+ fn = _first("rust", src)
+ def_use = fn.def_use
def_lines, use_lines = slicing._var_lines(def_use)
return slicing._infer_in_out(
- def_lines, use_lines, s, e, slicing._declared_before_read(def_use)
+ def_lines,
+ use_lines,
+ s,
+ e,
+ slicing._declared_before_read(def_use),
+ reads=slicing._reads_observing(fn, def_lines),
)
diff --git a/tests/unit/health/test_extract_method_live_out.py b/tests/unit/health/test_extract_method_live_out.py
index efe2a0f9d..ed60360e3 100644
--- a/tests/unit/health/test_extract_method_live_out.py
+++ b/tests/unit/health/test_extract_method_live_out.py
@@ -631,3 +631,289 @@ function f(xs: string[], ys: string[], out: string[]): void {
)
writes = _marked(src, "W")
assert any(x.start_line <= writes[0] <= x.end_line for x in _spans("typescript", "ts", src))
+
+
+# A later write on a path that need not run does not hide the span's value:
+# an ``else`` arm (hermes ``api_server.py``) or a ``try`` body that may raise
+# before its write (hermes ``moa_loop.py``).
+_LATER_WRITE_MAY_NOT_RUN = [
+ (
+ "python",
+ "py",
+ """
+def f(a, b, c):
+ if a:
+ x = b + 1 # W
+ if b > c:
+ log(b)
+ log(c)
+ log(a)
+ else:
+ x = c
+ return x # R
+""",
+ ),
+ (
+ "typescript",
+ "ts",
+ """
+function f(a: number, b: number, c: number): number {
+ let x = 0;
+ if (a > 0) {
+ x = b + 1; // W
+ if (b > c) {
+ log(b);
+ }
+ log(c);
+ log(a);
+ } else {
+ x = c;
+ }
+ return x; // R
+}
+""",
+ ),
+ (
+ "go",
+ "go",
+ """
+package m
+func f(a int, b int, c int) int {
+ x := 0
+ if a > 0 {
+ x = b + 1 // W
+ if b > c {
+ log(b)
+ }
+ log(c)
+ log(a)
+ } else {
+ x = c
+ }
+ return x // R
+}
+""",
+ ),
+ (
+ "java",
+ "java",
+ """
+class M {
+ int f(int a, int b, int c) {
+ int x = 0;
+ if (a > 0) {
+ x = b + 1; // W
+ if (b > c) {
+ log(b);
+ }
+ log(c);
+ log(a);
+ } else {
+ x = c;
+ }
+ return x; // R
+ }
+}
+""",
+ ),
+ (
+ "python",
+ "py",
+ """
+def f(a, b):
+ x = None # W
+ if a:
+ log(a)
+ log(b)
+ log(a)
+ try:
+ y = g(a)
+ x = y
+ except Exception:
+ pass
+ return x # R
+""",
+ ),
+ (
+ "typescript",
+ "ts",
+ """
+function f(a: number, b: number): number {
+ let x = 0;
+ x = a + 1; // W
+ if (a > b) {
+ log(a);
+ }
+ log(b);
+ log(a);
+ try {
+ const y = g(a);
+ x = y;
+ } catch (err) {
+ log(err);
+ }
+ return x; // R
+}
+""",
+ ),
+ (
+ "java",
+ "java",
+ """
+class M {
+ int f(int a, int b) {
+ int x = 0;
+ x = a + 1; // W
+ if (a > b) {
+ log(a);
+ }
+ log(b);
+ log(a);
+ try {
+ int y = g(a);
+ x = y;
+ } catch (Exception err) {
+ log(err);
+ }
+ return x; // R
+ }
+}
+""",
+ ),
+ (
+ # ``finally`` runs even when ``g`` raises before the body's write.
+ "python",
+ "py",
+ """
+def f(a, b):
+ x = None # W
+ if a:
+ log(a)
+ log(b)
+ log(a)
+ log(b)
+ try:
+ x = g(a)
+ finally:
+ log(x) # R
+ return x
+""",
+ ),
+ (
+ # The write sits deep in the ``try`` body; the handler reads it.
+ "python",
+ "py",
+ """
+def f(a, b):
+ x = 0
+ try:
+ for i in a:
+ log(i)
+ x = b + 1 # W
+ if b:
+ log(b)
+ log(a)
+ log(b)
+ x = g(a)
+ except Exception:
+ log(x) # R
+ return x
+""",
+ ),
+ (
+ "typescript",
+ "ts",
+ """
+function f(a: number[], b: number): number {
+ let x = 0;
+ try {
+ for (const i of a) {
+ log(i);
+ }
+ x = b + 1; // W
+ if (b) {
+ log(b);
+ }
+ log(a);
+ log(b);
+ x = g(a);
+ } catch (err) {
+ log(x); // R
+ }
+ return x;
+}
+""",
+ ),
+]
+
+
+ ("language", "ext", "src"),
+ _LATER_WRITE_MAY_NOT_RUN,
+ ids=[
+ "py-else",
+ "ts-else",
+ "go-else",
+ "java-else",
+ "py-try",
+ "ts-try",
+ "java-try",
+ "py-try-finally",
+ "py-try-inner",
+ "ts-try-inner",
+ ],
+)
+def test_write_reaching_past_a_later_conditional_write_is_returned(
+ language: str, ext: str, src: str
+):
+ leaking, returning = _leaks(language, ext, "x", src)
+ assert leaking == []
+ assert returning, "the span holding the write should still be offered, returning x"
+
+
+def test_closure_read_sees_the_value_reaching_its_creation():
+ """hermes ``api_server.py``: the read after the span sits in a closure made
+ after the ``if`` / ``else`` join, so the ``else`` arm's write does not
+ hide the span's from it."""
+ src = textwrap.dedent(
+ """
+def f(a, b, c):
+ if a:
+ x = b + 1 # W
+ if b > c:
+ log(b)
+ log(c)
+ log(a)
+ else:
+ x = c
+ def run():
+ return x # R
+ return run
+"""
+ )
+ leaking, returning = _leaks("python", "py", "x", src)
+ assert leaking == []
+ assert returning
+
+
+def test_closure_read_no_statement_covers_keeps_the_line_rule():
+ """A closure read the CFG places nowhere still counts, by the nearest def
+ above it: dropping it could only drop a return."""
+ src = textwrap.dedent(
+ """
+def f(a, b, c):
+ x = 0
+ x = b + 1 # W
+ if b > c:
+ log(b)
+ log(c)
+ log(a)
+ def run():
+ return x # R
+ return run
+"""
+ )
+ with mock.patch.object(slicer, "_statements_by_line", lambda cfg: {}):
+ leaking, returning = _leaks("python", "py", "x", src)
+ assert leaking == []
+ assert returning
diff --git a/tests/unit/health/test_perf_promotion.py b/tests/unit/health/test_perf_promotion.py
index 51a5e0492..a0f661f41 100644
--- a/tests/unit/health/test_perf_promotion.py
+++ b/tests/unit/health/test_perf_promotion.py
@@ -338,3 +338,36 @@ def test_line_outside_any_loop_is_not_independent():
""",
"HIT",
)
+
+
+def test_a_write_deeper_in_the_try_body_does_not_prove_more():
+ # The handler is reachable from every block of the body, so a write after
+ # a nested branch is still skipped on the exception path.
+ assert not _independent(
+ """
+ async def run(items):
+ for item in items:
+ try:
+ if item.flag:
+ log(item)
+ key = item.id
+ except AttributeError:
+ pass
+ r = await fetch(key) # HIT
+ """,
+ "HIT",
+ )
+ assert _independent(
+ """
+ async def run(items):
+ for item in items:
+ try:
+ if item.flag:
+ log(item)
+ key = item.id
+ except AttributeError:
+ key = 0
+ r = await fetch(key) # HIT
+ """,
+ "HIT",
+ )
diff --git a/tests/unit/health/test_refactoring_extract_method.py b/tests/unit/health/test_refactoring_extract_method.py
index 32053d441..e166c94a8 100644
--- a/tests/unit/health/test_refactoring_extract_method.py
+++ b/tests/unit/health/test_refactoring_extract_method.py
@@ -202,6 +202,7 @@ def _find_extractions_reference(analysis, lmap):
_infer_in_out,
_loop_carry_free,
_outs_definitely_assigned,
+ _reads_observing,
_Scan,
_sorted,
_span_metrics,
@@ -222,6 +223,7 @@ def _find_extractions_reference(analysis, lmap):
body_nloc = _stmts_nloc(body_container.named_children, lines)
def_lines, use_lines = _var_lines(analysis.def_use)
declared_first = _declared_before_read(analysis.def_use)
+ reads = _reads_observing(analysis, def_lines)
decision_kinds = (
lmap.branch_kinds
| lmap.loop_kinds
@@ -283,7 +285,9 @@ def _find_extractions_reference(analysis, lmap):
continue
s = span[0].start_point[0] + 1
e = span[-1].end_point[0] + 1
- params, returns = _infer_in_out(def_lines, use_lines, s, e, declared_first)
+ params, returns = _infer_in_out(
+ def_lines, use_lines, s, e, declared_first, reads=reads
+ )
if len(params) > _MAX_PARAMS or len(returns) > _MAX_RETURNS:
continue
if not _outs_definitely_assigned(span, returns, def_lines, lmap):
@@ -1218,10 +1222,11 @@ def test_a_refused_span_yields_only_to_a_disjoint_one(monkeypatch):
def _span_in_out(src: str, s: int, e: int):
from repowise.core.analysis.health.dataflow import slice as slicer
- def_use = _first(src).def_use
- def_lines, use_lines = slicer._var_lines(def_use)
- declared_first = slicer._declared_before_read(def_use)
- return slicer._infer_in_out(def_lines, use_lines, s, e, declared_first)
+ fn = _first(src)
+ def_lines, use_lines = slicer._var_lines(fn.def_use)
+ declared_first = slicer._declared_before_read(fn.def_use)
+ reads = slicer._reads_observing(fn, def_lines)
+ return slicer._infer_in_out(def_lines, use_lines, s, e, declared_first, reads=reads)
def test_a_comprehension_binder_is_not_a_parameter():