Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treat *some* warnings as errors in Swift?

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.

like image 811
Senseful Avatar asked Aug 30 '16 19:08

Senseful


People also ask

How do you handle errors in Swift?

In Swift, there’s a built-in error handling mechanism that makes your life easy as an iOS dev. In Swift, a function can throw errors. This means an error propagates from a function back to its caller and the caller handles it. The basic way to handle errors is with do-try-catch blocks:

What is the difference between warningsaserrors and warningsnotaserrors?

You use WarningsAsErrors to configure a set of warnings as errors. Use WarningsNotAsErrors to configure a set of warnings that should not be errors when you've set all warnings as errors. The DisabledWarnings option lets you suppress the compiler from displaying one or more warnings.

How do I make a list of warnings be treated as errors?

Optionally, if you want only a few specific warnings to be treated as errors, you may specify a comma-separated list of warning numbers to treat as errors. The set of all nullability warnings can be specified with the Nullable shorthand. Use WarningLevel to specify the level of warnings that you want the compiler to display.

What is the use of the warninglevel option?

The WarningLevel option specifies the warning level for the compiler to display. The element value is the warning level you want displayed for the compilation: Lower numbers show only high severity warnings. Higher numbers show more warnings. The value must be zero or a positive integer:


1 Answers

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.

EDIT: As of Swift 4.2, Swift added a #warning syntax.

like image 192
Ole Begemann Avatar answered Sep 20 '22 13:09

Ole Begemann