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 Go string concat reset detection (#2939)

MigBuilds925f07ed6740
diff --git a/packages/core/src/repowise/core/analysis/health/perf/dialects/go.py b/packages/core/src/repowise/core/analysis/health/perf/dialects/go.py
index 247838eb5..48059a3aa 100644
--- a/packages/core/src/repowise/core/analysis/health/perf/dialects/go.py
+++ b/packages/core/src/repowise/core/analysis/health/perf/dialects/go.py
@@ -82,6 +82,7 @@ GO_REGEX_COMPILE: frozenset[str] = frozenset(
{"MustCompile", "Compile", "MustCompilePOSIX", "CompilePOSIX"}
)
_GO_STRING_KINDS: frozenset[str] = frozenset({"interpreted_string_literal", "raw_string_literal"})
+_GO_LOOP_KINDS: frozenset[str] = frozenset({"for_statement"})
# Heavy connection / client constructors, keyed ``(package, func)``. Opening one
# per ``for ... range`` iteration is the connection-churn anti-pattern.
@@ -100,9 +101,16 @@ GO_RESOURCE_CTORS: frozenset[tuple[str, str]] = frozenset(
GO_LOCK_METHODS: frozenset[str] = frozenset({"Lock", "RLock"})
# ``slices.Chunk`` (Go 1.23) and hand-rolled peers, matched on the call's
# rightmost name so a local helper (``Batch(xs, n)``) counts too.
-GO_CHUNK_CALLS: frozenset[str] = frozenset(
- {"Chunk", "chunk", "Batch", "batch", "Chunks", "chunks"}
-)
+GO_CHUNK_CALLS: frozenset[str] = frozenset({"Chunk", "chunk", "Batch", "batch", "Chunks", "chunks"})
+
+
+def _single_identifier(node: Node | None) -> bytes | None:
+ if node is None:
+ return None
+ targets = node.named_children if node.type == "expression_list" else [node]
+ if len(targets) != 1 or targets[0].type != "identifier":
+ return None
+ return targets[0].text
class GoPerfDialect(BasePerfDialect):
@@ -188,7 +196,24 @@ class GoPerfDialect(BasePerfDialect):
if right is None:
return False
targets = right.named_children if right.type == "expression_list" else [right]
- return any(c.type in _GO_STRING_KINDS for c in targets)
+ if not any(c.type in _GO_STRING_KINDS for c in targets):
+ return False
+ left = _single_identifier(node.child_by_field_name("left"))
+ return left is None or not self.resets_per_iteration(node, left, _GO_LOOP_KINDS)
+
+ def binds_name(self, node: Node, name: bytes) -> bool:
+ if node.type == "short_var_declaration":
+ return _single_identifier(node.child_by_field_name("left")) == name
+ if node.type == "var_spec":
+ identifier = node.child_by_field_name("name")
+ return (
+ identifier is not None
+ and identifier.type == "identifier"
+ and identifier.text == name
+ )
+ if node.type != "assignment_statement" or not any(c.type == "=" for c in node.children):
+ return False
+ return _single_identifier(node.child_by_field_name("left")) == name
def loop_call_marker(
self, root: str, method: str, node: Node, list_names: frozenset[str]
diff --git a/tests/unit/health/test_perf_dialects.py b/tests/unit/health/test_perf_dialects.py
index e959e9863..c5caf1165 100644
--- a/tests/unit/health/test_perf_dialects.py
+++ b/tests/unit/health/test_perf_dialects.py
@@ -141,6 +141,21 @@ def test_go_cases(src, expected, note):
assert _hits("go", src) == sorted(expected), note
+def test_go_string_concat_reset_per_iteration_not_flagged():
+ reassigned = (
+ 'package p\nfunc f(rows []string){ for _, row := range rows { line = row; line += "," } }\n'
+ )
+ short_declared = 'package p\nfunc f(rows []string){ for _, row := range rows { line := row; line += "," } }\n'
+ var_declared = 'package p\nfunc f(rows []string){ for _, row := range rows { var line string; line += "," } }\n'
+ accumulator = (
+ 'package p\nfunc f(rows []string){ out := ""; for _, row := range rows { out += "," } }\n'
+ )
+
+ for source in (reassigned, short_declared, var_declared):
+ assert not any(kind == "string_concat_in_loop" for kind, _ in _hits("go", source))
+ assert ("string_concat_in_loop", "") in _hits("go", accumulator)
+
+
# ---------------------------------------------------------------------------
# C#
# ---------------------------------------------------------------------------