mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
fcf848608e
* Add Example wrapper in order to display test failures inline when running in Xcode. * Stop using Swift 5.1-only features so we can compile on Xcode 10.2. * Wrap strings in Example. * Add Changelog entry. * Wrap all examples in Example struct. * Better and more complete capturing of line numbers. * Fix broken test. * Better test traceability. * Address or disable linting warnings. * Add documentation comments. * Disable linter for a few cases. * Limit mutability and add copy-and-mutate utility functions. * Limit scope of mutability.
36 lines
1.5 KiB
Swift
36 lines
1.5 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct ContainsOverRangeNilComparisonRule: CallPairRule, OptInRule, ConfigurationProviderRule,
|
|
AutomaticTestableRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "contains_over_range_nil_comparison",
|
|
name: "Contains over range(of:) comparison to nil",
|
|
description: "Prefer `contains` over `range(of:) != nil` and `range(of:) == nil`.",
|
|
kind: .performance,
|
|
nonTriggeringExamples: [
|
|
Example("let range = myString.range(of: \"Test\")"),
|
|
Example("myString.contains(\"Test\")"),
|
|
Example("!myString.contains(\"Test\")"),
|
|
Example("resourceString.range(of: rule.regex, options: .regularExpression) != nil")
|
|
],
|
|
triggeringExamples: ["!=", "=="].flatMap { comparison in
|
|
return [
|
|
Example("↓myString.range(of: \"Test\") \(comparison) nil")
|
|
]
|
|
}
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile) -> [StyleViolation] {
|
|
let pattern = "\\)\\s*(==|!=)\\s*nil"
|
|
return validate(file: file, pattern: pattern, patternSyntaxKinds: [.keyword],
|
|
callNameSuffix: ".range", severity: configuration.severity,
|
|
reason: "Prefer `contains` over range(of:) comparison to nil") { expression in
|
|
return expression.enclosedArguments.map { $0.name } == ["of"]
|
|
}
|
|
}
|
|
}
|