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(perf): take a Go semaphore of any size but a literal 0 as a bound (#2985)
Yang Bai7d404c5ae2fb
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.pyindex b78bcd3d6..76bd3b250 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@@ -543,7 +543,13 @@ class GoPerfDialect(BasePerfDialect):@staticmethoddef _is_buffered_make(call: Node) -> bool:- """True if ``call`` is ``make(chan T, N)`` with a positive buffer size."""+ """True if ``call`` is ``make(chan T, N)`` and ``N`` is not a literal ``0``.++ The size is usually a variable, a field or a call (``nParallel``,+ ``cfg.Workers``, ``runtime.NumCPU()``), which is taken as a bound. One+ that happens to be 0 at run time cannot be seen from here and is not+ claimed; the literal ``0`` is the one size known to be unbuffered.+ """if call.type != "call_expression":return Falsefn = call.child_by_field_name("function")@@ -557,11 +563,10 @@ class GoPerfDialect(BasePerfDialect):return Falsesize = named[1]if size.type != "int_literal" or size.text is None:- return False- try:- return int(size.text.decode("utf-8", "replace")) > 0- except ValueError:- return False+ return True+ digits = size.text.decode("utf-8", "replace").lower().replace("_", "")+ # 0, 00, 0x0, 0b0, 0o0: no digit other than zero after the base prefix.+ return any(ch not in "0xob" for ch in digits)@staticmethoddef _nearest_for_is_range(node: Node) -> bool:diff --git a/tests/unit/health/test_perf_dialects.py b/tests/unit/health/test_perf_dialects.pyindex 9b52a88b8..98531bec5 100644--- a/tests/unit/health/test_perf_dialects.py+++ b/tests/unit/health/test_perf_dialects.py@@ -703,6 +703,54 @@ def test_go_goroutine_in_range_loop_but_not_accept_loop():assert not any(k == "goroutine_in_unbounded_loop" for k, _ in _hits("go", count))+_GO_SEMAPHORE = (+ "package p\nfunc f(items []int, n int, cfg Config) {{\n"+ "\tsem := {make}\n"+ "\tfor _, it := range items {{\n"+ "{acquire}"+ "\t\tgo func() {{ defer func(){{ <-sem }}(); _ = it }}()\n"+ "\t}}\n}}\n"+)+_GO_ACQUIRE = "\t\tsem <- struct{}{}\n"+++def _go_semaphore_hits(make: str, acquire: str = _GO_ACQUIRE) -> list[tuple[str, str]]:+ hits = _hits("go", _GO_SEMAPHORE.format(make=make, acquire=acquire))+ return [hit for hit in hits if hit[0] == "goroutine_in_unbounded_loop"]+++ "size",+ ["4", "0x10", "1_000", "n", "nParallel", "cfg.Workers", "runtime.NumCPU()", "n * 2", "int64(n)"],+)+def test_go_semaphore_bounds_the_loop_whatever_sizes_it(size):+ """A semaphore sized from a flag or a config value is the usual form; only+ an integer literal used to count as a bound (#2966)."""+ assert _go_semaphore_hits(f"make(chan struct{{}}, {size})") == []+++ "make",+ [+ "make(chan struct{})",+ "make(chan struct{}, 0)",+ "make(chan struct{}, 00)",+ "make(chan struct{}, 0x0)",+ "make(chan struct{}, 0b0)",+ "make(chan struct{}, 0o0)",+ "make(chan struct{}, 0_0)",+ ],+)+def test_go_unbuffered_channel_is_not_a_semaphore(make):+ assert _go_semaphore_hits(make) == [("goroutine_in_unbounded_loop", "")]+++def test_go_semaphore_never_acquired_does_not_bound_the_loop():+ assert _go_semaphore_hits("make(chan struct{}, n)", acquire="") == [+ ("goroutine_in_unbounded_loop", "")+ ]++def test_python_list_insert_zero_vs_variable_index():front = "def f(xs):\n out = []\n for x in xs:\n out.insert(0, x)\n"idx = "def f(xs):\n out = []\n for i, x in enumerate(xs):\n out.insert(i, x)\n"