Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions Sources/TestingMacros/ConditionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,24 @@ public struct NonOptionalRequireMacro: RefinedConditionMacro {
in context: some MacroExpansionContext
) throws -> ExprSyntax {
if let argument = macro.arguments.first {
#if !SWT_FIXED_137943258
// Silence this warning if we see a token (`?`, `nil`, or "Optional") that
// might indicate the test author expects the expression is optional.
let tokenKindsIndicatingOptionality: [TokenKind] = [
.infixQuestionMark,
.postfixQuestionMark,
.keyword(.nil),
.identifier("Optional")
]
let looksOptional = argument.tokens(viewMode: .sourceAccurate).lazy
.map(\.tokenKind)
.contains(where: tokenKindsIndicatingOptionality.contains)
if !looksOptional {
context.diagnose(.nonOptionalRequireIsRedundant(argument.expression, in: macro))
}
#else
context.diagnose(.nonOptionalRequireIsRedundant(argument.expression, in: macro))
#endif
}

// Perform the normal macro expansion for #require().
Expand Down
16 changes: 16 additions & 0 deletions Tests/TestingMacrosTests/ConditionMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,22 @@ struct ConditionMacroTests {
#expect(diagnostic.message.contains("is redundant"))
}

#if !SWT_FIXED_137943258
@Test(
"#require(optional value mistyped as non-optional) diagnostic is suppressed",
.bug("https://github.com/swiftlang/swift/issues/79202"),
arguments: [
"#requireNonOptional(expression as? T)",
"#requireNonOptional(expression as Optional<T>)",
"#requireNonOptional(expression ?? nil)",
]
)
func requireNonOptionalDiagnosticSuppressed(input: String) throws {
let (_, diagnostics) = try parse(input)
#expect(diagnostics.isEmpty)
}
#endif

@Test("#require(throws: Never.self) produces a diagnostic",
arguments: [
"#requireThrows(throws: Swift.Never.self)",
Expand Down