forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
contains_over_range_not_nil
rule, make `contains_over_first_not…
…_nil` also match == nil (realm#2811)
- Loading branch information
1 parent
ffb2f4f
commit 2c07615
Showing
8 changed files
with
178 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
Source/SwiftLintFramework/Rules/Performance/ContainsOverRangeNilComparisonRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
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: [ | ||
"let range = myString.range(of: \"Test\")", | ||
"myString.contains(\"Test\")", | ||
"!myString.contains(\"Test\")", | ||
"resourceString.range(of: rule.regex, options: .regularExpression) != nil" | ||
], | ||
triggeringExamples: ["!=", "=="].flatMap { comparison in | ||
return [ | ||
"↓myString.range(of: \"Test\") \(comparison) nil" | ||
] | ||
} | ||
) | ||
|
||
public func validate(file: File) -> [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"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters