Skip to content

Commit 5898e7a

Browse files
committed
Address batch of comments
1 parent 9e45610 commit 5898e7a

File tree

3 files changed

+30
-38
lines changed

3 files changed

+30
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
* Support deinitializers and subscripts in `function_body_length` rule.
8484
[SimplyDanny](https://github.com/SimplyDanny)
8585
* Add opt-in `unneeded_throws_rethrows` rules that triggers when declarations
86-
marked `throws`/`rethrows` never actually throw or call any throwing code.
86+
marked `throws`/`rethrows` never actually throw or call any throwing code.
8787
[Tony Ngo](https://github.com/tonyskansf)
8888

8989
### Bug Fixes

Source/SwiftLintBuiltInRules/Rules/Lint/UnneededThrowsRule.swift

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct UnneededThrowsRule: Rule {
88
static let description = RuleDescription(
99
identifier: "unneeded_throws_rethrows",
1010
name: "Unneeded (re)throws keyword",
11-
description: "Non-throwing functions/variables should not be marked as `throws` or `rethrows`",
11+
description: "Non-throwing functions/properties/closures should not be marked as `throws` or `rethrows`.",
1212
kind: .lint,
1313
nonTriggeringExamples: UnneededThrowsRuleExamples.nonTriggeringExamples,
1414
triggeringExamples: UnneededThrowsRuleExamples.triggeringExamples,
@@ -24,16 +24,12 @@ private extension UnneededThrowsRule {
2424
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
2525
private var scopes = Stack<Scope>()
2626

27-
override func visit(_: ProtocolDeclSyntax) -> SyntaxVisitorContinueKind {
28-
.skipChildren
29-
}
30-
31-
override func visit(_: TypeAliasDeclSyntax) -> SyntaxVisitorContinueKind {
32-
.skipChildren
33-
}
34-
35-
override func visit(_: EnumCaseDeclSyntax) -> SyntaxVisitorContinueKind {
36-
.skipChildren
27+
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] {
28+
[
29+
ProtocolDeclSyntax.self,
30+
TypeAliasDeclSyntax.self,
31+
EnumCaseDeclSyntax.self,
32+
]
3733
}
3834

3935
override func visit(_: FunctionParameterClauseSyntax) -> SyntaxVisitorContinueKind {
@@ -47,9 +43,9 @@ private extension UnneededThrowsRule {
4743

4844
override func visitPost(_: InitializerDeclSyntax) {
4945
if let closedScope = scopes.closeScope() {
50-
validateScope(
51-
closedScope,
52-
reason: "The initializer does not throw any error"
46+
validate(
47+
scope: closedScope,
48+
reason: "initializer does not throw any error"
5349
)
5450
}
5551
}
@@ -61,9 +57,9 @@ private extension UnneededThrowsRule {
6157

6258
override func visitPost(_: AccessorDeclSyntax) {
6359
if let closedScope = scopes.closeScope() {
64-
validateScope(
65-
closedScope,
66-
reason: "The accessor does not throw any error"
60+
validate(
61+
scope: closedScope,
62+
reason: "accessor does not throw any error"
6763
)
6864
}
6965
}
@@ -75,9 +71,9 @@ private extension UnneededThrowsRule {
7571

7672
override func visitPost(_: FunctionDeclSyntax) {
7773
if let closedScope = scopes.closeScope() {
78-
validateScope(
79-
closedScope,
80-
reason: "The body of this function does not throw any error"
74+
validate(
75+
scope: closedScope,
76+
reason: "body of this function does not throw any error"
8177
)
8278
}
8379
}
@@ -91,9 +87,9 @@ private extension UnneededThrowsRule {
9187

9288
override func visitPost(_: FunctionTypeSyntax) {
9389
if let closedScope = scopes.closeScope() {
94-
validateScope(
95-
closedScope,
96-
reason: "The closure type does not throw any error"
90+
validate(
91+
scope: closedScope,
92+
reason: "closure type does not throw any error"
9793
)
9894
}
9995
}
@@ -123,19 +119,17 @@ private extension UnneededThrowsRule {
123119
}
124120

125121
override func visitPost(_ node: DoStmtSyntax) {
126-
let doesNotContainCatchClauseWithoutPattern = !node.catchClauses.contains { catchClause in
127-
catchClause.catchItems.isEmpty
128-
}
129-
if doesNotContainCatchClauseWithoutPattern {
130-
scopes.markCurrentScopeAsThrowing()
122+
if node.catchClauses.contains(where: { $0.catchItems.isEmpty }) {
123+
// All errors will be caught.
124+
return
131125
}
126+
scopes.markCurrentScopeAsThrowing()
132127
}
133128

134-
override func visit(_ node: ForStmtSyntax) -> SyntaxVisitorContinueKind {
129+
override func visitPost(_ node: ForStmtSyntax) {
135130
if node.tryKeyword != nil {
136131
scopes.markCurrentScopeAsThrowing()
137132
}
138-
return .visitChildren
139133
}
140134

141135
override func visitPost(_ node: TryExprSyntax) {
@@ -148,12 +142,12 @@ private extension UnneededThrowsRule {
148142
scopes.markCurrentScopeAsThrowing()
149143
}
150144

151-
private func validateScope(_ scope: Scope, reason: String) {
145+
private func validate(scope: Scope, reason: String) {
152146
guard let throwsToken = scope.throwsClause?.throwsSpecifier else { return }
153147
violations.append(
154148
ReasonedRuleViolation(
155149
position: throwsToken.positionAfterSkippingLeadingTrivia,
156-
reason: reason,
150+
reason: "Superfluous 'throws'; " + reason,
157151
correction: ReasonedRuleViolation.ViolationCorrection(
158152
start: throwsToken.positionAfterSkippingLeadingTrivia,
159153
end: throwsToken.endPosition,
@@ -192,9 +186,7 @@ private extension FunctionCallExprSyntax {
192186

193187
private extension PatternBindingSyntax {
194188
var containsInitializerClause: Bool {
195-
children(viewMode: .sourceAccurate).contains { child in
196-
child.is(InitializerClauseSyntax.self)
197-
}
189+
initializer != nil
198190
}
199191

200192
var functionTypeSyntax: FunctionTypeSyntax? {

Source/SwiftLintBuiltInRules/Rules/Lint/UnneededThrowsRuleExamples.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ internal struct UnneededThrowsRuleExamples {
8080
}
8181
"""),
8282
Example("""
83-
func foo() throws{
83+
func foo() throws {
8484
let bar = Bar()
8585
8686
if bar.boolean {
@@ -103,7 +103,7 @@ internal struct UnneededThrowsRuleExamples {
103103
}
104104
"""),
105105
Example("""
106-
func foo() async rethrows {
106+
func foo() async throws {
107107
for try await item in items {}
108108
}
109109
"""),

0 commit comments

Comments
 (0)