forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainsOverFirstNotNilRule.swift
More file actions
43 lines (37 loc) · 1.5 KB
/
ContainsOverFirstNotNilRule.swift
File metadata and controls
43 lines (37 loc) · 1.5 KB
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
//
// ContainsOverFirstNotNilRule.swift
// SwiftLint
//
// Created by Samuel Susla on 17/09/17.
// Copyright © 2017 Realm. All rights reserved.
//
import SourceKittenFramework
public struct ContainsOverFirstNotNilRule: CallPairRule, OptInRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "contains_over_first_not_nil",
name: "Contains over first not nil",
description: "Prefer `contains` over `first(where:) != nil`",
kind: .performance,
nonTriggeringExamples: [
"let first = myList.first(where: { $0 % 2 == 0 })\n",
"let first = myList.first { $0 % 2 == 0 }\n"
],
triggeringExamples: [
"↓myList.first { $0 % 2 == 0 } != nil\n",
"↓myList.first(where: { $0 % 2 == 0 }) != nil\n",
"↓myList.map { $0 + 1 }.first(where: { $0 % 2 == 0 }) != nil\n",
"↓myList.first(where: someFunction) != nil\n",
"↓myList.map { $0 + 1 }.first { $0 % 2 == 0 } != nil\n",
"(↓myList.first { $0 % 2 == 0 }) != nil\n"
]
)
public func validate(file: File) -> [StyleViolation] {
return validate(file: file,
pattern: "[\\}\\)]\\s*!=\\s*nil",
patternSyntaxKinds: [.keyword],
callNameSuffix: ".first",
severity: configuration.severity)
}
}