forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnusedOptionalBindingRule.swift
73 lines (65 loc) · 2.76 KB
/
UnusedOptionalBindingRule.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// UnusedOptionalBindingRule.swift
// SwiftLint
//
// Created by Rafael Machado on 1/5/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct UnusedOptionalBindingRule: ASTRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "unused_optional_binding",
name: "Unused Optional Binding",
description: "Prefer `!= nil` over `let _ =`",
nonTriggeringExamples: [
"if let bar = Foo.optionalValue {\n" +
"}\n",
"if let (_, second) = getOptionalTuple() {\n" +
"}\n",
"if let (_, asd, _) = getOptionalTuple(), let bar = Foo.optionalValue {\n" +
"}\n"
],
triggeringExamples: [
"if let ↓_ = Foo.optionalValue {\n" +
"}\n",
"if let a = Foo.optionalValue, let ↓_ = Foo.optionalValue2 {\n" +
"}\n",
"guard let a = Foo.optionalValue, let ↓_ = Foo.optionalValue2 {\n" +
"}\n",
"if let (first, second) = getOptionalTuple(), let ↓_ = Foo.optionalValue {\n" +
"}\n",
"if let (first, _) = getOptionalTuple(), let ↓_ = Foo.optionalValue {\n" +
"}\n",
"if let (_, second) = getOptionalTuple(), let ↓_ = Foo.optionalValue {\n" +
"}\n",
"if let ↓(_, _, _) = getOptionalTuple(), let bar = Foo.optionalValue {\n" +
"}\n"
]
)
public func validate(file: File,
kind: StatementKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard kind == .if || kind == .guard,
let offset = dictionary.offset,
let length = dictionary.length,
let range = file.contents.bridge().byteRangeToNSRange(start: offset, length: length) else {
return []
}
return violations(in: range, of: file).map {
StyleViolation(ruleDescription: type(of: self).description, severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}
private func violations(in range: NSRange, of file: File) -> [NSRange] {
let kinds = SyntaxKind.commentAndStringKinds()
let underlineOutsideParenthesis = "(?<=[^(]\\s)_(?=\\s[^)])"
let underlineInsideParenthesis = "\\((\\s*[_,]\\s*)+\\)"
let pattern = underlineOutsideParenthesis + "|" + underlineInsideParenthesis
return file.match(pattern: pattern,
excludingSyntaxKinds: kinds,
range: range)
}
}