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() {} + """ + ) + } }