<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Dec 10, 2015 at 2:58 PM, Adrian Kashivskyy via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I think non-void functions should warn about their return value being unused <b>by default</b>, thus eliminating the need to attribute them with @warn_unused_result.<br><br><b>Motivation</b><br><br>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.<div><br></div><div>I also noticed that many of stdlib's functions are marked with this attribute, which supports my point of this being the default behavior.<br><br><b>Example code<br></b><br>The following should be default behavior:<br><br><blockquote type="cite"></blockquote><blockquote type="cite"></blockquote><blockquote type="cite">func square(x: Int) -> Int { return x * x }<br>square(2) // warning: result of call to 'square' unused<br></blockquote><br>Currently, unless annotated by @warn_unused_result, the compiler will not trigger any warnings.</div></div></blockquote><div><br></div><div></div></div><div>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.</div><div><br></div><div>Dmitri</div><div><br></div>-- <br><div class="gmail_signature">main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br>(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>>*/</div>
</div></div>