[swift-evolution] Make non-void functions @warn_unused_result by default

Haravikk swift-evolution at haravikk.me
Wed Feb 24 18:03:56 CST 2016


> On 24 Feb 2016, at 23:45, Janosch Hildebrand via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Ideally every function would be perfectly annotated but ultimately not every API developer will know about, remember or put in the effort to annotate with @warn_unused_result. Making the inverse the default should hopefully be “safer” by warning by default.

I stumbled across @warn_unused_result pretty early on when learning Swift, but I still miss it on around 90% of my method declarations that should have it, particularly if I’m filling something out quickly to flesh it out later. I think in part of the problem is simply that it’s extra detail to add, which is kind of strange in Swift when we have all our time savers like type inference and the simplified forms of closures; it makes more sense to add an attribute in the less common case, which in my experience is having a method result that may not be needed.

I’d liken it to mutable variables that you don’t actually mutate; it’s an error I run into all the time, but I’d rather have false positives then miss cases. It’s also important on structs with both mutating and non-mutating options; while the choice of let or var should keep you right much of the time, sometimes you do want a copy from a mutable value, so a mistakenly typing .reverse() instead of .reversed() for example is a mistake that result usage warnings can help prevent, but as long as developers keep forgetting to add @warn_unused_result it’s a warning that may not come up when it’s really needed =)


More information about the swift-evolution mailing list