Skip to content

Commit

Permalink
Ignore functions with default parameters in unneeded_override rule (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Nov 16, 2023
1 parent 33e3593 commit 9cd4740
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

#### Bug Fixes

* Ignore overridden functions with default parameters in the `unneeded_override`
rule as they might change behavior.
[SimplyDanny](https://github.com/SimplyDanny)
[#5355](https://github.com/realm/SwiftLint/pull/5355)

* Trigger `nsobject_prefer_isequal` and `redundant_self_in_closure` even in case
the surrounding declaration is nested in an extension.
[SimplyDanny](https://github.com/SimplyDanny)
Expand Down
16 changes: 11 additions & 5 deletions Source/SwiftLintBuiltInRules/Rules/Lint/UnneededOverrideRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private func simplify(_ node: some SyntaxProtocol) -> (any ExprSyntaxProtocol)?
if let expr = node.as(AwaitExprSyntax.self) {
return expr.expression
} else if let expr = node.as(TryExprSyntax.self) {
// Assume using try! / try? changes behavior
// Assume using try! / try? changes behavior.
if expr.questionOrExclamationMark != nil {
return nil
}
Expand All @@ -37,7 +37,7 @@ private func simplify(_ node: some SyntaxProtocol) -> (any ExprSyntaxProtocol)?

private func extractFunctionCallSyntax(_ node: some SyntaxProtocol) -> FunctionCallExprSyntax? {
// Extract the function call from other expressions like try / await / return.
// If this returns a non-super calling function that will get filtered out later
// If this returns a non-super calling function, it will get filtered out later.
var syntax = simplify(node)
while let nestedSyntax = syntax {
if nestedSyntax.as(FunctionCallExprSyntax.self) != nil {
Expand Down Expand Up @@ -79,7 +79,7 @@ private func isUnneededOverride(_ node: FunctionDeclSyntax) -> Bool {
return false
}

// Assume having @available changes behavior
// Assume having @available changes behavior.
if node.attributes.contains(attributeNamed: "available") {
return false
}
Expand All @@ -92,8 +92,14 @@ private func isUnneededOverride(_ node: FunctionDeclSyntax) -> Bool {
return false
}

// Assume any change in arguments passed means behavior was changed
let expectedArguments = node.signature.parameterClause.parameters.map {
let declParameters = node.signature.parameterClause.parameters
if declParameters.contains(where: { $0.defaultValue != nil }) {
// Any default parameter might be a change to the super function.
return false
}

// Assume any change in arguments passed means behavior was changed.
let expectedArguments = declParameters.map {
($0.firstName.text == "_" ? "" : $0.firstName.text, $0.secondName?.text ?? $0.firstName.text)
}
let actualArguments = call.arguments.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ struct UnneededOverrideRuleExamples {
})
}
}
"""),
Example("""
class Baz: Foo {
// A default argument might be a change
override func bar(value: String = "Hello") {
super.bar(value: value)
}
}
""")
]

Expand Down

0 comments on commit 9cd4740

Please sign in to comment.