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): rescue a Swift extension whose member name is declared elsewhere (#3876)

Yanis0a4fb1ea98ec
diff --git a/packages/core/src/repowise/core/analysis/dead_code/swift_reachability.py b/packages/core/src/repowise/core/analysis/dead_code/swift_reachability.py
index 733bcbe90..95f06de0e 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/swift_reachability.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/swift_reachability.py
@@ -19,7 +19,7 @@ an entry, has an importer, is a test, or was itself reached) does one of:
* for an extension of a type the repository declares, is the type's file; for
an extension of a platform type (``extension Color``), uses one of the
extension's members, by an edge or by ``.member`` / ``member(`` written
- elsewhere, when no other file declares that member name.
+ elsewhere.
Uses are counted from live files only and the answer is a fixpoint, so two dead
files that name each other stay dead. Comments and string literals are
@@ -54,8 +54,8 @@ _NAME = r"[^\W\d]\w*"
_TYPE_USE = re.compile(
rf"(?:[:<\[(,]|->|\bas[?!]?|\bis)\s*(?:{_NAME}\s*\.\s*)*({_NAME})|\b({_NAME})\s*[(.]"
)
-#: A member written as ``.member`` or called as ``member(``.
-_MEMBER_USE = re.compile(rf"\.\s*({_NAME})|\b({_NAME})\s*\(")
+#: A member written as ``.member`` or called as ``member(``, not declared by ``func``/``subscript``.
+_MEMBER_USE = re.compile(rf"\.\s*({_NAME})|(?<!\bfunc\s)\b(?!subscript\b)({_NAME})\s*\(")
def _line_comment_end(text: str, i: int) -> int:
@@ -165,7 +165,6 @@ class _SwiftFacts:
self.declared: dict[str, set[str]] = {} # file -> type names it declares
self.extends: dict[str, set[str]] = {} # file -> extended type names
self.members: dict[str, set[str]] = {} # file -> its extensions' member names
- self.member_files: dict[str, set[str]] = {} # any symbol name -> declaring files
self.own: dict[str, set[str]] = {} # file -> names it declares or extends
self.type_users: dict[str, set[str]] = {} # name -> files writing it as a type
self.member_users: dict[str, set[str]] = {} # name -> files writing it as a member
@@ -189,7 +188,6 @@ class _SwiftFacts:
name = (data.get("name") or "").strip("`") # ``func `if`()``
if not name:
continue
- self.member_files.setdefault(name, set()).add(path)
# Nested types too: ``StatusEditor.EditorStore`` is declared
# inside an extension of ``StatusEditor``.
if data.get("kind") in _TYPE_KINDS:
@@ -263,11 +261,9 @@ class _Reach:
return False
def _member_used(self, path: str) -> bool:
- """A live file writes a member of *path*'s extensions no other file declares."""
+ """A live file writes a member of *path*'s extensions."""
facts = self.facts
for member in facts.members.get(path, ()):
- if facts.member_files.get(member, set()) != {path}:
- continue
if any(u != path and self._live(u) for u in facts.member_users.get(member, ())):
return True
return False
diff --git a/tests/unit/dead_code/test_swift_reachability.py b/tests/unit/dead_code/test_swift_reachability.py
index 07c27e045..68ba5e5fe 100644
--- a/tests/unit/dead_code/test_swift_reachability.py
+++ b/tests/unit/dead_code/test_swift_reachability.py
@@ -63,6 +63,9 @@ _FILES = {
"import Foundation\n\npublic struct PartialAccountWithAvatar: Codable {\n"
" public let id: String\n}\n"
),
+ f"{_DS}/Views/DismissGame.swift": (
+ "import SwiftUI\n\nextension DependencyValues {\n public var dismissGame: Int { 0 }\n}\n"
+ ),
f"{_MODELS}/Legacy.swift": (
"import Foundation\n\npublic struct LegacyThing {}\n\n"
"extension LegacyThing {\n func old() {}\n}\n"
@@ -74,6 +77,7 @@ _FILES = {
" @State private var history: [StatusHistory]?\n"
" let store = StatusEditor.EditorStore()\n"
' let label = "LegacyThing"\n'
+ " @Dependency(\\.dismissGame) var dismissGame\n"
" public var body: some View {\n"
' TagRowView(tag: "swift").foregroundColor(.brandTint)\n }\n}\n'
),
@@ -121,6 +125,7 @@ def test_files_a_live_file_uses_names_or_extends_are_reachable(tmp_path: Path) -
"Colors.swift", # a member of its platform-type extension is used
"EditorStore.swift",
"EditorMode.swift", # extends a type declared in a reachable file
+ "DismissGame.swift", # `.dismissGame` is also declared in the using file
}
), unreachable
@@ -152,7 +157,11 @@ def _graph(decls: dict[str, list[tuple[str, str, str | None]]], entry: str) -> n
_ROOT = "App/Main.swift"
_DECLS: dict[str, list[tuple[str, str, str | None]]] = {
- _ROOT: [("Main", "class", None)],
+ _ROOT: [
+ ("Main", "class", None),
+ ("label", "method", "Main"),
+ ("subscript", "method", "Main"),
+ ],
"Models/StatusHistory.swift": [("StatusHistory", "class", None)],
"Models/Store.swift": [("Store", "class", None)],
"Models/Store+Mode.swift": [("Store", "impl", None), ("Mode", "class", "Store")],
@@ -160,6 +169,10 @@ _DECLS: dict[str, list[tuple[str, str, str | None]]] = {
"Probe/String+Probe.swift": [("String", "impl", None), ("probeTrimmed", "method", "String")],
"Probe/URL+Id.swift": [("URL", "impl", None), ("id", "variable", "URL")],
"Probe/Account.swift": [("Account", "class", None), ("id", "variable", "Account")],
+ "Probe/Observer+Store.swift": [("Observer", "impl", None), ("store", "method", "Observer")],
+ "Probe/Canceller+Store.swift": [("Canceller", "impl", None), ("store", "method", "Canceller")],
+ "Probe/Locale+Label.swift": [("Locale", "impl", None), ("label", "method", "Locale")],
+ "Probe/Grid+Cell.swift": [("Grid", "impl", None), ("subscript", "method", "Grid")],
"Probe/DeadA.swift": [("DeadA", "class", None)],
"Probe/DeadB.swift": [("DeadB", "class", None)],
"Probe/Text.swift": [("Text", "class", None)],
@@ -180,6 +193,8 @@ _SOURCE = {
b' let q = """\n QuotedType(\n """\n'
b' let r = #"RawQuotedType( \\"#\n'
b" let n = count + BareOperand\n"
+ b" func label() {}\n func run() { token.store(in: &bag) }\n"
+ b" subscript(k: Int) -> Int { k }\n"
b' let v = "ItemView" // ItemView\n'
b"}\n"
),
@@ -190,6 +205,10 @@ _SOURCE = {
"Probe/String+Probe.swift": b"extension String { func probeTrimmed() -> String { self } }\n",
"Probe/URL+Id.swift": b"extension URL { var id: String { absoluteString } }\n",
"Probe/Account.swift": b"struct Account { let id: String }\n",
+ "Probe/Observer+Store.swift": b"extension Observer { func store(in b: Int) {} }\n",
+ "Probe/Canceller+Store.swift": b"extension Canceller { func store(in b: Int) {} }\n",
+ "Probe/Locale+Label.swift": b"extension Locale { func label() -> Int { 0 } }\n",
+ "Probe/Grid+Cell.swift": b"extension Grid { subscript(i: Int) -> Int { i } }\n",
"Probe/DeadA.swift": b"struct DeadA { let b: DeadB }\n",
"Probe/DeadB.swift": b"struct DeadB { let a: [DeadA] }\n",
"Probe/Text.swift": b"struct Text {}\n",
@@ -208,7 +227,10 @@ def test_live_uses_in_type_and_member_positions_reach_a_file() -> None:
"Models/StatusHistory.swift", # [StatusHistory]
"Models/Store.swift", # Store()
"Models/Store+Mode.swift", # extends a reached type
- "Views/Colors.swift", # .brandTint, declared by no other file
+ "Views/Colors.swift", # .brandTint
+ "Probe/URL+Id.swift", # `$0.id`, though a repo struct has an `id` too
+ "Probe/Observer+Store.swift", # one `.store(in:)` reaches both extensions
+ "Probe/Canceller+Store.swift",
} <= named, named
@@ -216,7 +238,8 @@ def test_dead_code_the_text_cannot_vouch_for_stays_unreachable() -> None:
named = build_swift_named_files(_graph(_DECLS, _ROOT), _SOURCE)
assert not named & {
"Probe/String+Probe.swift", # an unused extension of a platform type
- "Probe/URL+Id.swift", # its only member name is declared elsewhere too
+ "Probe/Locale+Label.swift", # the root only declares `label`, never uses it
+ "Probe/Grid+Cell.swift", # a subscript is declared, never written by name
"Probe/DeadA.swift", # two dead files naming each other
"Probe/DeadB.swift",
"Probe/Text.swift", # a platform name