@@ -4,16 +4,30 @@ import SwiftSyntax
44
55/// Force-try (`try!`) is forbidden.
66///
7- /// This rule does not apply to test code, defined as code which matches one or more of:
8- /// * Parent directory named "Tests"
7+ /// This rule does not apply to test code, defined as code which:
98/// * Contains the line `import XCTest`
109///
1110/// Lint: Using `try!` results in a lint error.
1211///
13- /// Format: The use of `try!` is replaced with a `try`...`catch` block where the `catch` block
14- /// contains `fatalError("TODO(<username>): document before submitting")`
12+ /// TODO: Create exception for NSRegularExpression
1513///
1614/// - SeeAlso: https://google.github.io/swift#error-types
17- public final class NeverUseForceTry : SyntaxFormatRule {
15+ public final class NeverUseForceTry : SyntaxLintRule {
1816
17+ public override func visit( _ node: SourceFileSyntax ) {
18+ setImportsXCTest ( context: context, sourceFile: node)
19+ super. visit ( node)
20+ }
21+
22+ public override func visit( _ node: TryExprSyntax ) {
23+ guard !context. importsXCTest else { return }
24+ guard let mark = node. questionOrExclamationMark else { return }
25+ if mark. tokenKind == . exclamationMark {
26+ diagnose ( . doNotForceTry, on: node. tryKeyword)
27+ }
28+ }
29+ }
30+
31+ extension Diagnostic . Message {
32+ static let doNotForceTry = Diagnostic . Message ( . warning, " do not use force try " )
1933}
0 commit comments