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(dead-code): recognize Kotlin operator functions as contract methods (#3695)

milana027884833c3
diff --git a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
index 26d1340ab..75c7c9a60 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
@@ -1771,7 +1771,12 @@ class DeadCodeAnalyzer:
if sym.get("kind") == "interface" and not self._file_has_implementors(node):
confidence = min(confidence, RISK_CAP_CONFIDENCE)
- if is_contract_method(sym_name, sym.get("kind"), sym.get("language", "unknown")):
+ if is_contract_method(
+ sym_name,
+ sym.get("kind"),
+ sym.get("language", "unknown"),
+ modifiers=sym.get("modifiers"),
+ ):
confidence = min(confidence, RISK_CAP_CONFIDENCE)
if risk_factors:
@@ -2109,7 +2114,10 @@ class DeadCodeAnalyzer:
if node_data.get("kind") in _UNCALLABLE_TYPE_KINDS:
return False
if is_contract_method(
- sym_name, node_data.get("kind"), node_data.get("language", "unknown")
+ sym_name,
+ node_data.get("kind"),
+ node_data.get("language", "unknown"),
+ modifiers=node_data.get("modifiers"),
):
return False
if self._name_matches_dynamic(sym_name, dynamic_patterns):
diff --git a/packages/core/src/repowise/core/analysis/dead_code/contract_methods.py b/packages/core/src/repowise/core/analysis/dead_code/contract_methods.py
index 678760c04..5b4aca4f8 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/contract_methods.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/contract_methods.py
@@ -30,6 +30,7 @@ overrides without static callers, etc.
from __future__ import annotations
import re
+from collections.abc import Iterable
# Method names reserved by COM / IUnknown / IDispatch. Case-sensitive —
# Windows COM uses PascalCase universally.
@@ -284,7 +285,12 @@ _GODOT_CALLBACK_NAMES: frozenset[str] = frozenset({
})
-def is_contract_method(sym_name: str, sym_kind: str | None, language: str | None) -> bool:
+def is_contract_method(
+ sym_name: str,
+ sym_kind: str | None,
+ language: str | None,
+ modifiers: Iterable[str] | None = None,
+) -> bool:
"""Return True if *sym_name* is a reserved contract-method name in *language*.
The check is intentionally narrow: only kind=``method`` symbols in
@@ -308,6 +314,8 @@ def is_contract_method(sym_name: str, sym_kind: str | None, language: str | None
return True
if language in _JVM_LANGUAGES and sym_name in _JVM_CONTRACT_METHOD_NAMES:
return True
+ if language == "kotlin" and "operator" in (modifiers or ()):
+ return True
if language == "gdscript" and sym_name in _GODOT_CALLBACK_NAMES:
return True
if language in _CPP_LANGUAGES:
diff --git a/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py b/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py
index 542d9f5a7..1022cc94a 100644
--- a/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py
+++ b/packages/core/src/repowise/core/analysis/health/refactoring/move_method.py
@@ -182,7 +182,7 @@ def _overrides(graph: Any, data: dict, own_class_id: str, language: str) -> bool
return True
if _CONTRACT_MODIFIERS.intersection(data.get("modifiers") or ()):
return True
- if is_contract_method(name, data.get("kind"), language):
+ if is_contract_method(name, data.get("kind"), language, modifiers=data.get("modifiers")):
return True
declared = {
(_node(graph, member) or {}).get("name")
diff --git a/tests/unit/analysis/test_jvm_reachability.py b/tests/unit/analysis/test_jvm_reachability.py
index 88478a645..436be8864 100644
--- a/tests/unit/analysis/test_jvm_reachability.py
+++ b/tests/unit/analysis/test_jvm_reachability.py
@@ -307,6 +307,14 @@ class TestJvmContractMethods:
def test_unrelated_language_not_protected(self) -> None:
assert not is_contract_method("equals", "method", "python")
+ def test_kotlin_operator_modifier_protected(self) -> None:
+ assert is_contract_method("plus", "function", "kotlin", modifiers=("operator",))
+ assert is_contract_method("plus", "method", "kotlin", modifiers=("operator",))
+ assert not is_contract_method("plus", "function", "kotlin")
+ assert not is_contract_method("plus", "function", "kotlin", modifiers=())
+ assert not is_contract_method("plus", "method", "java", modifiers=("operator",))
+
+
# ---------------------------------------------------------------------------
# Framework decorators recognised
@@ -399,6 +407,73 @@ class TestJvmEndToEnd:
kinds = [(f.kind, f.symbol_name) for f in report.findings]
assert (DeadCodeKind.UNUSED_INTERNAL, "init") not in kinds
+ def test_kotlin_public_operator_capped_at_risk_cap(self) -> None:
+ graph = _build_graph(
+ {
+ "src/MultipartApp.kt": _kt_file(
+ symbols=[{
+ "name": "plus",
+ "kind": "function",
+ "visibility": "public",
+ "modifiers": ("operator",),
+ "language": "kotlin",
+ }],
+ ),
+ },
+ )
+ analyzer = DeadCodeAnalyzer(graph)
+ report = analyzer.analyze({"min_confidence": 0.0})
+ findings = [f for f in report.findings if f.symbol_name == "plus"]
+ assert len(findings) == 1
+ finding = findings[0]
+ assert finding.kind == DeadCodeKind.UNUSED_EXPORT
+ assert finding.confidence == 0.4
+ assert not finding.safe_to_delete
+
+ def test_kotlin_private_operator_skipped_from_unused_internal(self) -> None:
+ graph = _build_graph(
+ {
+ "src/Delegate.kt": _kt_file(
+ symbols=[{
+ "name": "getValue",
+ "kind": "function",
+ "visibility": "private",
+ "modifiers": ("operator",),
+ "language": "kotlin",
+ }],
+ ),
+ },
+ )
+ analyzer = DeadCodeAnalyzer(graph)
+ report = analyzer.analyze({"min_confidence": 0.0})
+ internal_names = {
+ f.symbol_name
+ for f in report.findings
+ if f.kind == DeadCodeKind.UNUSED_INTERNAL
+ }
+ assert "getValue" not in internal_names
+
+ def test_kotlin_plain_function_without_operator_modifier_unaffected(self) -> None:
+ graph = _build_graph(
+ {
+ "src/Utils.kt": _kt_file(
+ symbols=[{
+ "name": "concat",
+ "kind": "function",
+ "visibility": "public",
+ "modifiers": (),
+ "language": "kotlin",
+ }],
+ ),
+ },
+ )
+ analyzer = DeadCodeAnalyzer(graph)
+ report = analyzer.analyze({"min_confidence": 0.0})
+ findings = [f for f in report.findings if f.symbol_name == "concat"]
+ assert len(findings) == 1
+ assert findings[0].kind == DeadCodeKind.UNUSED_EXPORT
+ assert findings[0].confidence == 0.6
+
# ---------------------------------------------------------------------------
# Workspace-driven never-flag (Phase 5 follow-up)
diff --git a/tests/unit/health/test_refactoring_move_method.py b/tests/unit/health/test_refactoring_move_method.py
index 255233447..9b5926dfb 100644
--- a/tests/unit/health/test_refactoring_move_method.py
+++ b/tests/unit/health/test_refactoring_move_method.py
@@ -431,6 +431,7 @@ def test_override_virtual_and_abstract_modifiers_never_move():
("cs", "csharp", ("public", "abstract")),
("kt", "kotlin", ("override",)),
("kt", "kotlin", ("open",)),
+ ("kt", "kotlin", ("operator",)),
("vb", "vbnet", ("public", "overrides")),
):
g = _envy_graph_in(ext, language)