[swift-evolution] Warning on the '(T...) -> U' to '(T...) -> ()' conversion

Jordan Rose jordan_rose at apple.com
Mon Feb 8 21:36:04 CST 2016


We added the conversion to Void specifically because a lot of things return values that the caller doesn't care about. The most common case was

dispatch_sync(queue) { [weak sefl]
  self?.doTheThing()
}

I think it only makes sense to warn on conversion to Void if the expression being converted is itself one we would have warned on with @warn_unused_result, and in fact we do this already even in Swift 2.1.

func test() -> Bool { return false }
@warn_unused_result
func test2() -> Bool { return true }

func use(_: () -> Void) {}

use { test() }
use { test2() }

So the real problem is that != isn't marked @warn_unused_result, which is SR-245 <https://bugs.swift.org/browse/SR-245>.

(Although I agree that if there's an explicit return and the expression doesn't have Void type, we should warn on the Void conversion as well. Or even error.)

Jordan


> On Feb 8, 2016, at 19:25 , Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Javier Soto brought up a case where his ReactiveCocoa code silently did the wrong thing by implicitly discarding the result of a closure:
> 
> https://twitter.com/Javi/status/695680700033306624
> 
> Since it sounds like we plan on making `@warn_unused_result` the default, it seems natural to also warn when a closure has its result type converted away to Void. If there's one 'return' statement, we could dive into the return expression to decide whether to warn based on the function whose result is getting ignored.
> 
> -Joe
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160208/49a2a124/attachment.html>


More information about the swift-evolution mailing list