Compare a deinit the way other Swift methods are compared.

A deinit has a body, and a function nested inside it was being reported
on its own. The deinit is now the candidate, and the nested function
stays inside it. Actor methods are documented with the other type methods.

Proved by test_a_deinit_is_compared_and_keeps_its_nested_function.

Co-authored-by: Robert C. Martin <[email protected]>
This commit is contained in:
Cursor Agent
2026-11-25 23:51:01 +00:00
co-authored by Robert C. Martin
parent 105ff6f50c
commit 2862b8428e
4 changed files with 39 additions and 6 deletions
+1 -1
View File
@@ -110,7 +110,7 @@ DUPLICATE score=0.89
| Python | functions and methods | functions nested inside another function | | Python | functions and methods | functions nested inside another function |
| Swift | functions and methods with a body, computed properties, and file-level closures | initializers, subscripts, protocol requirements, and closures or functions nested inside another form | | Swift | functions and methods with a body, computed properties, and file-level closures | initializers, subscripts, protocol requirements, and closures or functions nested inside another form |
A Swift computed property includes a SwiftUI `body`. A method on a struct, class, enum, or extension is a candidate, and so is a protocol method that has a default body. A protocol requirement does not. A closure written inside a function stays in that function. A closure bound with `let` or `var` at file scope is its own candidate, as a top-level arrow function is in TypeScript. Argument labels stay, including the label of a trailing closure, because in Swift they are part of the call. `>=`, `!=`, `...`, and `..<` stay. Locals, field names, and literals, including `nil`, do not. An interpolated string keeps the interpolation. A Swift computed property includes a SwiftUI `body`. A method on a struct, class, actor, enum, or extension is a candidate, and so is a deinit and a protocol method that has a default body. A protocol requirement does not. A closure written inside a function stays in that function. A closure bound with `let` or `var` at file scope is its own candidate, as a top-level arrow function is in TypeScript. Argument labels stay, including the label of a trailing closure, because in Swift they are part of the call. `>=`, `!=`, `...`, and `..<` stay. Locals, field names, and literals, including `nil`, do not. An interpolated string keeps the interpolation.
Clojure reader conditionals keep the `:clj` branch, as dry4clj does. Syntax-quote is kept as a `syntax-quote` form rather than expanded. Clojure reader conditionals keep the `:clj` branch, as dry4clj does. Syntax-quote is kept as a `syntax-quote` form rather than expanded.
+1 -1
View File
@@ -60,7 +60,7 @@ named {_skipped_directory_text()} are skipped, as are *_test.go, *.test.cts,
Clojure compares every top-level form except ns. The other languages compare Clojure compares every top-level form except ns. The other languages compare
functions and methods. Swift also compares computed properties, including a functions and methods. Swift also compares computed properties, including a
SwiftUI view body, and a closure bound at file scope. A form is only compared SwiftUI view body, a closure bound at file scope, and deinit. A form is only compared
with forms in the same language. with forms in the same language.
Exit codes: Exit codes:
+7 -4
View File
@@ -4,7 +4,7 @@ Clojure compares every top-level list except `ns`, matching dry4clj. The other
languages compare the functions and methods crapper scores: bodies, not languages compare the functions and methods crapper scores: bodies, not
signatures alone, and not callbacks nested inside another function. Rust signatures alone, and not callbacks nested inside another function. Rust
functions inside `mod tests` are test code and are skipped. Swift also functions inside `mod tests` are test code and are skipped. Swift also
compares computed properties and a closure bound at file scope. compares computed properties, a closure bound at file scope, and deinit.
""" """
from __future__ import annotations from __future__ import annotations
@@ -131,8 +131,9 @@ def _typescript_nodes(data: bytes, root) -> list:
return found return found
# A closure, a local function, and a function written inside a view body stay # A closure, a local function, and a function written inside a view body or a
# inside the form that contains them. A protocol requirement has no body. # deinit stay inside the form that contains them. A protocol requirement has
# no body. deinit itself is a method with a body.
_SWIFT_NEST = { _SWIFT_NEST = {
"function_declaration", "function_declaration",
"protocol_function_declaration", "protocol_function_declaration",
@@ -140,7 +141,9 @@ _SWIFT_NEST = {
"computed_property", "computed_property",
"init_declaration", "init_declaration",
"subscript_declaration", "subscript_declaration",
"deinit_declaration",
} }
_SWIFT_FUNCTION = {"function_declaration", "deinit_declaration"}
def _swift_property(node) -> bool: def _swift_property(node) -> bool:
@@ -152,7 +155,7 @@ def _swift_nodes(data: bytes, root) -> list:
for node in descendants(root): for node in descendants(root):
if _inside(node, _SWIFT_NEST): if _inside(node, _SWIFT_NEST):
continue continue
if node.type == "function_declaration": if node.type in _SWIFT_FUNCTION:
found.append(node) found.append(node)
elif node.type == "protocol_function_declaration" and _has(node, "statements"): elif node.type == "protocol_function_declaration" and _has(node, "statements"):
found.append(node) found.append(node)
+30
View File
@@ -372,6 +372,36 @@ def test_a_swift_package_skips_its_tests(tmp_path):
assert relative == ["Sources/App.swift"] assert relative == ["Sources/App.swift"]
def test_a_deinit_is_compared_and_keeps_its_nested_function(tmp_path):
source = """\
class Panel {
deinit {
func hidden() -> Int {
return cache()
}
close(handle)
}
func stop() {
close(handle)
}
}
"""
entries, warning = entries_in_source("swift", source, "Panel.swift", "Panel.swift")
assert warning is None
assert [(entry.start_line, entry.end_line) for entry in entries] == [(2, 7), (9, 11)]
assert _scored(
tmp_path,
"class A {\n deinit {\n close(handle)\n release(buffer)\n }\n}\n",
"class B {\n deinit {\n close(other)\n release(kept)\n }\n}\n",
) == 1.0
assert _scored(
tmp_path,
"class A {\n deinit {\n close(handle)\n }\n}\n",
"class B {\n deinit {\n shutdown(other)\n }\n}\n",
) < 1.0
def test_a_top_level_swift_closure_matches_its_renamed_copy(tmp_path): def test_a_top_level_swift_closure_matches_its_renamed_copy(tmp_path):
left = """\ left = """\
let alpha = { (xs: [Int]) -> [Int] in let alpha = { (xs: [Int]) -> [Int] in