mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
38 lines
983 B
Swift
38 lines
983 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)
|
|
}
|
|
}
|
|
}
|
|
}
|