diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 02c163df..450a2fe0 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -1901,7 +1901,10 @@ public struct _FormatRules { // Check specifiers don't include `lazy` formatter.forEach(.keyword("var")) { i, _ in - if formatter.specifiersForType(at: i, contains: "lazy") { + if formatter.specifiersForType(at: i, contains: { + let string = $1.string + return string == "lazy" || (string != "@objc" && string.hasPrefix("@")) + }) { return // Can't remove the init } // Check this isn't a Codable diff --git a/Tests/RulesTests.swift b/Tests/RulesTests.swift index 161331c5..e1a53841 100644 --- a/Tests/RulesTests.swift +++ b/Tests/RulesTests.swift @@ -5039,6 +5039,20 @@ class RulesTests: XCTestCase { XCTAssertEqual(try format(input + "\n", rules: FormatRules.all), output + "\n") } + func testNoRemoveNilInitWithCustomAttribute() { + let input = "@Foo var foo: Int? = nil" + let output = input + XCTAssertEqual(try format(input, rules: [FormatRules.redundantNilInit]), output) + XCTAssertEqual(try format(input + "\n", rules: FormatRules.all), output + "\n") + } + + func testRemoveNilInitWithObjcAttributes() { + let input = "@objc var foo: Int? = nil" + let output = "@objc var foo: Int?" + XCTAssertEqual(try format(input, rules: [FormatRules.redundantNilInit]), output) + XCTAssertEqual(try format(input + "\n", rules: FormatRules.all), output + "\n") + } + // MARK: redundantLet func testRemoveRedundantLet() { diff --git a/Tests/XCTestManifests.swift b/Tests/XCTestManifests.swift index b5968ff7..52ab0939 100644 --- a/Tests/XCTestManifests.swift +++ b/Tests/XCTestManifests.swift @@ -490,6 +490,7 @@ extension RulesTests { ("testEnumCaseIndentingCommas", testEnumCaseIndentingCommas), ("testEnumCaseIndentingCommasWithXcodeStyle", testEnumCaseIndentingCommasWithXcodeStyle), ("testEnumCaseNotEqualYodaCondition", testEnumCaseNotEqualYodaCondition), + ("testEnumCaseWrappedIfWithXcodeStyle", testEnumCaseWrappedIfWithXcodeStyle), ("testEnumIfCaseEndifIndenting", testEnumIfCaseEndifIndenting), ("testExponentialGrouping", testExponentialGrouping), ("testExpressionCountEqualsZero", testExpressionCountEqualsZero), @@ -571,7 +572,6 @@ extension RulesTests { ("testIfEndifInsideEnumWithTrailingCommentOutdenting", testIfEndifInsideEnumWithTrailingCommentOutdenting), ("testIfEndifNoIndenting", testIfEndifNoIndenting), ("testIfEndifOutdenting", testIfEndifOutdenting), - ("testEnumCaseWrappedIfWithXcodeStyle", testEnumCaseWrappedIfWithXcodeStyle), ("testIfFunctionAndReplaced", testIfFunctionAndReplaced), ("testIfUnknownCaseEndifIndenting", testIfUnknownCaseEndifIndenting), ("testIfUnknownCaseEndifIndenting2", testIfUnknownCaseEndifIndenting2), @@ -828,6 +828,7 @@ extension RulesTests { ("testNoRemoveLinebreakAfterCommentInElements", testNoRemoveLinebreakAfterCommentInElements), ("testNoRemoveMethodSignature", testNoRemoveMethodSignature), ("testNoRemoveMultilineGetSet", testNoRemoveMultilineGetSet), + ("testNoRemoveNilInitWithCustomAttribute", testNoRemoveNilInitWithCustomAttribute), ("testNoRemoveNonNilInit", testNoRemoveNonNilInit), ("testNoRemoveNonRedundantNestedFunctionSelf", testNoRemoveNonRedundantNestedFunctionSelf), ("testNoRemoveNonRedundantNestedFunctionSelf2", testNoRemoveNonRedundantNestedFunctionSelf2), @@ -1114,6 +1115,7 @@ extension RulesTests { ("testRemoveInternalFunctionArgumentName", testRemoveInternalFunctionArgumentName), ("testRemoveMultilineIsolatedGet", testRemoveMultilineIsolatedGet), ("testRemoveMultipleRedundantNilInitsInSameLine", testRemoveMultipleRedundantNilInitsInSameLine), + ("testRemoveNilInitWithObjcAttributes", testRemoveNilInitWithObjcAttributes), ("testRemoveRedundantBackticksInLet", testRemoveRedundantBackticksInLet), ("testRemoveRedundantEmptyReturnType", testRemoveRedundantEmptyReturnType), ("testRemoveRedundantImplicitUnwrapInit", testRemoveRedundantImplicitUnwrapInit),