[swift-evolution] Warn about unused Optional.some(())

Daniel Duan daniel at duan.org
Mon Jan 30 16:58:28 CST 2017

Hi all,

Right now, expressions that evaluates to Optional<()>, Optional<Optional<()>>… gets special treatment when it’s unused. For example:

func f(s: String) {}
let s: String = “”
s.map(f) // no warning here, even tho the resulting type is `Optional<()>` and unused.

func g() throws {}
try? g() // no warnings here neither.

This is convenient, but encourages composing map/filter/reduce, etc with side-effect-ful functions, which we have found a few cases of in our production code recently. Granted, these cases could’ve been caught with more careful code reviews. But we wouldn’t have missed them if this “feature” didn’t exist.

I think we should remove the special treatment so that code in the example above would generate a warning about `()?` being unused. Users can silence it manually by assigning the result to `_`. 

OTOH, this would undermine the convenience of `try?` when the throwing function don’t return anything.

What do y’all think?

Daniel Duan

More information about the swift-evolution mailing list