Treat *some* warnings as errors in Swift?

2019-02-16 12:58发布

问题:

Imagine I mark the following method deprecated in Swift:

@available(*, deprecated=1.0)
func myFunc() { 
    // ...
}

And I treat warnings as errors in Swift by setting OTHER_SWIFT_FLAGS="-warnings-as-errors".

How do I make it show these deprecation notices as warnings, while still treating the rest of the warnings as errors?


It seems like GCC had a pretty good solution to this problem:

-Werror // treat all warnings as errors
-Wno-error=<warning> // don't treat <warning> as error (e.g. -Wno-error=switch)
-Werror=<warning> // treat <warning> as error

So if this was Objective-C, I could simply use -Werror -Wno-error=deprecated-declarations and get exactly what I want.

What is the equivalent for Swift?


I tried adding -Wno-error=deprecated-declarations to the OTHER_SWIFT_FLAGS, but it seems like it's not meant for Swift, so it doesn't work.

回答1:

This isn't possible. As of Swift 4, the Swift compiler doesn't have switches to either enable/disable particular warnings or promote particular warnings to errors.

The Swift core developers expressed their reluctance to add a litany of compiler flags on several occasions on the swift-evolution mailing list. The rationale is that they want to keep a single "dialect" of Swift so that every developer works with the same language features etc.

Whether this should extend to something like particular warning flags is of course debatable, but that's the current official stance. It's definitely possible that these rules will be somewhat loosened in the future, but I wouldn't bet on it.