Skip to content

Commit 25c4905

Browse files
committed
Recursively check type annotation instead of iterating over children
1 parent 5898e7a commit 25c4905

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

Source/SwiftLintBuiltInRules/Rules/Lint/UnneededThrowsRule.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,25 @@ private extension PatternBindingSyntax {
190190
}
191191

192192
var functionTypeSyntax: FunctionTypeSyntax? {
193-
guard let typeAnnotation else { return nil }
194-
195-
var children = Set(typeAnnotation.children(viewMode: .sourceAccurate))
193+
typeAnnotation?.type.baseFunctionTypeSyntax
194+
}
195+
}
196196

197-
while let child = children.popFirst() {
198-
if let functionType = child.as(FunctionTypeSyntax.self) {
199-
return functionType
200-
}
201-
children.formUnion(child.children(viewMode: .sourceAccurate))
197+
private extension TypeSyntax {
198+
var baseFunctionTypeSyntax: FunctionTypeSyntax? {
199+
switch Syntax(self).as(SyntaxEnum.self) {
200+
case .functionType(let function):
201+
function
202+
case .optionalType(let optional):
203+
optional.wrappedType.baseFunctionTypeSyntax
204+
case .attributedType(let attributed):
205+
attributed.baseType.baseFunctionTypeSyntax
206+
case .tupleType(let tuple):
207+
tuple.elements
208+
.compactMap { $0.type.baseFunctionTypeSyntax }
209+
.first
210+
default:
211+
nil
202212
}
203-
204-
return nil
205213
}
206214
}

Source/SwiftLintBuiltInRules/Rules/Lint/UnneededThrowsRuleExamples.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ internal struct UnneededThrowsRuleExamples {
140140
catch is SomeError { throw AnotherError }
141141
catch is AnotherError {}
142142
}
143-
""")
143+
"""),
144+
Example("let s: S<() throws -> Void> = S()"),
144145
]
145146

146147
static let triggeringExamples = [

0 commit comments

Comments
 (0)