Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive on redundant_optional_initialization #1164

Merged
merged 1 commit into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#1156](https://github.com/realm/SwiftLint/issues/1156)

* Fix false positive on `redundant_optional_initialization` rule.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1159](https://github.com/realm/SwiftLint/issues/1159)

## 0.16.0: Maximum Energy Efficiency Setting

##### Breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ public struct RedundantOptionalInitializationRule: ASTRule, CorrectableRule, Con
"func foo(bar: Int? = 0) { }\n",
"var myVar: Optional<Int>\n",
"let myVar: Optional<Int> = nil\n",
"var myVar: Optional<Int> = 0\n"
"var myVar: Optional<Int> = 0\n",
// properties with body should be ignored
"var foo: Int? {\n" +
" if bar != nil { }\n" +
" return 0\n" +
"}\n",
// properties with a closure call
"var foo: Int? = {\n" +
" if bar != nil { }\n" +
" return 0\n" +
"}()\n"
],
triggeringExamples: [
"var myVar: Int?↓ = nil\n",
Expand Down Expand Up @@ -59,17 +69,29 @@ public struct RedundantOptionalInitializationRule: ASTRule, CorrectableRule, Con
dictionary["key.setter_accessibility"] != nil,
let type = dictionary["key.typename"] as? String,
typeIsOptional(type),
case let contents = file.contents.bridge(),
let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }),
let length = (dictionary["key.length"] as? Int64).flatMap({ Int($0) }),
let range = contents.byteRangeToNSRange(start: offset, length: length),
let match = file.match(pattern: pattern, with: [.keyword], range: range).first else {
let range = range(for: dictionary, file: file),
let match = file.match(pattern: pattern, with: [.keyword], range: range).first,
match.location == range.location + range.length - match.length else {
return []
}

return [match]
}

private func range(for dictionary: [String: SourceKitRepresentable], file: File) -> NSRange? {
guard let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }),
let length = (dictionary["key.length"] as? Int64).flatMap({ Int($0) }) else {
return nil
}

let contents = file.contents.bridge()
if let bodyOffset = (dictionary["key.bodyoffset"] as? Int64).flatMap({ Int($0) }) {
return contents.byteRangeToNSRange(start: offset, length: bodyOffset - offset)
} else {
return contents.byteRangeToNSRange(start: offset, length: length)
}
}

private func violationRanges(in file: File, dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
return dictionary.substructure.flatMap { subDict -> [NSRange] in
guard let kindString = subDict["key.kind"] as? String,
Expand Down