diff --git a/Sources/FormattingHelpers.swift b/Sources/FormattingHelpers.swift index 428b3150..fedcae99 100644 --- a/Sources/FormattingHelpers.swift +++ b/Sources/FormattingHelpers.swift @@ -2441,12 +2441,12 @@ extension Formatter { processDeclaredVariables(at: &i, names: &members) } } else { - let removeSelf = explicitSelf != .insert && !usingDynamicLookup && !inClosureDisallowingImplicitSelf - let onlyLocal = options.swiftVersion < "5" - + let removeSelf = explicitSelf != .insert && !usingDynamicLookup && ( + (staticSelf && classOrStatic) || (!staticSelf && !inClosureDisallowingImplicitSelf) + ) processDeclaredVariables(at: &i, names: &localNames, removeSelfKeyword: removeSelf ? selfKeyword : nil, - onlyLocal: onlyLocal, + onlyLocal: options.swiftVersion < "5", scopeAllowsImplicitSelfRebinding: scopeAllowsImplicitSelfRebinding) } case .keyword("func"): @@ -2586,7 +2586,9 @@ extension Formatter { assert(!isTypeRoot) // Guard is included because it's an error to reference guard vars in body var scopedNames = localNames - let removeSelf = explicitSelf != .insert && !inClosureDisallowingImplicitSelf + let removeSelf = explicitSelf != .insert && !usingDynamicLookup && ( + (staticSelf && classOrStatic) || (!staticSelf && !inClosureDisallowingImplicitSelf) + ) processDeclaredVariables( at: &index, names: &scopedNames, removeSelfKeyword: removeSelf ? selfKeyword : nil, diff --git a/Tests/RulesTests+Redundancy.swift b/Tests/RulesTests+Redundancy.swift index 06e4e091..b560aeee 100644 --- a/Tests/RulesTests+Redundancy.swift +++ b/Tests/RulesTests+Redundancy.swift @@ -6433,6 +6433,19 @@ class RedundancyTests: RulesTests { testFormatting(for: input, rule: FormatRules.redundantStaticSelf) } + func testPreserveStaticSelfInInstanceFunction() { + let input = """ + enum Foo { + static var value = 0 + + func f() { + Self.value = value + } + } + """ + testFormatting(for: input, rule: FormatRules.redundantStaticSelf) + } + func testPreserveStaticSelfForShadowedProperty() { let input = """ enum Foo { @@ -6448,7 +6461,7 @@ class RedundancyTests: RulesTests { func testPreserveStaticSelfInGetter() { let input = """ - public enum Foo { + enum Foo { static let foo: String = "foo" var sharedFoo: String { @@ -6481,6 +6494,20 @@ class RedundancyTests: RulesTests { testFormatting(for: input, output, rule: FormatRules.redundantStaticSelf) } + func testPreserveStaticSelfInGuardLet() { + let input = """ + class LocationDeeplink: Deeplink { + convenience init?(warnRegion: String) { + guard let value = Self.location(for: warnRegion) else { + return nil + } + self.init(location: value) + } + } + """ + testFormatting(for: input, rule: FormatRules.redundantStaticSelf) + } + // MARK: - semicolons func testSemicolonRemovedAtEndOfLine() {