mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
44 lines
1.5 KiB
Swift
44 lines
1.5 KiB
Swift
//
|
|
// 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)
|
|
}
|
|
|
|
}
|