Is it possible to fine-tune warnings as errors in Swift?

In C++ (with GCC or clang), we can use -Werror=warning-name to convert a specific warning to an error. For example, formatting errors often produce security issues, so it is a good idea to turn them into an error using -Werror=format to convert all formatting warnings into errors.

I wonder if there is a similar feature in Swift. I know that we can use -Xfrontend -warn-as-error to convert all warnings into errors, but I want to convert a specific warning into an error.

Specifically, I want to convert the following warning into an error: (Taken from swift/test/Sema/diag_unintended_optional_behavior.swift at swift-5.9.2-RELEASE · apple/swift · GitHub)

var i: Int? = o
print("Always some, Always some, Always some: \(i)")
// expected-warning@-1 {{string interpolation produces a debug description for an optional value; did you mean to make this explicit?}}
// expected-note@-2 {{use 'String(describing:)' to silence this warning}} {{51-51=String(describing: }} {{52-52=)}}
// expected-note@-3 {{provide a default value to avoid this warning}} {{52-52= ?? <#default value#>}}

When I read Swift's compiler source (link: swift/lib/Sema/MiscDiagnostics.cpp at swift-5.9.2-RELEASE · apple/swift · GitHub), it seems that a diagnostic is produced, but it is only a warning. I wonder if there is a way to convert this warning into an error.

3 Likes