mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
43 lines
1.3 KiB
Swift
43 lines
1.3 KiB
Swift
//
|
|
// EmptyCountRule.swift
|
|
// SwiftLint
|
|
//
|
|
// Created by JP Simard on 2015-11-17.
|
|
// Copyright (c) 2015 Realm. All rights reserved.
|
|
//
|
|
|
|
import SourceKittenFramework
|
|
|
|
public struct EmptyCountRule: ConfigurationProviderRule, OptInRule {
|
|
public var configuration = SeverityConfiguration(.error)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "empty_count",
|
|
name: "Empty Count",
|
|
description: "Prefer checking `isEmpty` over comparing `count` to zero.",
|
|
nonTriggeringExamples: [
|
|
"var count = 0\n",
|
|
"[Int]().isEmpty\n",
|
|
"[Int]().count > 1\n",
|
|
"[Int]().count == 1\n"
|
|
],
|
|
triggeringExamples: [
|
|
"[Int]().count == 0\n",
|
|
"[Int]().count > 0\n",
|
|
"[Int]().count != 0\n"
|
|
]
|
|
)
|
|
|
|
public func validateFile(_ file: File) -> [StyleViolation] {
|
|
let pattern = "count\\s*(==|!=|<|<=|>|>=)\\s*0"
|
|
let excludingKinds = SyntaxKind.commentAndStringKinds()
|
|
return file.matchPattern(pattern, excludingSyntaxKinds: excludingKinds).map {
|
|
StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file, byteOffset: $0.location))
|
|
}
|
|
}
|
|
}
|