From d429d06080da5c3e240e14e285535d8a455eb0dc Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Mon, 20 Jan 2025 08:06:57 -0800 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20remove=20`#if`=20attributes=20w?= =?UTF-8?q?ith=20`AttributeRemover`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #2923 rdar://141840779 --- .../SwiftSyntaxMacroExpansion/MacroSystem.swift | 6 +++++- .../AttributeRemoverTests.swift | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift b/Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift index e4e923f794b..b77cf33ec02 100644 --- a/Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift +++ b/Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift @@ -530,7 +530,11 @@ public class AttributeRemover: SyntaxRewriter { public override func visit(_ node: AttributeListSyntax) -> AttributeListSyntax { var filteredAttributes: [AttributeListSyntax.Element] = [] - for case .attribute(let attribute) in node { + for attribute in node { + guard case .attribute(let attribute) = attribute else { + filteredAttributes.append(attribute) + continue + } if self.predicate(attribute) { var leadingTrivia = attribute.leadingTrivia diff --git a/Tests/SwiftSyntaxMacroExpansionTest/AttributeRemoverTests.swift b/Tests/SwiftSyntaxMacroExpansionTest/AttributeRemoverTests.swift index 1858bbd92bd..fe8a41e6fbe 100644 --- a/Tests/SwiftSyntaxMacroExpansionTest/AttributeRemoverTests.swift +++ b/Tests/SwiftSyntaxMacroExpansionTest/AttributeRemoverTests.swift @@ -470,4 +470,21 @@ final class AttributeRemoverTests: XCTestCase { """ ) } + + func testKeepPoundIfInAttributes() { + assertSyntaxRemovingTestAttributes( + """ + #if true + @inlinable + #endif + func f() {} + """, + reduction: """ + #if true + @inlinable + #endif + func f() {} + """ + ) + } }