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(health): stop calling Rust builders, accessor types and enums low-cohesion (#2951)

Raghav Chamadiyaccbae96e72de
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py b/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py
index f9c4732a9..18c91ce60 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/class_analysis.py
@@ -480,7 +480,9 @@ def _stateful_groups(
exposes state) and is not made only of constructors and overrides. A
constructor sets fields that properties or out-of-line methods this pass
cannot place may read, and ``toString`` / ``Equals`` are contracts; neither
- is a class to extract.
+ is a class to extract. Rust gets the same count for its own reason: a
+ builder setter, a getter or a ``match self`` arm touches one field or
+ none, and ``new()`` has no ``self`` at all.
"""
return [
group
@@ -637,7 +639,9 @@ def _compute_lcom4(
*refs* defaults to the explicit ``self.x`` references. Where the map
names field declarations (implicit receivers), overloads are one node and
- only ``_stateful_groups`` count toward LCOM4.
+ only ``_stateful_groups`` count toward LCOM4; a map that sets
+ ``cohesion_counts_state_only`` gets the same count without the implicit
+ references.
"""
if not method_nodes:
return 1, 0, [], 1.0
@@ -660,7 +664,7 @@ def _compute_lcom4(
indexed = _indexed_groups(
roots, method_fcs, members_per_method, method_names, refs.call_targets
)
- if implicit:
+ if implicit or lmap.cohesion_counts_state_only:
groups = _stateful_groups(indexed, refs.contracts)
return max(len(groups), 1), field_count, groups, tcc
return len(indexed), field_count, [g for _, g in indexed], tcc
diff --git a/packages/core/src/repowise/core/analysis/health/complexity/languages.py b/packages/core/src/repowise/core/analysis/health/complexity/languages.py
index d3ac54955..c1440393e 100644
--- a/packages/core/src/repowise/core/analysis/health/complexity/languages.py
+++ b/packages/core/src/repowise/core/analysis/health/complexity/languages.py
@@ -116,6 +116,11 @@ class LanguageNodeMap:
binding_kinds: frozenset[str] = frozenset()
identifier_kinds: frozenset[str] = frozenset()
nested_type_kinds: frozenset[str] = frozenset()
+ # Count only cohesion components that hold state (two or more fields), as
+ # implicit-receiver languages always do. For an explicit-receiver language
+ # whose methods routinely touch one field or none: Rust setters, getters
+ # and ``match self`` arms, and ``new()`` with no ``self`` at all.
+ cohesion_counts_state_only: bool = False
# ------------------------------------------------------------------
# Assertion detection (test-quality smells). Both fields default to
@@ -554,6 +559,8 @@ _RUST = LanguageNodeMap(
class_kinds=frozenset({"impl_item"}),
self_identifiers=frozenset({"self"}),
member_access_kinds=frozenset({"field_expression"}),
+ # Builders, accessor types and enums are one field per method by design.
+ cohesion_counts_state_only=True,
# ``assert!`` / ``assert_eq!`` / ``assert_ne!`` are macro invocations.
assert_call_kinds=frozenset({"macro_invocation"}),
# The perf pass: both ``foo()`` and method/scoped calls (``x.fetch_all()`` /
diff --git a/tests/unit/health/test_implicit_member_cohesion.py b/tests/unit/health/test_implicit_member_cohesion.py
index 6a61fb76b..40166a2b7 100644
--- a/tests/unit/health/test_implicit_member_cohesion.py
+++ b/tests/unit/health/test_implicit_member_cohesion.py
@@ -418,3 +418,82 @@ public:
};
"""
assert _classes("cpp", source, "dllmain.cpp")["Module"].lcom4 == 1
+
+
+RUST = """
+struct Builder { a: u8, b: u8, c: u8, d: u8 }
+
+impl Builder {
+ pub fn new() -> Builder { Builder { a: 0, b: 0, c: 0, d: 0 } }
+ pub fn a(&mut self, v: u8) -> &mut Self { self.a = v; self }
+ pub fn b(&mut self, v: u8) -> &mut Self { self.b = v; self }
+ pub fn c(&mut self, v: u8) -> &mut Self { self.c = v; self }
+ pub fn d(&mut self, v: u8) -> &mut Self { self.d = v; self }
+ pub fn get_a(&self) -> u8 { self.a }
+}
+
+enum Kind { One, Two }
+
+impl Kind {
+ fn is_one(&self) -> bool { matches!(self, Kind::One) }
+ fn is_two(&self) -> bool { matches!(self, Kind::Two) }
+ fn name(&self) -> &str { match self { Kind::One => "one", Kind::Two => "two" } }
+ fn code(&self) -> u8 { match *self { Kind::One => 1, Kind::Two => 2 } }
+ fn default() -> Kind { Kind::One }
+}
+
+struct Split { a1: u8, a2: u8, b1: u8, b2: u8 }
+
+impl Split {
+ fn new() -> Split { Split { a1: 0, a2: 0, b1: 0, b2: 0 } }
+ fn set_a(&mut self, v: u8) { self.a1 = v; self.a2 = v; }
+ fn sum_a(&self) -> u8 { self.a1 + self.a2 }
+ fn set_b(&mut self, v: u8) { self.b1 = v; self.b2 = v; }
+ fn sum_b(&self) -> u8 { self.b1 + self.b2 }
+}
+"""
+
+
+def test_rust_builders_accessors_and_enums_are_not_low_cohesion():
+ classes = _classes("rust", RUST, "lib.rs")
+ assert classes["Builder"].lcom4 == 1
+ assert classes["Kind"].lcom4 == 1
+
+
+def test_rust_split_over_two_field_groups_stays_found():
+ split = _classes("rust", RUST, "lib.rs")["Split"]
+ assert split.lcom4 == 2
+ assert [set(g.fields) for g in split.components] == [{"a1", "a2"}, {"b1", "b2"}]
+
+
+def test_rust_calls_to_methods_outside_the_impl_are_not_fields():
+ source = """
+struct S { a: u8, b: u8 }
+impl S {
+ fn x1(&self) { self.helper(); self.a; }
+ fn x2(&self) { self.a; }
+ fn y1(&self) { self.clone(); self.b; }
+ fn y2(&self) { self.b; }
+ fn y3(&self) { self.b; }
+}
+"""
+ s = _classes("rust", source, "lib.rs")["S"]
+ assert s.lcom4 == 1
+
+
+def test_rust_constructor_like_method_is_an_ordinary_component():
+ # Rust has no constructor or override exemption: ``reset`` touching two
+ # fields is a responsibility like any other method.
+ source = """
+struct R { a1: u8, a2: u8, b1: u8, b2: u8 }
+impl R {
+ fn reset(&mut self) { self.a1 = 0; self.a2 = 0; }
+ fn sum_a(&self) -> u8 { self.a1 + self.a2 }
+ fn set_b(&mut self, v: u8) { self.b1 = v; self.b2 = v; }
+ fn sum_b(&self) -> u8 { self.b1 + self.b2 }
+ fn new() -> R { R { a1: 0, a2: 0, b1: 0, b2: 0 } }
+}
+"""
+ r = _classes("rust", source, "lib.rs")["R"]
+ assert r.lcom4 == 2
+ assert [set(g.methods) for g in r.components] == [{"reset", "sum_a"}, {"set_b", "sum_b"}]