Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(health): ignore loops that retain every row (#3406)
nightcitybladecf76e12f53cc
diff --git a/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.py b/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.pyindex ec0836ba1..ea6b4fbb1 100644--- a/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.py+++ b/packages/core/src/repowise/core/analysis/health/perf/reduction_idioms.py@@ -122,6 +122,33 @@ def _adds_to(stmts: list[Node], container: str, keys: frozenset[str]) -> bool:)+def _keeps_every_row(stmts: list[Node], values: frozenset[str]) -> bool:+ """Whether an unguarded statement stores each iterated row."""+ nested_loops = frozenset({"for_statement", "while_statement"})+ guarded = False+ for stmt in stmts:+ candidates = walk(stmt, nested_loops) if stmt.type not in nested_loops else ()+ guarded = guarded or any(n.type == "continue_statement" for n in candidates)+ if guarded:+ continue+ node = next((c for c in stmt.children if c.is_named), stmt)+ call = _method_call(node)+ if call and call[1] in ("add", "append", "extend") and any(+ references(arg, values) for arg in call[2]+ ):+ return True+ left = node.child_by_field_name("left") if node.type == "assignment" else None+ right = node.child_by_field_name("right") if left is not None else None+ if (+ left is not None+ and right is not None+ and left.type == "subscript"+ and references(right, values)+ ):+ return True+ return False++def _keeps_first_via_guard(stmt: Node, rest: list[Node], rows: frozenset[str], keys: frozenset[str]) -> bool:@@ -143,7 +170,7 @@ def reduces_per_key(for_stmt: Node) -> bool:return Falsestmts = [c for c in body.children if c.is_named]keys = _derived(stmts, rows)- return any(+ return not _keeps_every_row(stmts, keys) and any(_keeps_first_via_setdefault(stmt, rows, keys)or _keeps_first_via_guard(stmt, stmts[index + 1 :], rows, keys)for index, stmt in enumerate(stmts)diff --git a/tests/unit/health/test_perf_unbounded_reduction.py b/tests/unit/health/test_perf_unbounded_reduction.pyindex 978ce0e9f..3a8b3170e 100644--- a/tests/unit/health/test_perf_unbounded_reduction.py+++ b/tests/unit/health/test_perf_unbounded_reduction.py@@ -167,6 +167,26 @@ def test_bare_one_to_one_projection_does_not_fire(tmp_path: Path):assert hits == []+def test_filter_makes_keep_all_append_a_reduction(tmp_path: Path):+ hits = _hits(+ tmp_path,+ "mixed.py",+ """+ def filtered(supabase):+ for build in supabase.table("builds").select("*").execute().data:+ if not build.active: continue+ by_name.setdefault(build.name, build)+ by_file.setdefault(build.path, []).append(build)+ def unfiltered(supabase):+ for build in supabase.table("builds").select("*").execute().data:+ for item in build.items: continue+ by_name.setdefault(build.name, build)+ by_file.setdefault(build.path, []).append(build)+ """,+ )+ assert {hit.function for hit in hits} == {"filtered"}++def test_query_inside_a_loop_does_not_fire(tmp_path: Path):hits = _hits(tmp_path,