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

Dmitri Gribenko gribozavr at gmail.com
Thu Dec 10 18:41:21 CST 2015


On Thu, Dec 10, 2015 at 2:58 PM, Adrian Kashivskyy via swift-evolution <
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>*/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151210/4e5cbf94/attachment.html>


More information about the swift-evolution mailing list