Skip to content

Commit

Permalink
Fix redundant_optional_initialization autocorrect (realm#3717)
Browse files Browse the repository at this point in the history
* Fix autocorrect for redundant_optional_initialization in case observer's brace exists.
  • Loading branch information
naru-jpn authored Sep 30, 2021
1 parent b63a60d commit e4ff164
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

* Fixes a bug with the `missing_docs` rule where `excludes_inherited_types` would not be set.

* Fix redundant_optional_initialization autocorrect broken
in case observer's brace exists.
[Naruki Chigira](https://github.com/naru-jpn)
[#3718](https://github.com/realm/SwiftLint/issues/3718)

* Fix a false positive in the `unneeded_break_in_switch` rule when
using `do/catch`.
[Marcelo Fabri](https://github.com/marcelofabri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,27 @@ public struct RedundantOptionalInitializationRule: SubstitutionCorrectableASTRul
Example("var myVar: Int?↓=nil\n"): Example("var myVar: Int?\n"),
Example("var myVar: Optional<Int>↓=nil\n"): Example("var myVar: Optional<Int>\n"),
Example("class C {\n#if true\nvar myVar: Int?↓ = nil\n#endif\n}"):
Example("class C {\n#if true\nvar myVar: Int?\n#endif\n}")
Example("class C {\n#if true\nvar myVar: Int?\n#endif\n}"),
Example("""
var myVar: Int?↓ = nil {
didSet { }
}
"""):
Example("""
var myVar: Int? {
didSet { }
}
"""),
Example("""
var myVar: Int?↓=nil{
didSet { }
}
"""):
Example("""
var myVar: Int?{
didSet { }
}
""")
]

guard SwiftVersion.current >= .fourDotOne else {
Expand All @@ -104,7 +124,7 @@ public struct RedundantOptionalInitializationRule: SubstitutionCorrectableASTRul
return corrections
}()

private let pattern = "\\s*=\\s*nil\\b\\s*\\{?"
private let pattern = "(\\s*=\\s*nil\\b)\\s*\\{?"

public func validate(file: SwiftLintFile, kind: SwiftDeclarationKind,
dictionary: SourceKittenDictionary) -> [StyleViolation] {
Expand All @@ -126,13 +146,22 @@ public struct RedundantOptionalInitializationRule: SubstitutionCorrectableASTRul
typeIsOptional(type),
!dictionary.enclosedSwiftAttributes.contains(.lazy),
dictionary.isMutableVariable(file: file),
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 {
let range = range(for: dictionary, file: file) else {
return []
}

let regularExpression = regex(pattern)
guard let match = regularExpression.matches(in: file.stringView, range: range).first,
match.range.location == range.location + range.length - match.range.length else {
return []
}

return [match]
let matched = file.stringView.substring(with: match.range)
if matched.hasSuffix("{"), match.numberOfRanges > 1 {
return [match.range(at: 1)]
} else {
return [match.range]
}
}

private func range(for dictionary: SourceKittenDictionary, file: SwiftLintFile) -> NSRange? {
Expand Down

0 comments on commit e4ff164

Please sign in to comment.