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 1 commit
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
1 change: 1 addition & 0 deletions Sources/TestingMacros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ target_sources(TestingMacros PRIVATE
Support/Additions/FunctionDeclSyntaxAdditions.swift
Support/Additions/IntegerLiteralExprSyntaxAdditions.swift
Support/Additions/MacroExpansionContextAdditions.swift
Support/Additions/StringLiteralExprSyntaxAdditions.swift
Support/Additions/TokenSyntaxAdditions.swift
Support/Additions/TriviaPieceAdditions.swift
Support/Additions/TypeSyntaxProtocolAdditions.swift
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

import SwiftSyntax

extension StringLiteralExprSyntax {
/// Whether or not this string literal expression is completely empty.
///
/// The value of this property is `false` even if the literal contains one or
/// more interpolation segments whose evaluated expressions are an empty
/// string. The value is only `true` if this string literal contains only one
/// empty string segment.
var isEmptyLiteral: Bool {
guard segments.count == 1,
case let .stringSegment(stringSegment) = segments.first,
case let .stringSegment(text) = stringSegment.content.tokenKind
else {
return false
}

return text.isEmpty
}
}
8 changes: 8 additions & 0 deletions Sources/TestingMacros/Support/AttributeDiscovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ 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 displayName, displayName.isEmptyLiteral {
context.diagnose(.emptyDisplayName(displayName))
}

// 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
16 changes: 16 additions & 0 deletions Sources/TestingMacros/Support/DiagnosticMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,22 @@ 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:
/// - displayNameExpr: The display name string literal expression.
///
/// - Returns: A diagnostic message.
static func emptyDisplayName(_ displayNameExpr: StringLiteralExprSyntax) -> Self {
Self(
syntax: Syntax(displayNameExpr),
message: "Display name string should not be empty",
severity: .warning
)
}

/// 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() {}"#:
"Display name string should not be empty",
##"@Test(#""#) func f() {}"##:
"Display name string should not be empty",
#"@Suite("") struct S {}"#:
"Display name string should not be empty",
]
)
func apiMisuseWarnings(input: String, expectedMessage: String) throws {
Expand Down