Fix redundantInit removing required init in some cases

This commit is contained in:
Nick Lockwood
2022-05-01 21:55:01 +01:00
parent a6888dca3d
commit 5ef8bba345
3 changed files with 43 additions and 2 deletions
+1 -1
View File
@@ -793,7 +793,7 @@ Known issues
* When using the `--self remove` option, the `redundantSelf` rule will remove references to `self` in autoclosure arguments, which may change the meaning of the code, or cause it not to compile. To work around this issue, use the `--selfrequired` option to provide a comma-delimited list of methods to be excluded from the rule. The `expect()` function from the popular [Nimble](https://github.com/Quick/Nimble) unit testing framework is already excluded by default. If you are using the `--self insert` option then this is not an issue.
* If you assign `SomeClass.self` to a variable and then instantiate an instance of the class using that variable, Swift requires that you use an explicit `.init()`, however, the `redundantInit` rule is not currently capable of detecting this situation and will remove the `.init`. To work around this issue, use the `// swiftformat:disable:next redundantInit` comment directive to disable the rule for any affected lines of code (or just disable the `redundantInit` rule completely).
* If you assign `SomeClass.self` to a variable and then instantiate an instance of the class using that variable, Swift requires that you use an explicit `.init()`, however, the `redundantInit` rule is not currently capable of detecting this situation in all cases, and may remove the `.init`. To work around this issue, use the `// swiftformat:disable:next redundantInit` comment directive to disable the rule for any affected lines of code (or just disable the `redundantInit` rule completely).
* The `--self insert` option can only recognize locally declared member variables, not ones inherited from superclasses or extensions in other files, so it cannot insert missing `self` references for those. Note that the reverse is not true: `--self remove` should remove *all* redundant `self` references.
+15 -1
View File
@@ -4632,7 +4632,21 @@ public struct _FormatRules {
firstChar != "$", String(firstChar).uppercased() == String(firstChar) else {
return
}
var j = dotIndex
while let prevIndex = formatter.index(
of: prevToken, before: j
) ?? formatter.index(
of: .startOfScope, before: j
) {
j = prevIndex
if prevToken == formatter.tokens[prevIndex],
let prevPrevToken = formatter.last(
.nonSpaceOrCommentOrLinebreak, before: prevIndex
), [.keyword("let"), .keyword("var")].contains(prevPrevToken)
{
return
}
}
formatter.removeTokens(in: i + 1 ..< openParenIndex)
formatter.removeTokens(in: dotIndex ... i)
}
+27
View File
@@ -762,6 +762,33 @@ class RedundancyTests: RulesTests {
testFormatting(for: input, rule: FormatRules.redundantInit)
}
func testNoRemoveInitForLowercaseType() {
let input = """
let foo = bar.init()
"""
testFormatting(for: input, rule: FormatRules.redundantInit)
}
func testNoRemoveInitForLocalLetType() {
let input = """
let Foo = Foo.self
let foo = Foo.init()
"""
testFormatting(for: input, rule: FormatRules.redundantInit)
}
func testNoRemoveInitForLocalLetType2() {
let input = """
let Foo = Foo.self
if x {
return Foo.init(x)
} else {
return Foo.init(y)
}
"""
testFormatting(for: input, rule: FormatRules.redundantInit)
}
// MARK: - redundantLetError
func testCatchLetError() {