-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathForceCastRule.swift
33 lines (29 loc) · 1.06 KB
/
ForceCastRule.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import SwiftSyntax
@SwiftSyntaxRule
struct ForceCastRule: Rule {
var configuration = SeverityConfiguration<Self>(.error)
static let description = RuleDescription(
identifier: "force_cast",
name: "Force Cast",
description: "Force casts should be avoided",
kind: .idiomatic,
nonTriggeringExamples: [
Example("NSNumber() as? Int")
],
triggeringExamples: [ Example("NSNumber() ↓as! Int") ]
)
}
private extension ForceCastRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: AsExprSyntax) {
if node.questionOrExclamationMark?.tokenKind == .exclamationMark {
violations.append(node.asKeyword.positionAfterSkippingLeadingTrivia)
}
}
override func visitPost(_ node: UnresolvedAsExprSyntax) {
if node.questionOrExclamationMark?.tokenKind == .exclamationMark {
violations.append(node.asKeyword.positionAfterSkippingLeadingTrivia)
}
}
}
}