[swift-dev] Categorization of warnings in Swift

Chris Lattner clattner at apple.com
Tue Jan 19 00:34:13 CST 2016


> On Jan 12, 2016, at 1:24 PM, Dmitri Gribenko via swift-dev <swift-dev at swift.org> wrote:
> 
>         "%select{variable|constant}2 %0 inferred to have type %1, "
>         "which may be unexpected"
>     no_throw_in_try
>         "no calls to throwing functions occur within 'try' expression"
>     no_throw_in_do_with_catch
>         "'catch' block is unreachable because no errors are thrown in 'do' block"
> 
> It seems like some of these can be too pedantic in cross-platform code bases because of #if's that are not analyzable and might need more granular suppression mechanisms.   For example:
> 
> func foo() throws {}
> func bar() {}
> 
> func baz() {
>   do {
> #if os(OSX)
>     try foo()
> #else
>     bar()
> #endif
>     // common code.
>   } catch { // on non-OS X, warning: 'catch' block is unreachable because no errors are thrown in 'do' block
>     // ...
>   }
> }
> 
> I definitely remember we had some similar issues in the standard library or tests, but don't remember the specifics.

FWIW, one of the reasons we build an AST for disabled #if regions is so we can be smart about this sort of thing.  The fact that we don’t for this warning is a bug in the warning, not something we should use warning suppression to fix. 

To give you an example of this in action, we currently warn on this:

func f() -> Int {
  var x = 42   // warning: variable 'x' was never mutated; consider changing to 'let' constant
  return x
}

but are smart enough to not warn on this:

func f() -> Int {
  var x = 42
#if false
  x = 12
#endif
  return x
}


We should do the same for catch blocks that have a try in a disabled region.

-Chris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160118/49ed8191/attachment.html>


More information about the swift-dev mailing list