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.

feat(dataflow): record error exits and keep Extract Method cuts from changing exception flow (#3488)

Raghav Chamadiyad6489606e4c8
diff --git a/docs/layers/REFACTORING.md b/docs/layers/REFACTORING.md
index bb816d2e6..76e3aa21a 100644
--- a/docs/layers/REFACTORING.md
+++ b/docs/layers/REFACTORING.md
@@ -236,7 +236,10 @@ Most plans answer a health finding, so per-path marker rules in
(every returned value written on every path, no state carried across loop
iterations, no local a closure shares across the span's edge, no write to a
`global` name without its declaration). A staged plan also leaves out a stage
- whose inputs or outputs depend on a `switch` or `match` arm's write. Spans it
+ whose inputs or outputs depend on a `switch` or `match` arm's write. A span or
+ stage never changes which handler catches an error or what it sees: it is not
+ offered when it would split a `try` from its handlers, write a value a handler
+ or `finally` reads, or move a Go `defer`. Spans it
cannot 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.
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py b/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
index 838591697..dad3e34db 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/error_handling.py
@@ -31,7 +31,11 @@ _EH_BLOCK_KINDS = frozenset({"block", "statement_block", "compound_statement"})
_EH_TRIVIAL_STMT = frozenset({"comment", "pass_statement", "line_comment", "block_comment"})
# Rust: each of these is a latent panic-on-error.
_RUST_UNWRAP_METHODS = frozenset({"unwrap", "expect", "unwrap_unchecked"})
-_RUST_PANIC_MACROS = frozenset({"panic", "unreachable", "todo", "unimplemented"})
+#: Shared with the dataflow pass's error exits (``dataflow.raises``).
+RUST_PANIC_MACROS = frozenset({"panic", "unreachable", "todo", "unimplemented"})
+#: Python handler types that catch every ordinary exception; only the second
+#: also catches the interpreter's exit signals.
+PY_CATCH_ALL = ("Exception", "BaseException")
def _eh_text(node: Node) -> str:
@@ -103,7 +107,7 @@ def _eh_except_catch_all_name(clause: Node) -> tuple[bool, str | None]:
if first.type == "as_pattern":
named = [c for c in first.children if c.is_named]
target = named[0] if named else first
- if target.type == "identifier" and _eh_text(target) in ("Exception", "BaseException"):
+ if target.type == "identifier" and _eh_text(target) in PY_CATCH_ALL:
return True, _eh_text(target)
return False, None
@@ -121,7 +125,7 @@ def _eh_catches_base(clause: Node) -> bool:
provably cannot, so it is *broad* rather than truly catch-all.
"""
is_catch_all, name = _eh_except_catch_all_name(clause)
- return is_catch_all and name != "Exception"
+ return is_catch_all and name != PY_CATCH_ALL[0]
def _eh_rust_attr_is_test(attr_text: str) -> bool:
@@ -176,7 +180,7 @@ def _eh_rust_hit(node: Node) -> bool:
return rust_unwrap.method_call_parts(node)[0] in _RUST_UNWRAP_METHODS
if node.type == "macro_invocation":
mac = node.child_by_field_name("macro")
- return mac is not None and _eh_text(mac) in _RUST_PANIC_MACROS
+ return mac is not None and _eh_text(mac) in RUST_PANIC_MACROS
return False
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/analyze.py b/packages/core/src/repowise/core/analysis/health/dataflow/analyze.py
index aab828dcc..272eddf7f 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/analyze.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/analyze.py
@@ -85,7 +85,7 @@ def analyze_function(
if dialect is None:
return None
try:
- cfg = build_cfg(fn_node, lmap)
+ cfg = build_cfg(fn_node, lmap, dialect=dialect)
except CFGGuardTrippedError:
return None
def_use = compute_def_use(cfg, fn_node, lmap, dialect)
@@ -131,7 +131,7 @@ def analyze_file(
continue
try:
- cfg = build_cfg(fn_node, lmap)
+ cfg = build_cfg(fn_node, lmap, dialect=dialect)
except CFGGuardTrippedError:
result.stats.guard_tripped += 1
continue
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 ac1723ce8..762c5ad8f 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/cfg.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/cfg.py
@@ -39,18 +39,29 @@ silence), the same precision-first default as the def/use dialect.
handlers / ``finally`` it can raise into from the state before it and from each
of its blocks. Those block edges are ``raise_edges``, since an exception can
leave a block mid-way: in ``x = 1; y = g(); x = 2`` the handler sees ``x = 1``.
+The same walk records each ``try``'s clauses and each error exit with the
+handler that catches it (:mod:`raises`).
"""
from __future__ import annotations
-from dataclasses import dataclass, field
+from dataclasses import dataclass, field, replace
from typing import TYPE_CHECKING
from ..complexity.languages import LanguageNodeMap
+from .raises import (
+ Handler,
+ RaiseSite,
+ TryRegion,
+ catches,
+ error_exit,
+)
if TYPE_CHECKING:
from tree_sitter import Node
+ from .dialects.base import DefUseDialect
+
# Upper bound on basic blocks per function. A structured CFG has roughly one
# block per branch/loop arm, so a few hundred is already an extreme function;
# the cap is a backstop against pathological generated code, not a real limit.
@@ -144,9 +155,14 @@ class CFG:
function_start_line: int = 0
# ``(src, dst)`` edges an exception can take from inside *src*, mid-block.
raise_edges: frozenset[tuple[int, int]] = frozenset()
+ # Error exits, ``try`` statements and Go ``defer`` lines, in source order.
+ raises: tuple[RaiseSite, ...] = ()
+ try_regions: tuple[TryRegion, ...] = ()
+ defers: tuple[int, ...] = ()
def __post_init__(self) -> None:
self._by_id: dict[int, BasicBlock] = {b.id: b for b in self.blocks}
+ self._at: dict[int, tuple[int, int]] | None = None
@property
def entry(self) -> BasicBlock:
@@ -159,6 +175,17 @@ class CFG:
def block(self, block_id: int) -> BasicBlock:
return self._by_id[block_id]
+ def statement_at(self, line: int) -> tuple[int, int] | None:
+ """``(block id, index)`` of the narrowest statement holding *line*;
+ None when no statement does. Built on first use."""
+ if self._at is None:
+ best: dict[int, tuple[int, int, int]] = {}
+ for b in self.blocks:
+ for k, st in enumerate(b.statements):
+ _narrowest(best, st, b.id, k)
+ self._at = {ln: (bid, k) for ln, (_w, bid, k) in best.items()}
+ return self._at.get(line)
+
def successors(self, block: BasicBlock) -> list[BasicBlock]:
return [self._by_id[s] for s in block.successors]
@@ -188,6 +215,15 @@ class CFG:
return out
+def _narrowest(best: dict[int, tuple[int, int, int]], st: Statement, bid: int, k: int) -> None:
+ """Keep *st* (statement *k* of block *bid*) on each of its lines where it
+ is narrower than what *best* holds."""
+ width = st.end_line - st.start_line
+ for ln in range(st.start_line, st.end_line + 1):
+ if ln not in best or width < best[ln][0]:
+ best[ln] = (width, bid, k)
+
+
@dataclass
class _LoopCtx:
"""Jump targets for ``break`` / ``continue`` within the enclosing loop."""
@@ -203,14 +239,27 @@ class _CFGBuilder:
mutable state (the block list, the id counter, and the loop-target stack).
"""
- def __init__(self, lmap: LanguageNodeMap, *, max_blocks: int) -> None:
+ def __init__(
+ self, lmap: LanguageNodeMap, *, max_blocks: int, dialect: DefUseDialect | None = None
+ ) -> None:
self.lmap = lmap
+ self.dialect = dialect
self.max_blocks = max_blocks
self.blocks: list[BasicBlock] = []
self._next_id = 0
self._loops: list[_LoopCtx] = []
self.exit_block: BasicBlock | None = None
self._raise_edges: set[tuple[int, int]] = set()
+ # Handlers of each ``try`` whose body is being built, innermost last,
+ # and the handler clauses being built: what a raise there meets.
+ self._guards: list[tuple[Handler, ...]] = []
+ self._handling: list[Handler] = []
+ self._raises: list[RaiseSite] = []
+ self._regions: list[TryRegion] = []
+ self._defers: list[int] = []
+ self._jump_kinds = (
+ lmap.return_kinds | lmap.raise_kinds | lmap.break_kinds | lmap.continue_kinds
+ )
# -- low-level graph ops --------------------------------------------------
@@ -245,6 +294,7 @@ class _CFGBuilder:
self._raises_to(region, dst)
def _record_stmt(self, block: BasicBlock, node: Node) -> None:
+ self._record_exit(node)
block.statements.append(
Statement(node.type, node.start_point[0] + 1, node.end_point[0] + 1, node=node)
)
@@ -286,6 +336,10 @@ class _CFGBuilder:
``doc_comment``), the same idiom nloc counting uses."""
if block is None:
return []
+ # A C-family arm without braces is the statement itself
+ # (``if (x) throw e;``): its children are not statements.
+ if block.type in self._jump_kinds:
+ return [block]
kids = [c for c in block.named_children if "comment" not in c.type]
while len(kids) == 1 and kids[0].type in self.lmap.block_kinds:
kids = [c for c in kids[0].named_children if "comment" not in c.type]
@@ -307,6 +361,9 @@ class _CFGBuilder:
entry_id=entry.id,
exit_id=self.exit_block.id,
raise_edges=frozenset(self._raise_edges),
+ raises=tuple(sorted(self._raises, key=lambda r: r.line)),
+ try_regions=tuple(sorted(self._regions, key=lambda r: r.start_line)),
+ defers=tuple(self._defers),
)
def _process_seq(self, stmts: list[Node], cur: BasicBlock | None) -> BasicBlock | None:
@@ -354,6 +411,39 @@ class _CFGBuilder:
self._record_stmt(cur, st)
return cur
+ # -- error exits ----------------------------------------------------------
+
+ def _record_exit(self, st: Node) -> None:
+ """Record *st* as an error exit (or a ``defer``) when it is one."""
+ if st.type == "defer_statement":
+ self._defers.append(st.start_point[0] + 1)
+ return
+ if st.type in self.lmap.switch_kinds:
+ self._record_arm_raises(st)
+ return
+ found = error_exit(st, self.lmap, self._handling[-1] if self._handling else None)
+ if found is None:
+ return
+ kind, exc, raised = found
+ caught = None
+ if kind in ("raise", "rethrow"):
+ guards = (h for hs in reversed(self._guards) for h in hs)
+ caught = next((h.start_line for h in guards if catches(h.types, raised)), None)
+ self._raises.append(RaiseSite(st.start_point[0] + 1, kind, exc, caught))
+
+ def _record_arm_raises(self, switch: Node) -> None:
+ """The raises in a ``switch`` / ``match`` the graph keeps whole. The
+ statement is one graph node, so its arms are walked here, once, and
+ only within it: nested functions and lambdas are not entered."""
+ scopes = self.lmap.function_kinds | self.lmap.lambda_kinds
+ stack = list(reversed(switch.named_children))
+ while stack:
+ node = stack.pop()
+ if node.type in self.lmap.raise_kinds:
+ self._record_exit(node)
+ elif node.type not in scopes:
+ stack.extend(reversed(node.named_children))
+
def _build_if(self, if_node: Node, cur: BasicBlock) -> BasicBlock:
cur.kind = "branch"
self._record_head(cur, if_node)
@@ -524,12 +614,15 @@ class _CFGBuilder:
# 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.
+ clauses = [c for c in try_node.children if c.type in self.lmap.catch_kinds]
+ heads = [self._handler(c) for c in clauses]
first = len(self.blocks)
body_entry = self._new()
self._edge(cur, body_entry)
- body_out = self._process_seq(
- self._body_stmts(try_node.child_by_field_name("body")), body_entry
- )
+ body = try_node.child_by_field_name("body")
+ self._guards.append(tuple(heads))
+ body_out = self._process_seq(self._body_stmts(body), body_entry)
+ self._guards.pop()
protected = self.blocks[first:]
if finally_clause is not None:
self._protect(cur, protected, normal_join)
@@ -549,23 +642,71 @@ class _CFGBuilder:
elif body_out is not None:
self._edge(body_out, normal_join)
- for handler in (c for c in try_node.children if c.type in self.lmap.catch_kinds):
+ handlers = self._build_handlers(clauses, heads, cur, protected, normal_join, finally_clause)
+ parts = (body, else_clause, clauses, finally_clause)
+ self._regions.append(_try_region(try_node, parts, handlers))
+ return join
+
+ def _build_handlers(
+ self,
+ clauses: list[Node],
+ heads: list[Handler],
+ cur: BasicBlock,
+ protected: list[BasicBlock],
+ normal_join: BasicBlock,
+ finally_clause: Node | None,
+ ) -> tuple[Handler, ...]:
+ """Each handler clause of a ``try``, reached from its *protected*
+ body; returns them with whether each can complete normally."""
+ handlers = []
+ for handler, head in zip(clauses, heads, strict=True):
first = len(self.blocks)
handler_entry = self._new("handler")
# The clause head (``except E as e:``) reads the exception type and
# binds its name; the dialect says which (``handler_def_use``).
self._record_head(handler_entry, handler)
self._protect(cur, protected, handler_entry)
+ self._handling.append(head)
handler_out = self._process_seq(
self._body_stmts(self._block_of(handler)), handler_entry
)
+ self._handling.pop()
+ handlers.append(replace(head, completes=handler_out is not None))
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._raises_to(self.blocks[first:], normal_join)
-
- return join
+ return tuple(handlers)
+
+ def _handler(self, clause: Node) -> Handler:
+ """What *clause* names and binds, from the language's dialect; with
+ none, a handler whose types match no raise."""
+ types, name = self.dialect.handler_head(clause) if self.dialect else ((), None)
+ return Handler(clause.start_point[0] + 1, clause.end_point[0] + 1, types, name)
+
+
+def _try_region(
+ try_node: Node,
+ parts: tuple[Node | None, Node | None, list[Node], Node | None],
+ handlers: tuple[Handler, ...],
+) -> TryRegion:
+ """*try_node*'s lines and its clauses': *parts* is its body, ``else``,
+ handler clauses and ``finally``."""
+ body, else_clause, clauses, finally_clause = parts
+ nodes = [body, else_clause, *clauses, finally_clause]
+ return TryRegion(
+ try_node.start_point[0] + 1,
+ try_node.end_point[0] + 1,
+ _lines(body) or (0, 0),
+ tuple(x for x in map(_lines, nodes) if x is not None),
+ handlers,
+ _lines(finally_clause),
+ )
+
+
+def _lines(node: Node | None) -> tuple[int, int] | None:
+ return (node.start_point[0] + 1, node.end_point[0] + 1) if node is not None else None
def build_cfg(
@@ -573,6 +714,7 @@ def build_cfg(
lmap: LanguageNodeMap,
*,
max_blocks: int = _DEFAULT_MAX_BLOCKS,
+ dialect: DefUseDialect | None = None,
) -> CFG:
"""Build the control-flow graph for one function AST node.
@@ -580,6 +722,8 @@ def build_cfg(
``_collect_function_nodes`` yields); *lmap* is its language's
``LanguageNodeMap``. Raises :class:`CFGGuardTrippedError` if the function
exceeds *max_blocks* -- callers building over a corpus should catch it and
- skip the function (the :mod:`gating` harness does).
+ skip the function (the :mod:`gating` harness does). *dialect* says what
+ each exception handler names (``handler_head``); without it no raise is
+ counted as caught.
"""
- return _CFGBuilder(lmap, max_blocks=max_blocks).build(fn_node)
+ return _CFGBuilder(lmap, max_blocks=max_blocks, dialect=dialect).build(fn_node)
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.py
index 9e6504d85..d75c811b0 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
@@ -149,6 +149,10 @@ def mentions_receiver(node: Node, names: frozenset[str]) -> bool:
return re.search(rf"(?<![\w$])(?:{words})(?![\w$])", text) is not None
+#: ``(types, bound name)`` of an exception handler (:meth:`handler_head`).
+HandlerHead = tuple[tuple[str, ...] | None, str | None]
+
+
@runtime_checkable
class DefUseDialect(Protocol):
"""The contract a per-language def/use dialect satisfies."""
@@ -161,6 +165,8 @@ class DefUseDialect(Protocol):
def parameter_defs(self, fn_node: Node) -> tuple[Occurrence, ...]: ...
+ def handler_head(self, handler: Node) -> HandlerHead: ...
+
class BaseDefUseDialect:
"""Shared machinery for concrete dialects.
@@ -472,6 +478,12 @@ class BaseDefUseDialect:
reading every language had before handler heads were recorded."""
return StatementDefUse(defs=(), uses=())
+ def handler_head(self, handler: Node) -> HandlerHead:
+ """The exception types a handler clause names, as written (None: it
+ catches everything), and the name it binds. Default: types no raise
+ can be matched against, so nothing is counted as caught."""
+ return (), None
+
def receiver(self, fn_node: Node, lmap: LanguageNodeMap) -> Receiver | None:
"""The instance *fn_node* runs on (:class:`Receiver`), ``NO_RECEIVER``
for a plain function, None when this language cannot tell.
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.py
index b23361487..85e144820 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
@@ -41,6 +41,7 @@ from typing import TYPE_CHECKING
from .base import (
NO_RECEIVER,
BaseDefUseDialect,
+ HandlerHead,
Occurrence,
Receiver,
StatementDefUse,
@@ -99,6 +100,16 @@ class CppDefUseDialect(BaseDefUseDialect):
def _is_scope_boundary(self, node: Node) -> bool:
return node.type in _SCOPE_BOUNDARIES
+ def handler_head(self, handler: Node) -> HandlerHead:
+ """``catch (const E& e)`` names ``E``; ``catch (...)`` catches
+ everything. The bound name is not needed: C++ re-raises with ``throw;``."""
+ params = handler.child_by_field_name("parameters")
+ decl = next(
+ (c for c in params.named_children if c.type == "parameter_declaration"), None
+ ) if params is not None else None
+ kind = decl.child_by_field_name("type") if decl is not None else None
+ return ((node_text(kind),) if kind is not None else None), None
+
def collect_reads(self, node: Node | None, out: list[Occurrence]) -> None:
"""Like the base collector, but a ``call_expression`` contributes only
its receiver and arguments — never the called function's name."""
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py
index 4617f3cb4..e5faafdcb 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/csharp.py
@@ -26,7 +26,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
-from .base import BaseDefUseDialect, Occurrence, StatementDefUse
+from .base import BaseDefUseDialect, HandlerHead, Occurrence, StatementDefUse, node_text
if TYPE_CHECKING:
from tree_sitter import Node
@@ -79,6 +79,18 @@ class CSharpDefUseDialect(BaseDefUseDialect):
def _is_scope_boundary(self, node: Node) -> bool:
return node.type in _SCOPE_BOUNDARIES
+ def handler_head(self, handler: Node) -> HandlerHead:
+ """``catch (E e)`` names ``E`` and binds ``e``; a bare ``catch``
+ catches everything. A ``when`` filter may decline, so a filtered
+ clause names no type a raise is matched against."""
+ decl = next((c for c in handler.named_children if c.type == "catch_declaration"), None)
+ name = decl.child_by_field_name("name") if decl is not None else None
+ bound = node_text(name) if name is not None else None
+ if any(c.type == "catch_filter_clause" for c in handler.named_children):
+ return (), bound
+ kind = decl.child_by_field_name("type") if decl is not None else None
+ return ((node_text(kind),) if kind is not None else None), bound
+
def collect_reads(self, node: Node | None, out: list[Occurrence]) -> None:
"""Collect variable reads under *node*.
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.py
index 7f57e3cee..44cf9aff4 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
@@ -24,7 +24,15 @@ from __future__ import annotations
from typing import TYPE_CHECKING
-from .base import NO_RECEIVER, BaseDefUseDialect, Occurrence, Receiver, StatementDefUse
+from .base import (
+ NO_RECEIVER,
+ BaseDefUseDialect,
+ HandlerHead,
+ Occurrence,
+ Receiver,
+ StatementDefUse,
+ node_text,
+)
if TYPE_CHECKING:
from tree_sitter import Node
@@ -64,6 +72,17 @@ class JavaDefUseDialect(BaseDefUseDialect):
def _is_scope_boundary(self, node: Node) -> bool:
return node.type in _SCOPE_BOUNDARIES
+ def handler_head(self, handler: Node) -> HandlerHead:
+ """``catch (A | B e)`` names ``A`` and ``B`` and binds ``e``."""
+ kids = handler.named_children
+ param = next((c for c in kids if c.type == "catch_formal_parameter"), None)
+ if param is None:
+ return (), None
+ kinds = next((c for c in param.named_children if c.type == "catch_type"), None)
+ types = tuple(node_text(t) for t in kinds.named_children) if kinds else ()
+ name = param.child_by_field_name("name")
+ return types, node_text(name) if name is not None else None
+
def collect_reads(self, node: Node | None, out: list[Occurrence]) -> None:
"""Like the base collector, but a ``method_invocation`` / ``method_reference``
contributes only its receiver and arguments -- never the method name."""
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py
index 1cc56bd9d..ea7f4d6f7 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/python.py
@@ -24,9 +24,11 @@ from .base import (
NO_RECEIVER,
BaseDefUseDialect,
Captured,
+ HandlerHead,
Occurrence,
Receiver,
StatementDefUse,
+ node_text,
)
if TYPE_CHECKING:
@@ -108,6 +110,20 @@ class PythonDefUseDialect(BaseDefUseDialect):
self._with_item(handler, defs, uses)
return StatementDefUse(defs=tuple(defs), uses=tuple(uses))
+ def handler_head(self, handler: Node) -> HandlerHead:
+ """``except (A, b.C) as e:`` names ``A`` and ``b.C`` and binds ``e``;
+ a bare ``except:`` catches everything."""
+ value = handler.child_by_field_name("value")
+ if value is None:
+ return None, None
+ alias = None
+ if value.type == "as_pattern":
+ alias = value.child_by_field_name("alias")
+ value = value.named_children[0]
+ grouped = value.type in ("tuple", "parenthesized_expression")
+ names = value.named_children if grouped else [value]
+ return tuple(node_text(n) for n in names), node_text(alias) if alias else None
+
def _own_receiver(self, fn_node: Node, lmap: LanguageNodeMap) -> Receiver | None:
"""A method's first parameter when it is ``self`` / ``cls`` and the
function sits in a class body; a nested function reaches ``self`` as a
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py
index c30904dc7..22687e474 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/dialects/ts_js.py
@@ -19,7 +19,14 @@ from __future__ import annotations
from typing import TYPE_CHECKING
-from .base import BaseDefUseDialect, Occurrence, Receiver, StatementDefUse
+from .base import (
+ BaseDefUseDialect,
+ HandlerHead,
+ Occurrence,
+ Receiver,
+ StatementDefUse,
+ node_text,
+)
if TYPE_CHECKING:
from tree_sitter import Node
@@ -131,6 +138,12 @@ class TsJsDefUseDialect(BaseDefUseDialect):
self._declare(defs, 0, param)
return StatementDefUse(defs=tuple(defs), uses=())
+ def handler_head(self, handler: Node) -> HandlerHead:
+ """A ``catch`` catches everything; it binds a plain parameter's name."""
+ param = handler.child_by_field_name("parameter")
+ named = param is not None and param.type == "identifier"
+ return None, node_text(param) if named else None
+
def declared_type(self, name: Node) -> str | None:
"""The base rule, plus ``| undefined`` for an optional parameter
(``x?: T`` holds ``T | undefined`` until something narrows it)."""
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/raises.py b/packages/core/src/repowise/core/analysis/health/dataflow/raises.py
new file mode 100644
index 000000000..bf9fbc8e4
--- /dev/null
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/raises.py
@@ -0,0 +1,313 @@
+"""Error exits of a function and the handlers that catch them.
+
+Recorded by the CFG builder as it walks each statement (:mod:`cfg`), so no
+extra tree walk: per ``try`` its clauses and handlers (:class:`TryRegion`),
+per error exit its line, kind, exception and the handler that catches it
+(:class:`RaiseSite`). Kinds: ``raise`` (``raise`` / ``throw``), ``rethrow``
+(a bare ``raise`` / ``throw;``, or raising the handler's own name),
+``panic`` (Go ``panic(...)``, Rust ``panic!`` and kin) and ``error_return``
+(Rust ``?``, ``return Err(...)``, ``bail!``). Go's ``return ..., err`` is
+not one: nothing in the syntax marks the value as an error.
+
+``caught_by`` is best effort. A handler catches a raise when it names no
+type, names the raised type (by last name segment), or names a root type that
+catches every ordinary exception. Subclass relations are not known, so a
+handler naming a base class of the raised type is missed and the raise reads
+as propagating; a filtered handler (C# ``catch (E) when (...)``) may decline,
+so it is never counted as catching. What a handler names comes from the
+language's dialect (``handler_head``).
+
+:func:`exception_flow_refusal` is the Extract Method precondition built on
+these facts: a span may not change which handler sees an exception or what
+state it sees.
+"""
+
+from __future__ import annotations
+
+from dataclasses import dataclass
+from typing import TYPE_CHECKING
+
+from ..complexity.error_handling import PY_CATCH_ALL, RUST_PANIC_MACROS
+from .dialects.base import node_text
+
+if TYPE_CHECKING:
+ from tree_sitter import Node
+
+ from ..complexity.languages import LanguageNodeMap
+ from .cfg import CFG
+
+# Why a span or stage was not offered (``StagePlan.refused``).
+TRY_SPLIT = "try_split"
+EXCEPTION_STATE = "exception_state"
+DEFER_MOVED = "defer_moved"
+
+# Handler types that catch every ordinary exception (Python / C# ``Exception``,
+# Java ``Throwable``, C++ ``std::exception``). Python's ``Exception`` does not
+# catch the interpreter's exit signals; ``BaseException`` catches everything.
+_ROOTS = frozenset({PY_CATCH_ALL[0], "Throwable", "exception"})
+_EXIT_SIGNALS = frozenset({"SystemExit", "KeyboardInterrupt", "GeneratorExit"})
+
+
+@dataclass(frozen=True)
+class Handler:
+ """One ``except`` / ``catch`` clause. ``types`` None: it catches
+ everything. ``completes``: some path leaves it normally (it swallows)."""
+
+ start_line: int
+ end_line: int
+ types: tuple[str, ...] | None
+ name: str | None
+ completes: bool = True
+
+
+@dataclass(frozen=True)
+class TryRegion:
+ """One ``try`` statement: its lines, its body's, each clause's (body,
+ ``else``, handlers, ``finally``) and its handlers."""
+
+ start_line: int
+ end_line: int
+ body: tuple[int, int]
+ clauses: tuple[tuple[int, int], ...]
+ handlers: tuple[Handler, ...]
+ finally_lines: tuple[int, int] | None = None
+
+ def exposed_to(self, s: int, e: int) -> tuple[list[tuple[int, int]], bool] | None:
+ """Where an exception raised in lines *s*..*e* is handled inside this
+ ``try``: the clauses that run next, and whether a handler that
+ swallows it lets the code after the ``try`` run. None when the lines
+ are not inside one clause of it. From the body: the handlers, the
+ ``finally``, and past a swallowing handler. From a handler or
+ ``else``: the ``finally``. From the ``finally``: nothing here."""
+ clause = next(((lo, hi) for lo, hi in self.clauses if lo <= s and e <= hi), None)
+ if clause is None:
+ return None
+ after_finally = [self.finally_lines] if self.finally_lines is not None else []
+ if clause == self.body:
+ return [(h.start_line, h.end_line) for h in self.handlers] + after_finally, True
+ return ([] if clause == self.finally_lines else after_finally), False
+
+
+@dataclass(frozen=True)
+class RaiseSite:
+ """One error exit. ``exception`` is the raised type or value as written
+ (None when unknown); ``caught_by`` the line of the handler in this
+ function that catches it, None when it leaves the function."""
+
+ line: int
+ kind: str
+ exception: str | None
+ caught_by: int | None
+
+
+def _segment(name: str) -> str:
+ """``errors.NotFound`` / ``std::runtime_error`` / ``List<X>`` -> last name."""
+ return name.split("<", 1)[0].replace("::", ".").rsplit(".", 1)[-1].strip(" &*")
+
+
+def catches(types: tuple[str, ...] | None, raised: tuple[str, ...] | None) -> bool:
+ """Whether a handler naming *types* catches an exception of *raised*
+ (None: unknown, a value rather than a class). Best effort: no subclass
+ relations, and a filtered handler (``types == ()``) never catches."""
+ if types is None:
+ return True
+ names = {_segment(t) for t in types}
+ if PY_CATCH_ALL[1] in names:
+ return True
+ if raised is None:
+ return bool(names & _ROOTS)
+ segs = {_segment(r) for r in raised}
+ return bool(names & segs) or (bool(names & _ROOTS) and not segs & _EXIT_SIGNALS)
+
+
+def raised_exception(node: Node) -> str | None:
+ """What a ``raise`` / ``throw`` statement raises, as written: the class
+ of a construction, else the expression; None for a bare re-raise."""
+ value = next((c for c in node.named_children if "comment" not in c.type), None)
+ if value is None:
+ return None
+ for field in ("constructor", "type", "function"):
+ inner = value.child_by_field_name(field)
+ if inner is not None:
+ return node_text(inner)
+ return node_text(value)
+
+
+def exit_kind(node: Node, lmap: LanguageNodeMap) -> str | None:
+ """The error-exit kind of a statement that is not a ``raise``: a panic or
+ an explicit error return (``return Err(...)``); None for any other."""
+ if node.type in lmap.return_kinds:
+ value = next(iter(node.named_children), None)
+ fn = value.child_by_field_name("function") if value is not None else None
+ return "error_return" if fn is not None and node_text(fn) == "Err" else None
+ if node.type == "expression_statement" and node.named_child_count == 1:
+ node = node.named_children[0]
+ if node.type == "call_expression": # Go ``panic(...)``
+ fn = node.child_by_field_name("function")
+ return "panic" if fn is not None and node_text(fn) == "panic" else None
+ if node.type in lmap.exit_macro_kinds:
+ return _macro_exit(node, lmap)
+ return None
+
+
+def _macro_exit(node: Node, lmap: LanguageNodeMap) -> str | None:
+ """Rust: a panic-family macro, or one that returns an error (``bail!``)."""
+ macro = node.child_by_field_name("macro")
+ name = _segment(node_text(macro)) if macro is not None else ""
+ if name in RUST_PANIC_MACROS:
+ return "panic"
+ return "error_return" if name in lmap.exit_macro_names else None
+
+
+def error_exit(
+ st: Node, lmap: LanguageNodeMap, handler: Handler | None
+) -> tuple[str, str | None, tuple[str, ...] | None] | None:
+ """``(kind, exception, raised types)`` of *st* when it is an error exit,
+ *handler* being the clause it sits in; None otherwise. A raised value
+ (lowercase, not dotted) has unknown types."""
+ if st.type == "try_expression":
+ return "error_return", None, None
+ if st.type not in lmap.raise_kinds:
+ kind = exit_kind(st, lmap)
+ return (kind, None, None) if kind is not None else None
+ exc = raised_exception(st)
+ if exc is None or (handler is not None and exc == handler.name):
+ types = handler.types if handler is not None else None
+ return "rethrow", " | ".join(types) if types else None, types
+ known = exc[:1].isupper() or "." in exc or ":" in exc
+ return "raise", exc, (exc,) if known else None
+
+
+def raises_in(cfg: CFG, start: int, end: int) -> tuple[RaiseSite, ...]:
+ """The error exits on lines *start*..*end*."""
+ return tuple(r for r in cfg.raises if start <= r.line <= end)
+
+
+def exception_flow_refusal(
+ cfg: CFG,
+ s: int,
+ e: int,
+ reads: dict[str, list[tuple[int, frozenset[int]]]],
+ def_lines: dict[str, list[int]],
+) -> str | None:
+ """Why lifting lines *s*..*e* into a helper would change exception flow,
+ or None. :data:`DEFER_MOVED`: the lines hold a Go ``defer``, which in a
+ helper runs when the helper returns and recovers only its panics.
+ :data:`TRY_SPLIT`: they cut a ``try`` apart from a handler or ``finally``,
+ or re-raise outside the handler they sit in. :data:`EXCEPTION_STATE`: they
+ write a value that code an exception from them reaches may read (a
+ handler, a ``finally``, or the code after a ``try`` whose handler swallows
+ it, at any nesting depth); a helper that raises loses its writes, so that
+ code would see the value from before. A raise that only propagates is
+ unchanged: the helper re-raises it."""
+ if any(s <= line <= e for line in cfg.defers):
+ return DEFER_MOVED
+ if _splits_a_try(cfg, s, e):
+ return TRY_SPLIT
+ for region in cfg.try_regions:
+ if not region.start_line <= s <= e <= region.end_line or (
+ s == region.start_line and e == region.end_line
+ ):
+ continue
+ exposed = region.exposed_to(s, e)
+ if exposed is not None and _state_seen(cfg, region, exposed, s, e, reads, def_lines):
+ return EXCEPTION_STATE
+ return None
+
+
+def _splits_a_try(cfg: CFG, s: int, e: int) -> bool:
+ """Whether lines *s*..*e* re-raise outside their handler, or hold part of
+ a ``try`` but not one whole clause of it."""
+ rethrows = [r.line for r in raises_in(cfg, s, e) if r.kind == "rethrow"]
+ if any(not _inside(_handler_of(cfg, line), s, e) for line in rethrows):
+ return True
+ return any(
+ not (region.end_line < s or e < region.start_line)
+ and not (s <= region.start_line and region.end_line <= e)
+ and region.exposed_to(s, e) is None
+ for region in cfg.try_regions
+ )
+
+
+def _handler_of(cfg: CFG, line: int) -> tuple[int, int] | None:
+ """The lines of the innermost handler holding *line*."""
+ held = [
+ (h.start_line, h.end_line)
+ for region in cfg.try_regions
+ for h in region.handlers
+ if h.start_line <= line <= h.end_line
+ ]
+ return max(held, default=None)
+
+
+def _inside(lines: tuple[int, int] | None, s: int, e: int) -> bool:
+ return lines is None or (s <= lines[0] and lines[1] <= e)
+
+
+def _state_seen(
+ cfg: CFG,
+ region: TryRegion,
+ exposed: tuple[list[tuple[int, int]], bool],
+ s: int,
+ e: int,
+ reads: dict[str, list[tuple[int, frozenset[int]]]],
+ def_lines: dict[str, list[int]],
+) -> bool:
+ """Whether a write on lines *s*..*e* that code in them may follow (and so
+ raise after) is read where *exposed* (:meth:`TryRegion.exposed_to`) says
+ an exception from them is handled in *region*."""
+ clauses, past = exposed
+ for var, entries in reads.items():
+ for line, seen in entries:
+ hit = any(lo <= line <= hi for lo, hi in clauses) or (
+ past and line > region.end_line and _swallowed(region, def_lines.get(var, ()))
+ )
+ if hit and any(s <= d <= e and _followed(cfg, d, s, e) for d in seen):
+ return True
+ return False
+
+
+def _swallowed(region: TryRegion, defs: list[int]) -> bool:
+ """Whether a handler of *region* completes without writing the name."""
+ return any(
+ h.completes and not any(h.start_line <= d <= h.end_line for d in defs)
+ for h in region.handlers
+ )
+
+
+def _followed(cfg: CFG, line: int, s: int, e: int) -> bool:
+ """Whether some path runs another statement of lines *s*..*e* after the
+ statement writing on *line* (in the flow graph), before leaving them."""
+ at = cfg.statement_at(line)
+ if at is None:
+ return True
+ block, k = at
+ rest = cfg.block(block).statements[k + 1 :]
+ if rest:
+ return s <= rest[0].start_line <= e
+ seen: set[int] = set()
+ stack = list(cfg.block(block).successors)
+ while stack:
+ bid = stack.pop()
+ if bid in seen or bid == cfg.exit_id:
+ continue
+ seen.add(bid)
+ nxt = cfg.block(bid)
+ if not nxt.statements:
+ stack.extend(nxt.successors)
+ elif s <= nxt.statements[0].start_line <= e:
+ return True
+ return False
+
+
+__all__ = [
+ "DEFER_MOVED",
+ "EXCEPTION_STATE",
+ "TRY_SPLIT",
+ "Handler",
+ "RaiseSite",
+ "TryRegion",
+ "catches",
+ "exception_flow_refusal",
+ "raises_in",
+]
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 9ca4f5fdd..6f665ca3b 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/slice.py
@@ -37,6 +37,7 @@ 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 .raises import exception_flow_refusal
from .reaching import definitions_at, observed_definitions
if TYPE_CHECKING:
@@ -151,6 +152,7 @@ def find_extractions(
lmap: LanguageNodeMap,
receiver: Receiver | None = None,
prefixes: dict[int, _Prefix] | None = None,
+ refused: set[str] | None = None,
) -> list[Extraction]:
"""Return safe Extract Method candidates for *analysis*, best first.
@@ -160,6 +162,9 @@ def find_extractions(
the function's instance (``DefUseDialect.receiver``), None when unknown.
*prefixes*, when given, receives each walked block's :class:`_Prefix` by
block id, so a staged plan reads them instead of walking again.
+ *refused*, when given, receives why a span that cleared every other gate
+ was dropped because lifting it would change exception flow
+ (:func:`raises.exception_flow_refusal`).
"""
fn_node = analysis.fn_node
if fn_node is None:
@@ -266,6 +271,11 @@ def find_extractions(
span, loop, s, e, carry_defs, use_lines, lmap
):
continue
+ reason = exception_flow_refusal(analysis.cfg, s, e, reads, def_lines)
+ if reason is not None:
+ if refused is not None:
+ refused.add(reason)
+ continue
uses, assigns = _receiver_facts(receiver, pre, i, j, _count_in(free_writes, s, e))
out.append(
Extraction(
diff --git a/packages/core/src/repowise/core/analysis/health/dataflow/stages.py b/packages/core/src/repowise/core/analysis/health/dataflow/stages.py
index 9043b9f34..f308915f1 100644
--- a/packages/core/src/repowise/core/analysis/health/dataflow/stages.py
+++ b/packages/core/src/repowise/core/analysis/health/dataflow/stages.py
@@ -23,7 +23,9 @@ into K contiguous stages, each a helper the function then calls in sequence.
at the call. A name a stage declares in a nested block (a loop's ``const``)
is the stage's own, never an output. A guard (``if not x: raise``) whose
names the code after the stage reads stays in the function: type checkers
- narrow ``x`` from it.
+ narrow ``x`` from it. A stage may not change exception flow
+ (:func:`raises.exception_flow_refusal`): inside a ``try`` body it may not
+ write a value a handler or ``finally`` reads.
- **Cuts.** A small dynamic program over the statement boundaries: lift as many
decision points as possible, then prefer narrow interfaces (fewest names
crossing a cut), cuts at a banner comment, a ``timed()`` label or a blank
@@ -55,6 +57,7 @@ from typing import TYPE_CHECKING
from ..complexity.nloc import is_string_stmt
from .assigned import DefiniteAssignment
from .loop_cut import carried, escapes
+from .raises import exception_flow_refusal
from .reaching import observed_definitions
from .slice import (
_MIN_SLICE_NLOC,
@@ -125,7 +128,8 @@ class StagePlan:
``moved_imports`` are those nothing left in the function reads.
``reason`` says why there are no stages; None when there are.
``refused`` names why a stage that cleared every other check was not
- offered (:data:`OUTER_WRITE`, :data:`SWITCH_ARM_WRITE`)."""
+ offered (:data:`OUTER_WRITE`, :data:`SWITCH_ARM_WRITE`, and the
+ exception-flow reasons of :mod:`raises`)."""
stages: tuple[Extraction, ...] = ()
context: tuple[str, ...] = ()
@@ -357,6 +361,7 @@ class _Cutter:
self.arm_writes = facts.arm_writes
self.refused = facts.refused
self.reads = facts.reads
+ self.cfg = facts.analysis.cfg
self.pre = pre
self._assigns: dict[tuple[int, str], bool] = {}
self.cut_cost = _cut_costs(code)
@@ -509,9 +514,10 @@ class _Cutter:
) -> bool:
"""The single-span slicer's own refusals: a hoisted binding, a
declaration the code after the stage still needs, a local a closure
- shares across the stage's edge. Then the two a stage names in
+ shares across the stage's edge. Then those a stage names in
``refused``: a ``global`` write that cannot take its declaration
- along, a ``switch`` / ``match`` arm write the interface depends on."""
+ along, a cut that changes exception flow (:mod:`raises`), a
+ ``switch`` / ``match`` arm write the interface depends on."""
if any(s <= first_def <= e and first_use < s for first_def, first_use in self.hoisted):
return False
if _declaration_escapes(
@@ -532,6 +538,9 @@ class _Cutter:
) -> str | None:
if _outer_write_escapes(s, e, self.outer, self.def_lines):
return OUTER_WRITE
+ reason = exception_flow_refusal(self.cfg, s, e, self.reads, self.def_lines)
+ if reason is not None:
+ return reason
if self._arm_write_crosses(s, e, params, returns):
return SWITCH_ARM_WRITE
return None
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/extract_method.py b/packages/core/src/repowise/core/analysis/health/refactoring/extract_method.py
index 1a3e94159..546a54cf4 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/extract_method.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/extract_method.py
@@ -207,7 +207,9 @@ class ExtractMethodDetector(RefactoringDetector):
receiver = dialect.receiver(fn_node, lmap) if dialect and fn_node else None
# The slicer's per-block walk, read again by a staged plan.
prefixes: dict[int, Any] = {}
- candidates = find_extractions(analysis, lmap, receiver, prefixes)
+ refused: set[str] = set()
+ candidates = find_extractions(analysis, lmap, receiver, prefixes, refused)
+ _mark_stage_refused(matched, frozenset(refused))
best = _choose(analysis, candidates, markers)
staged = _staged(analysis, best, lmap, receiver, ctx.language, matched, prefixes)
scope = _scope(fn_node, files) if staged is not None or best is not None else None
@@ -363,8 +365,9 @@ class ExtractMethodDetector(RefactoringDetector):
REFUSED_DETAIL = "extract_method_refused"
-#: The finding detail naming why a stage that cleared every other check was
-#: not offered (``StagePlan.refused``), sorted.
+#: The finding detail naming why a stage or span that cleared every other
+#: check was not offered (``StagePlan.refused``, the slicer's ``refused``),
+#: sorted.
STAGE_REFUSED_DETAIL = "extract_method_stage_refused"
@@ -374,7 +377,8 @@ def _mark_stage_refused(findings: list[Any], reasons: frozenset[str]) -> None:
for f in findings:
details = getattr(f, "details", None)
if isinstance(details, dict):
- details[STAGE_REFUSED_DETAIL] = sorted(reasons)
+ held = set(details.get(STAGE_REFUSED_DETAIL, ()))
+ details[STAGE_REFUSED_DETAIL] = sorted(held | reasons)
def _mark_refused(findings: list[Any], reason: str) -> None:
diff --git a/tests/unit/health/test_dataflow_raises.py b/tests/unit/health/test_dataflow_raises.py
new file mode 100644
index 000000000..d93774121
--- /dev/null
+++ b/tests/unit/health/test_dataflow_raises.py
@@ -0,0 +1,358 @@
+"""Error exits on the dataflow pass, and the Extract Method precondition on them."""
+
+from __future__ import annotations
+
+import textwrap
+
+import pytest
+
+from repowise.core.analysis.health.dataflow import analyze_file
+from repowise.core.analysis.health.dataflow.raises import (
+ DEFER_MOVED,
+ EXCEPTION_STATE,
+ TRY_SPLIT,
+ exception_flow_refusal,
+ raises_in,
+)
+
+
+def _cfg(language: str, src: str):
+ from repowise.core.ingestion.parser import _get_language
+
+ if _get_language(language) is None:
+ pytest.skip(f"tree-sitter language pack missing for {language}")
+ res = analyze_file("m", language, textwrap.dedent(src).encode(), flagged_only=False)
+ if not res.functions:
+ pytest.skip(f"no dataflow for {language}")
+ return res.functions[0].cfg
+
+
+def _facts(language: str, src: str) -> list[tuple[int, str, str | None, int | None]]:
+ return [(r.line, r.kind, r.exception, r.caught_by) for r in _cfg(language, src).raises]
+
+
+def test_python_raises_rethrows_and_the_handlers_that_catch_them():
+ src = """
+ def f(x):
+ try:
+ if x:
+ raise ValueError("a")
+ raise KeyError
+ except (ValueError, errors.Other) as e:
+ raise
+ except LookupError as d:
+ raise d
+ except Exception:
+ raise RuntimeError() from None
+ raise exc
+ """
+ assert _facts("python", src) == [
+ (5, "raise", "ValueError", 7),
+ # No subclass relations: KeyError reaches the root handler.
+ (6, "raise", "KeyError", 11),
+ (8, "rethrow", "ValueError | errors.Other", None),
+ (10, "rethrow", "LookupError", None),
+ (12, "raise", "RuntimeError", None),
+ # A raised value has no known type; nothing encloses it anyway.
+ (13, "raise", "exc", None),
+ ]
+ (region,) = _cfg("python", src).try_regions
+ assert [(h.start_line, h.types, h.name, h.completes) for h in region.handlers] == [
+ (7, ("ValueError", "errors.Other"), "e", False),
+ (9, ("LookupError",), "d", False),
+ (11, ("Exception",), None, False),
+ ]
+
+
+def test_python_exit_signals_pass_a_root_handler():
+ src = """
+ def f():
+ try:
+ raise SystemExit(1)
+ except Exception:
+ pass
+ """
+ assert _facts("python", src) == [(4, "raise", "SystemExit", None)]
+
+
+def test_braceless_ts_throw_is_an_error_exit_a_catch_sees():
+ src = """
+ function f(x) {
+ try {
+ if (!x) throw new Error("a");
+ g();
+ } catch (err) {
+ throw err;
+ }
+ throw new TypeError("b");
+ }
+ """
+ assert _facts("typescript", src) == [
+ (4, "raise", "Error", 6),
+ (7, "rethrow", None, None),
+ (9, "raise", "TypeError", None),
+ ]
+
+
+def test_a_raise_in_a_switch_arm_is_recorded_and_a_closure_raise_is_not():
+ src = """
+ function f(k) {
+ try {
+ switch (k) {
+ case 1:
+ throw new RangeError("a");
+ default:
+ g(() => { throw new Error("inner"); });
+ }
+ } catch (e) {}
+ }
+ """
+ assert _facts("typescript", src) == [(6, "raise", "RangeError", 10)]
+
+
+def test_java_union_catch_and_csharp_bare_rethrow():
+ java = """
+ class A { void f() {
+ try { if (x) throw new IOException("a"); }
+ catch (IOException | RuntimeException e) { throw e; }
+ } }
+ """
+ assert _facts("java", java) == [
+ (3, "raise", "IOException", 4),
+ (4, "rethrow", "IOException | RuntimeException", None),
+ ]
+ csharp = """
+ class A { void F() {
+ try { throw new ArgumentException("x"); }
+ catch (InvalidOperationException e) { throw; }
+ catch { }
+ } }
+ """
+ assert _facts("csharp", csharp) == [
+ (3, "raise", "ArgumentException", 5),
+ (4, "rethrow", "InvalidOperationException", None),
+ ]
+
+
+def test_go_panic_and_rust_error_returns():
+ go = """
+ package p
+ func f(x bool) {
+ defer g()
+ if x {
+ panic("x")
+ }
+ }
+ """
+ cfg = _cfg("go", go)
+ assert [(r.line, r.kind) for r in cfg.raises] == [(6, "panic")]
+ assert cfg.defers == (4,)
+ rust = """
+ fn f(x: bool) -> Result<(), E> {
+ g()?;
+ if x {
+ return Err(E::new());
+ }
+ panic!("x");
+ }
+ """
+ assert [(r.line, r.kind) for r in _cfg("rust", rust).raises] == [
+ (3, "error_return"),
+ (5, "error_return"),
+ (7, "panic"),
+ ]
+
+
+_TRY = """
+def f(a, b):
+ x = 0
+ try:
+ x = b + 1
+ log(a)
+ log(b)
+ except Exception:
+ log(x)
+ raise
+ return x
+"""
+
+
+def _refusal(src: str, s: int, e: int) -> str | None:
+ from repowise.core.analysis.health.dataflow.slice import _reads_observing, _var_lines
+
+ res = analyze_file("m.py", "python", textwrap.dedent(src).encode(), flagged_only=False)
+ fa = res.functions[0]
+ def_lines, _ = _var_lines(fa.def_use)
+ return exception_flow_refusal(fa.cfg, s, e, _reads_observing(fa, def_lines), def_lines)
+
+
+def test_a_write_the_handler_reads_stays_under_the_try():
+ # Lines 5-7: a raise from ``log`` would leave the handler the old ``x``.
+ assert _refusal(_TRY, 5, 7) == EXCEPTION_STATE
+ # The write as the span's last line: nothing after it in the span raises.
+ assert _refusal(_TRY, 6, 7) is None
+ # The whole try moves with its handler.
+ assert _refusal(_TRY, 3, 10) is None
+
+
+def test_a_write_last_on_its_path_is_not_refused():
+ src = """
+ def f(a, b):
+ x = 0
+ try:
+ if a:
+ x = g(a)
+ else:
+ log(b)
+ except Exception:
+ log(x)
+ raise
+ return x
+ """
+ assert _refusal(src, 5, 8) is None
+
+
+def test_a_cut_through_a_try_or_a_rethrow_outside_its_handler_is_refused():
+ assert _refusal(_TRY, 5, 9) == TRY_SPLIT
+ assert _refusal(_TRY, 9, 10) == TRY_SPLIT
+
+
+def test_a_raise_that_only_propagates_is_kept():
+ src = """
+ def f(a):
+ x = g(a)
+ if not x:
+ raise ValueError(a)
+ y = h(x)
+ return y
+ """
+ assert _refusal(src, 3, 6) is None
+ res = analyze_file("m.py", "python", textwrap.dedent(src).encode(), flagged_only=False)
+ assert [r.kind for r in raises_in(res.functions[0].cfg, 3, 6)] == ["raise"]
+
+
+def test_a_go_span_holding_defer_is_refused_and_counted():
+ from repowise.core.analysis.health.complexity.languages import get_language_map
+ from repowise.core.analysis.health.dataflow.slice import find_extractions
+
+ src = """
+ package p
+ func f(a []int, b int) int {
+ n := 0
+ mu.Lock()
+ defer mu.Unlock()
+ for _, v := range a {
+ if v > b {
+ n += v
+ }
+ }
+ if n > 10 {
+ n = 10
+ }
+ log(n)
+ return n
+ }
+ """
+ cfg = _cfg("go", src)
+ assert cfg.defers == (6,)
+ res = analyze_file("m.go", "go", textwrap.dedent(src).encode(), flagged_only=False)
+ refused: set[str] = set()
+ spans = find_extractions(res.functions[0], get_language_map("go"), refused=refused)
+ assert not [x for x in spans if x.start_line <= 6 <= x.end_line]
+ assert refused == {DEFER_MOVED}
+
+
+def test_python_handler_types_come_from_the_dialect():
+ src = """
+ def f():
+ try:
+ g()
+ except Foo:
+ pass
+ except:
+ pass
+ """
+ (region,) = _cfg("python", src).try_regions
+ assert [h.types for h in region.handlers] == [("Foo",), None]
+
+
+def test_a_csharp_filtered_catch_is_not_counted_as_catching():
+ src = """
+ class A { void F() {
+ try { throw new InvalidOperationException("x"); }
+ catch (InvalidOperationException e) when (e.Message == "y") { }
+ } }
+ """
+ assert _facts("csharp", src) == [(3, "raise", "InvalidOperationException", None)]
+
+
[email protected]("language", ["typescript", "java", "csharp", "cpp"])
+def test_a_braceless_return_leaves_the_if_without_reaching_its_join(language):
+ src = {
+ "typescript": "function f(x) {\n if (x) return;\n g();\n}\n",
+ "java": "class A { void f(int x) {\n if (x > 0) return;\n g();\n} }\n",
+ "csharp": "class A { void F(int x) {\n if (x > 0) return;\n G();\n} }\n",
+ "cpp": "void f(int x) {\n if (x) return;\n g();\n}\n",
+ }[language]
+ cfg = _cfg(language, src)
+ (arm,) = [b for b in cfg.blocks if any(s.start_line == 2 and not s.is_head for s in b.statements)]
+ assert arm.statements[-1].kind == "return_statement"
+ assert arm.successors == [cfg.exit_id]
+
+
+_NESTED = """
+def f(a, b):
+ x = 0
+ try:
+ try:
+ g(a)
+ except ValueError:
+ x = b + 1
+ log(a)
+ log(b)
+ finally:
+ log(x)
+ return x
+"""
+
+
+def test_a_span_in_an_inner_handler_keeps_a_write_the_outer_finally_reads():
+ # The span sits in the inner handler, not a ``try`` body: a raise from
+ # ``log`` after the write would leave the outer ``finally`` the old ``x``.
+ assert _refusal(_NESTED, 8, 10) == EXCEPTION_STATE
+ assert _refusal(_NESTED, 9, 10) is None
+
+
+def test_a_span_in_an_else_clause_keeps_a_write_its_finally_reads():
+ src = """
+ def f(a, b):
+ x = 0
+ try:
+ g(a)
+ except ValueError:
+ return 0
+ else:
+ x = b + 1
+ log(a)
+ finally:
+ log(x)
+ return x
+ """
+ assert _refusal(src, 9, 10) == EXCEPTION_STATE
+
+
+def test_a_span_in_a_handler_nested_in_a_swallowing_try_keeps_a_write_read_after():
+ src = """
+ def f(a, b):
+ x = 0
+ try:
+ try:
+ g(a)
+ except ValueError:
+ x = b + 1
+ log(a)
+ except Exception:
+ pass
+ return x
+ """
+ assert _refusal(src, 8, 9) == EXCEPTION_STATE
diff --git a/tests/unit/health/test_extract_method_live_out.py b/tests/unit/health/test_extract_method_live_out.py
index ed60360e3..e7aa9bf03 100644
--- a/tests/unit/health/test_extract_method_live_out.py
+++ b/tests/unit/health/test_extract_method_live_out.py
@@ -799,6 +799,36 @@ def f(a, b):
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",
+ ],
+)
+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"
+
+
+# The write sits deep in the ``try`` body, code that may raise follows it, and
+# the handler reads it: a helper that raises loses the write, so the handler
+# would see the value from before. No span holding the write and that code is
+# offered; the refusal is counted.
+_WRITE_SEEN_BY_HANDLER = [
(
# The write sits deep in the ``try`` body; the handler reads it.
"python",
@@ -847,28 +877,21 @@ function f(a: number[], b: number): number {
]
- ("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"
[email protected](("language", "ext", "src"), _WRITE_SEEN_BY_HANDLER, ids=["py", "ts"])
+def test_write_a_handler_reads_is_not_lifted_away_from_it(language: str, ext: str, src: str):
+ src = textwrap.dedent(src)
+ (write,) = _marked(src, "W")
+ res = analyze_file(f"m.{ext}", language, src.encode(), flagged_only=False)
+ refused: set[str] = set()
+ with mock.patch.object(slicer, "_MAX_BODY_SHARE", float("inf")):
+ spans = slicer.find_extractions(
+ res.functions[0], get_language_map(language), refused=refused
+ )
+ (region,) = res.functions[0].cfg.try_regions
+ lo, hi = region.body
+ inside = [x for x in spans if lo <= x.start_line <= write < x.end_line <= hi]
+ assert not inside
+ assert refused == {"exception_state"}
def test_closure_read_sees_the_value_reaching_its_creation():
diff --git a/tests/unit/health/test_extract_method_span_interface.py b/tests/unit/health/test_extract_method_span_interface.py
index 05aaa0978..f617073e7 100644
--- a/tests/unit/health/test_extract_method_span_interface.py
+++ b/tests/unit/health/test_extract_method_span_interface.py
@@ -482,3 +482,41 @@ def test_a_handler_rebinding_a_name_in_a_loop_carries_nothing():
return results
"""
assert any(x.start_line == 8 for x in _spans(src, "install"))
+
+
+_TRY_FN = """
+def run(items, a, b, {opts}):
+ seen = []
+ last = None
+ done = 0
+ try:
+ for item in items:
+{body}
+ last = item
+ except Exception:
+ report(done)
+ raise
+ return seen, last
+"""
+
+
+def test_a_stage_writing_what_a_handler_reads_is_refused_and_counted():
+ # A helper that raises loses its writes, so the handler would see the
+ # count from before the stage instead of the one the original reached.
+ def src(extra: str) -> str:
+ body = "".join(_item_section(n, extra=extra if n == 3 else "") for n in range(8))
+ return _TRY_FN.format(
+ opts=", ".join(f"opt_{n}" for n in range(8)),
+ body=textwrap.indent(textwrap.dedent(body), " " * 12),
+ )
+
+ hazard = src("\n done = done + 1")
+ assert "exception_state" in _refused(hazard)["run"]
+ stages = _plans(hazard)["run"].plan["stages"]
+ lines = textwrap.dedent(hazard).splitlines()
+ write = next(i for i, ln in enumerate(lines, 1) if "done = done + 1" in ln)
+ after = next(i for i, ln in enumerate(lines, 1) if 'item.kind == "k4"' in ln)
+ assert len(stages) >= 2
+ # A stage may end on the write (nothing after it can raise), not run past it.
+ assert not any(s["span"]["start"] <= write and after <= s["span"]["end"] for s in stages)
+ assert _refused(src(""))["run"] == []
diff --git a/tests/unit/health/test_refactoring_extract_method.py b/tests/unit/health/test_refactoring_extract_method.py
index f16f49945..6ad8e83f4 100644
--- a/tests/unit/health/test_refactoring_extract_method.py
+++ b/tests/unit/health/test_refactoring_extract_method.py
@@ -1040,7 +1040,7 @@ def test_a_best_span_below_the_floor_yields_to_the_next_one(monkeypatch):
trivial = Extraction(10, 17, ("a",), (), slice_nloc=8, ccn_removed=1)
worth = Extraction(20, 27, ("b",), (), slice_nloc=8, ccn_removed=3)
- monkeypatch.setattr(extract_method, "find_extractions", lambda _a, _l, _r=None, _p=None: [trivial, worth])
+ monkeypatch.setattr(extract_method, "find_extractions", lambda _a, _l, _r=None, _p=None, _rf=None: [trivial, worth])
# No parsed source to anchor against: this pins span choice, not anchoring.
monkeypatch.setattr(extract_method, "anchor_plan", lambda _plan, _fn: True)
fn = _Shape(12, 40)
@@ -1255,7 +1255,7 @@ def test_a_refused_span_yields_only_to_a_disjoint_one(monkeypatch):
shrunk = Extraction(2, 55, ("a",), (), slice_nloc=40, ccn_removed=8) # overlaps it
disjoint = Extraction(85, 95, ("b",), (), slice_nloc=10, ccn_removed=3)
monkeypatch.setattr(
- extract_method, "find_extractions", lambda _a, _l, _r=None, _p=None: [whole, shrunk, disjoint]
+ extract_method, "find_extractions", lambda _a, _l, _r=None, _p=None, _rf=None: [whole, shrunk, disjoint]
)
monkeypatch.setattr(extract_method, "anchor_plan", lambda _plan, _fn: True)
analysis = type(