@@ -4,16 +4,30 @@ import SwiftSyntax
4
4
5
5
/// Force-try (`try!`) is forbidden.
6
6
///
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:
9
8
/// * Contains the line `import XCTest`
10
9
///
11
10
/// Lint: Using `try!` results in a lint error.
12
11
///
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
15
13
///
16
14
/// - SeeAlso: https://google.github.io/swift#error-types
17
- public final class NeverUseForceTry : SyntaxFormatRule {
15
+ public final class NeverUseForceTry : SyntaxLintRule {
18
16
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 " )
19
33
}
0 commit comments