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(perf): report string concat in a loop only when it grows across iterations (#2984)

Raghav Chamadiyaf3dfe136db49
diff --git a/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py b/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py
index c29ee3b07..9f0815875 100644
--- a/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py
+++ b/packages/core/src/repowise/core/analysis/health/biomarkers/string_concat_in_loop.py
@@ -8,11 +8,13 @@ compiler-optimized and not flagged.
Precision-first: the walker's perf pass flags this ONLY when the RHS is provably
a string literal / template (``s += "..."`` / ``s += f"{x}"`` / ``s += "a" + b``),
never an opaque ``s += chunk`` that could be numeric. This detector lifts the
-pre-collected hits into findings.
+pre-collected hits into findings, except in test files: a test or benchmark that
+builds its expected text or input data with ``+=`` is not code anyone speeds up.
"""
from __future__ import annotations
+from ..coverage import is_test_file
from ..models import Severity
from .base import BiomarkerResult, FileContext
@@ -25,6 +27,8 @@ class StringConcatInLoopDetector:
def detect(self, ctx: FileContext) -> list[BiomarkerResult]:
out: list[BiomarkerResult] = []
+ if is_test_file(ctx.file_path):
+ return out
for hit in ctx.perf_hits:
if hit.kind != _KIND:
continue
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
index 673c91bcc..2779ea451 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/base.py
@@ -349,6 +349,8 @@ class BasePerfDialect:
branch_kinds: frozenset[str] = frozenset()
exit_kinds: frozenset[str] = frozenset()
scope_kinds: frozenset[str] = frozenset()
+ # Statements a ``break`` leaves without leaving the loop around them.
+ switch_kinds: frozenset[str] = frozenset()
# Methods that grow a sequence in call order (``append`` / ``push``).
sequence_appends: frozenset[str] = frozenset()
@@ -507,6 +509,98 @@ class BasePerfDialect:
stack.extend(n.children)
return False
+ def accumulates_across_iterations(
+ self, node: Node, name: bytes, loop_kinds: frozenset[str]
+ ) -> bool:
+ """True when the ``+=`` at *node* can grow *name* across iterations of
+ an enclosing loop in its own function.
+
+ The loops are walked from the innermost out. One whose body re-binds
+ *name* (:meth:`binds_name`) bounds the growth to a single pass, so
+ neither it nor any loop outside it is accumulating. An append directly
+ followed by ``return`` / ``throw`` runs once per call, and one followed
+ by a ``break`` once per run of the innermost loop: the interrupted-message
+ and fallback shapes (``msg += "[interrupted]"; break``). With no loop
+ found inside the function the walker's own loop verdict stands.
+ """
+ exit_kind = self._exit_after(node, loop_kinds)
+ if exit_kind not in (None, "break_statement"):
+ return False
+ loops = self._enclosing_loops(node, loop_kinds)
+ if not loops:
+ return True
+ for depth, loop in enumerate(loops):
+ if self._rebinds(loop, node, name, loop_kinds):
+ return False
+ if depth or exit_kind is None:
+ return True
+ return False
+
+ def _enclosing_loops(self, node: Node, loop_kinds: frozenset[str]) -> list[Node]:
+ """The loops around *node* inside its own function, innermost first."""
+ loops: list[Node] = []
+ cur = node.parent
+ while cur is not None and cur.type not in self.scope_kinds:
+ if cur.type in loop_kinds:
+ loops.append(cur)
+ cur = cur.parent
+ return loops
+
+ def _rebinds(self, loop: Node, append: Node, name: bytes, loop_kinds: frozenset[str]) -> bool:
+ """*loop*'s body re-binds *name* whenever *append* runs.
+
+ A binding under a branch or a nested loop that does not also hold the
+ append (``if (first) s = "head"``) may not run, so it is no reset.
+ """
+ body = self.loop_body(loop) or loop
+ return any(
+ self.binds_name(n, name) and self._runs_with(n, append, body, loop_kinds)
+ for n in self._walk(body, self.scope_kinds)
+ )
+
+ def _runs_with(self, node: Node, append: Node, body: Node, loop_kinds: frozenset[str]) -> bool:
+ """*node* runs on every pass that runs *append*: climbing from *node*, a
+ block holding *append* is reached before any branch or nested loop."""
+ cur = node.parent
+ while cur is not None and cur != body:
+ if cur.type in self.branch_kinds or cur.type in loop_kinds:
+ return False
+ if self._within(cur, append):
+ return True
+ cur = cur.parent
+ return True
+
+ def _exit_after(self, node: Node, loop_kinds: frozenset[str]) -> str | None:
+ """The kind of exit statement later in the block of the statement holding
+ *node*, or ``None``; a ``break`` that only leaves a ``switch`` is none."""
+ parent = node.parent
+ stmt = parent if parent is not None and parent.type == "expression_statement" else node
+ exit_stmt = self._next_exit(stmt)
+ if exit_stmt is None or exit_stmt.type == "continue_statement":
+ return None
+ if exit_stmt.type == "break_statement" and self._in_switch(stmt, loop_kinds):
+ return None
+ return exit_stmt.type
+
+ def _next_exit(self, stmt: Node) -> Node | None:
+ """The first exit statement after *stmt* in its block, unless a statement
+ in between can ``continue`` the loop instead."""
+ sib = stmt.next_named_sibling
+ while sib is not None and sib.type not in self.exit_kinds:
+ if any(n.type == "continue_statement" for n in self._walk(sib, self.scope_kinds)):
+ return None
+ sib = sib.next_named_sibling
+ return sib
+
+ def _in_switch(self, stmt: Node, loop_kinds: frozenset[str]) -> bool:
+ """A ``switch`` sits between *stmt* and its nearest loop."""
+ cur = stmt.parent
+ while cur is not None and cur.type not in loop_kinds:
+ if cur.type in self.switch_kinds:
+ return True
+ cur = cur.parent
+ return False
+
def _rhs_is_stringish(self, node: Node) -> bool:
"""True if an augmented-assignment's RHS is provably string-typed.
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py
index 33e216aff..dbb69ab3c 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/ts_js.py
@@ -68,6 +68,11 @@ TS_SINK_METHODS: frozenset[str] = (
_TS_STRING_KINDS: frozenset[str] = frozenset({"string", "template_string"})
_TS_AUG_ASSIGN_KINDS: frozenset[str] = frozenset({"augmented_assignment_expression"})
+# The walker's TS/JS loop kinds (``complexity.languages``), which cannot be
+# imported here without a cycle through the complexity package.
+_TS_LOOP_KINDS: frozenset[str] = frozenset(
+ {"for_statement", "for_in_statement", "for_of_statement", "while_statement", "do_statement"}
+)
# A function/lambda scope: the boundary for the reaching-assignment walk and the
# "not a loop exit" skip for break/return/throw inside a nested closure (a
@@ -200,6 +205,29 @@ class TsJsPerfDialect(TsJsMarkerHooks):
string_literal_kinds = _TS_STRING_KINDS
aug_assign_kinds = _TS_AUG_ASSIGN_KINDS
+ switch_kinds = frozenset({"switch_statement"})
+
+ def is_string_concat(self, node: Node) -> bool:
+ """``s += "x"`` that can grow ``s`` across loop iterations.
+
+ A ``let`` declared or freshly assigned inside the loop body is bounded
+ per pass, and an append right before ``break`` / ``return`` / ``throw``
+ runs once. A member or subscript target (``buf[0] += ...``) keeps the
+ flag: nothing here re-binds it.
+ """
+ if not super().is_string_concat(node):
+ return False
+ left = node.child_by_field_name("left")
+ if left is None or left.type != "identifier" or left.text is None:
+ return True
+ return self.accumulates_across_iterations(node, left.text, _TS_LOOP_KINDS)
+
+ def binds_name(self, node: Node, name: bytes) -> bool:
+ # ``s = s + part`` keeps accumulating; only a value not built from ``s`` resets it.
+ value = _assigned_value(node, name)
+ return value is not None and not any(
+ n.type == "identifier" and n.text == name for n in self._walk(value)
+ )
# Only ``for ... of`` / ``for ... in`` multiply over a collection. C-style
# ``for (;;)``, ``while`` and ``do`` are cursors (pagination / polling), so
diff --git a/tests/unit/health/test_perf_dialects.py b/tests/unit/health/test_perf_dialects.py
index a803676c1..8e83920bc 100644
--- a/tests/unit/health/test_perf_dialects.py
+++ b/tests/unit/health/test_perf_dialects.py
@@ -560,6 +560,67 @@ def test_python_string_concat_reset_per_iteration_not_flagged():
assert ("string_concat_in_loop", "") in _hits("python", accum)
+def _ts_concat_lines(src: str) -> list[int]:
+ fc = walk_file("t.ts", "typescript", src.encode())
+ return [h.line for h in fc.perf_hits if h.kind == "string_concat_in_loop"]
+
+
+ "src",
+ [
+ # Declared fresh each pass: bounded per iteration.
+ "function f(fs){ for (const f of fs){ let js = read(f);\n js += '\\n//map'; write(f, js) } }",
+ # Declared in the same branch that appends: fresh whenever the append runs.
+ "function f(fs){ for (const f of fs){ if (ok(f)) { let js = read(f); js += '//map'; write(js) } } }",
+ # Reset by a plain assignment inside the loop.
+ "function f(rs){ let v; for (const r of rs){ v = r.style; v += ';'; use(v) } }",
+ # Appended once, right before leaving the loop or the function.
+ "function f(s){ let m=''; for (const c of s){ if (c.stop) { m += ' [cut]'; break } } return m }",
+ "function f(xs){ let m=''; for (const x of xs){ if (x.bad) { m += 'bad'; return m } } }",
+ "function f(xs){ let m='e:'; for (const x of xs){ if (x.bad) { m += 'bad'; throw new Error(m) } } }",
+ # The break leaves the inner loop and the outer loop re-binds the name.
+ "function f(ys){ for (const y of ys){ let s=''; while (more()){ s += 'a'; break } use(s) } }",
+ ],
+)
+def test_ts_string_concat_bounded_per_iteration_not_flagged(src):
+ assert _ts_concat_lines(src) == []
+
+
+ "src",
+ [
+ "function f(xs){ let s=''; for (const x of xs){ s += `${x}\\n` } return s }",
+ # A re-bind that only some passes run is no reset.
+ "function f(xs){ let s=''; for (const x of xs){ if (x.first) { s = 'head' } s += `${x}` } return s }",
+ "function f(xs){ let s=''; for (const x of xs){ for (const y of x){ s = y } s += 'a' } return s }",
+ # ``s = s + ...`` keeps the old value, so it is no reset.
+ "function f(xs){ let s=''; for (const x of xs){ s = s + 'a'; s += 'b' } return s }",
+ # A break that only leaves a switch, or only the inner of two loops.
+ "function f(xs){ let s=''; for (const x of xs){ switch (x.k) { case 1: s += 'a'; break } } return s }",
+ "function f(ys){ let s=''; for (const y of ys){ for (const x of y){ s += 'a'; break } } return s }",
+ # Declared in the outer loop, grown across the inner one.
+ "function f(ys){ for (const y of ys){ let s=''; for (const x of y){ s += 'a' } use(s) } }",
+ # A path to ``continue`` before the exit keeps the loop going.
+ "function f(xs){ let s=''; for (const x of xs){ s += 'a'; if (x) continue; break } return s }",
+ # A subscript target is never re-bound by a declaration.
+ "function f(xs, buf){ for (const x of xs){ buf[0] += ` ${x}` } }",
+ ],
+)
+def test_ts_string_concat_across_iterations_still_flagged(src):
+ assert len(_ts_concat_lines(src)) == 1
+
+
+def test_ts_string_concat_loop_local_and_accumulator_in_one_loop():
+ src = (
+ "function f(xs){ let out='';\n"
+ " for (const x of xs){ let v = x.name;\n"
+ " v += ';';\n"
+ " out += `${v}`; }\n"
+ " return out }"
+ )
+ assert _ts_concat_lines(src) == [4]
+
+
def test_ts_nested_io_requires_collection_outer_loop():
"""A ``while`` cursor wrapping an inner ``for ... of`` is io_in_loop, not nested.
diff --git a/tests/unit/health/test_perf_io_in_loop.py b/tests/unit/health/test_perf_io_in_loop.py
index 18a2f0a19..fb815aa40 100644
--- a/tests/unit/health/test_perf_io_in_loop.py
+++ b/tests/unit/health/test_perf_io_in_loop.py
@@ -413,6 +413,16 @@ def test_detectors_only_consume_their_own_kind():
assert len(BlockingSyncInAsyncDetector().detect(ctx)) == 1
+ ("path", "expected"),
+ [("src/render.ts", 1), ("src/__tests__/render.spec.ts", 0), ("tests/test_render.py", 0)],
+)
+def test_string_concat_not_reported_in_test_files(path, expected):
+ ctx = _ctx([PerfHit("string_concat_in_loop", 2, "f", "")])
+ ctx.file_path = path
+ assert len(StringConcatInLoopDetector().detect(ctx)) == expected
+
+
def test_no_perf_hits_yields_no_findings():
assert IoInLoopDetector().detect(_ctx([])) == []