Skip to content

Emit a diagnostic if a display name string is empty #1256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Sources/TestingMacros/Support/AttributeDiscovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
import SwiftParser

/// A syntax rewriter that removes leading `Self.` tokens from member access
/// expressions in a syntax tree.
Expand Down Expand Up @@ -149,6 +150,16 @@ struct AttributeInfo {
}
}

// If there was a display name but it's completely empty, emit a warning
// diagnostic since this can cause confusion isn't generally recommended.
// Note that this is only possible for string literal display names; the
// compiler enforces that raw identifiers must be non-empty.
if let namedDecl = declaration.asProtocol((any NamedDeclSyntax).self),
let displayName, let displayNameArgument,
displayName.representedLiteralValue?.isEmpty == true {
context.diagnose(.declaration(namedDecl, hasEmptyDisplayName: displayName, fromArgument: displayNameArgument, using: attribute))
}

// Remove leading "Self." expressions from the arguments of the attribute.
// See _SelfRemover for more information. Rewriting a syntax tree discards
// location information from the copy, so only invoke the rewriter if the
Expand Down
31 changes: 31 additions & 0 deletions Sources/TestingMacros/Support/DiagnosticMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,37 @@ struct DiagnosticMessage: SwiftDiagnostics.DiagnosticMessage {
)
}

/// Create a diagnostic message stating that a string literal expression
/// passed as the display name to a `@Test` or `@Suite` attribute is empty
/// but should not be.
///
/// - Parameters:
/// - decl: The declaration that has an empty display name.
/// - displayNameExpr: The display name string literal expression.
/// - argumentContainingDisplayName: The argument node containing the node
/// `displayNameExpr`.
/// - attribute: The `@Test` or `@Suite` attribute.
///
/// - Returns: A diagnostic message.
static func declaration(
_ decl: some NamedDeclSyntax,
hasEmptyDisplayName displayNameExpr: StringLiteralExprSyntax,
fromArgument argumentContainingDisplayName: LabeledExprListSyntax.Element,
using attribute: AttributeSyntax
) -> Self {
Self(
syntax: Syntax(displayNameExpr),
message: "Attribute \(_macroName(attribute)) specifies an empty display name for \(_kindString(for: decl))",
severity: .warning,
fixIts: [
FixIt(
message: MacroExpansionFixItMessage("Remove '\(displayNameExpr.representedLiteralValue!)'"),
changes: [.replace(oldNode: Syntax(argumentContainingDisplayName), newNode: Syntax("" as ExprSyntax))]
),
]
)
}

/// Create a diagnostic message stating that a declaration has two display
/// names.
///
Expand Down
8 changes: 8 additions & 0 deletions Tests/TestingMacrosTests/TestDeclarationMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ struct TestDeclarationMacroTests {
// .serialized on a non-parameterized test function
"@Test(.serialized) func f() {}":
"Trait '.serialized' has no effect when used with a non-parameterized test function",

// empty display name string literal
#"@Test("") func f() {}"#:
"Attribute 'Test' specifies an empty display name for function",
##"@Test(#""#) func f() {}"##:
"Attribute 'Test' specifies an empty display name for function",
#"@Suite("") struct S {}"#:
"Attribute 'Suite' specifies an empty display name for structure",
]
)
func apiMisuseWarnings(input: String, expectedMessage: String) throws {
Expand Down