mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
* 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.
97 lines
2.7 KiB
Swift
97 lines
2.7 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct LegacyHashingRule: ASTRule, ConfigurationProviderRule, AutomaticTestableRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "legacy_hashing",
|
|
name: "Legacy Hashing",
|
|
description: "Prefer using the `hash(into:)` function instead of overriding `hashValue`",
|
|
kind: .idiomatic,
|
|
minSwiftVersion: .fourDotTwo,
|
|
nonTriggeringExamples: [
|
|
Example("""
|
|
struct Foo: Hashable {
|
|
let bar: Int = 10
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
hasher.combine(bar)
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo: Hashable {
|
|
let bar: Int = 10
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
hasher.combine(bar)
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
var hashValue: Int { return 1 }
|
|
class Foo: Hashable { \n }
|
|
"""),
|
|
Example("""
|
|
class Foo: Hashable {
|
|
let bar: String = "Foo"
|
|
|
|
public var hashValue: String {
|
|
return bar
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo: Hashable {
|
|
let bar: String = "Foo"
|
|
|
|
public var hashValue: String {
|
|
get { return bar }
|
|
set { bar = newValue }
|
|
}
|
|
}
|
|
""")
|
|
],
|
|
triggeringExamples: [
|
|
Example("""
|
|
struct Foo: Hashable {
|
|
let bar: Int = 10
|
|
|
|
public ↓var hashValue: Int {
|
|
return bar
|
|
}
|
|
}
|
|
"""),
|
|
Example("""
|
|
class Foo: Hashable {
|
|
let bar: Int = 10
|
|
|
|
public ↓var hashValue: Int {
|
|
return bar
|
|
}
|
|
}
|
|
""")
|
|
]
|
|
)
|
|
|
|
// MARK: - ASTRule
|
|
|
|
public func validate(file: SwiftLintFile,
|
|
kind: SwiftDeclarationKind,
|
|
dictionary: SourceKittenDictionary) -> [StyleViolation] {
|
|
guard kind == .varInstance,
|
|
dictionary.setterAccessibility == nil,
|
|
dictionary.typeName == "Int",
|
|
dictionary.name == "hashValue",
|
|
let offset = dictionary.offset else {
|
|
return []
|
|
}
|
|
|
|
return [StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file, byteOffset: offset))]
|
|
}
|
|
}
|