Skip to content

Commit d992e0d

Browse files
authored
Merge pull request swiftlang#2775 from ahoppen/empty-body-macro
Support body macros that don’t return any statements
2 parents 28794d2 + 777c126 commit d992e0d

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

Sources/SwiftSyntaxMacroExpansion/MacroSystem.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ private func expandBodyMacro(
462462
conformanceList: nil,
463463
in: context,
464464
indentationWidth: indentationWidth
465-
)
465+
),
466+
!expanded.isEmpty
466467
else {
467468
return nil
468469
}

Tests/SwiftSyntaxMacroExpansionTest/BodyMacroTests.swift

+30
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,34 @@ final class BodyMacroTests: XCTestCase {
125125
indentationWidth: indentationWidth
126126
)
127127
}
128+
129+
func testEmptyBodyMacro() {
130+
struct EmptyBodyMacro: BodyMacro {
131+
public static var formatMode: FormatMode { .disabled }
132+
133+
public static func expansion(
134+
of node: AttributeSyntax,
135+
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
136+
in context: some MacroExpansionContext
137+
) throws -> [CodeBlockItemSyntax] {
138+
return []
139+
}
140+
}
141+
142+
assertMacroExpansion(
143+
"""
144+
@EmptyBody
145+
func f() {
146+
print(42)
147+
}
148+
""",
149+
expandedSource: """
150+
func f() {
151+
print(42)
152+
}
153+
""",
154+
diagnostics: [],
155+
macros: ["EmptyBody": EmptyBodyMacro.self]
156+
)
157+
}
128158
}

0 commit comments

Comments
 (0)