Files
Danny Mösch 40bd97038a Support arbitrary configurations in @SwiftSyntaxRule (#5275)
Almost all rules based on SwiftSyntax can be set up now by just adding
`@SwiftSyntaxRule` to the rule struct.
2023-10-16 19:34:43 +02:00

38 lines
981 B
Swift

import SwiftSyntax
@SwiftSyntaxRule
struct ForceTryRule: Rule {
var configuration = SeverityConfiguration<Self>(.error)
static let description = RuleDescription(
identifier: "force_try",
name: "Force Try",
description: "Force tries should be avoided",
kind: .idiomatic,
nonTriggeringExamples: [
Example("""
func a() throws {}
do {
try a()
} catch {}
""")
],
triggeringExamples: [
Example("""
func a() throws {}
↓try! a()
""")
]
)
}
private extension ForceTryRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: TryExprSyntax) {
if node.questionOrExclamationMark?.tokenKind == .exclamationMark {
violations.append(node.positionAfterSkippingLeadingTrivia)
}
}
}
}