File tree 1 file changed +6
-1
lines changed
1 file changed +6
-1
lines changed Original file line number Diff line number Diff line change @@ -23,8 +23,8 @@ import SwiftSyntax
23
23
/// - SeeAlso: https://google.github.io/swift#force-unwrapping-and-force-casts
24
24
public final class NeverForceUnwrap : SyntaxLintRule {
25
25
26
- // Checks if "XCTest" is an import statement
27
26
public override func visit( _ node: SourceFileSyntax ) {
27
+ // Tracks whether "XCTest" is imported in the source file before processing the individual
28
28
setImportsXCTest ( context: context, sourceFile: node)
29
29
super. visit ( node)
30
30
}
@@ -35,7 +35,11 @@ public final class NeverForceUnwrap: SyntaxLintRule {
35
35
}
36
36
37
37
public override func visit( _ node: AsExprSyntax ) {
38
+ // Only fire if we're not in a test file and if there is an exclamation mark following the `as`
39
+ // keyword.
38
40
guard !context. importsXCTest else { return }
41
+ guard let questionOrExclamation = node. questionOrExclamationMark else { return }
42
+ guard questionOrExclamation. tokenKind == . exclamationMark else { return }
39
43
diagnose ( . doNotForceCast( name: node. typeName. description) , on: node)
40
44
}
41
45
}
@@ -44,6 +48,7 @@ extension Diagnostic.Message {
44
48
static func doNotForceUnwrap( name: String ) -> Diagnostic . Message {
45
49
return . init( . warning, " do not force unwrap ' \( name) ' " )
46
50
}
51
+
47
52
static func doNotForceCast( name: String ) -> Diagnostic . Message {
48
53
return . init( . warning, " do not force cast to ' \( name) ' " )
49
54
}
You can’t perform that action at this time.
0 commit comments