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

Adrian Kashivskyy adrian.kashivskyy at me.com
Thu Dec 10 18:47:39 CST 2015


I'm sure there are plenty of methods in Foundation that return a value which can be ignored. However, as Joe noticed, such functions are generally in minority.

I'd rather see a couple of `@allow_unused_result` than a dozen of `@warn_unused_result`.

Pozdrawiam – Regards,
Adrian Kashivskyy

> Wiadomość napisana przez Dmitri Gribenko <gribozavr at gmail.com> w dniu 11.12.2015, o godz. 01:41:
> 
> On Thu, Dec 10, 2015 at 2:58 PM, Adrian Kashivskyy via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> I think non-void functions should warn about their return value being unused by default, thus eliminating the need to attribute them with @warn_unused_result.
> 
> Motivation
> 
> It is a rare case for a result of a function to be unused – and most often it's caused by programmer having too little knowledge of the API. This, in my opinion, is a reasonable area of improvement for the compiler.
> 
> I also noticed that many of stdlib's functions are marked with this attribute, which supports my point of this being the default behavior.
> 
> Example code
> 
> The following should be default behavior:
> 
>> func square(x: Int) -> Int { return x * x }
>> square(2) // warning: result of call to 'square' unused
> 
> Currently, unless annotated by @warn_unused_result, the compiler will not trigger any warnings.
> 
> We have discussed this internally in the past, and we found that non-mutating methods on structs and enums are usually @warn_unused_result, it is a quite strong heuristic.  With classes, especially Cocoa ones, it might not be the case.  It would be good if someone investigated how this change would affect a class-heavy library, e.g., Foundation.
> 
> Dmitri
> 
> -- 
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com <mailto:gribozavr at gmail.com>>*/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/5c47b2e9/attachment.html>


More information about the swift-evolution mailing list