<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 10, 2015, at 2:58 PM, Adrian Kashivskyy via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I think non-void functions should warn about their return value being unused <b class="">by default</b>, thus eliminating the need to attribute&nbsp;them with @warn_unused_result.<br class=""></div></div></blockquote><div><br class=""></div><div>+1, this would be great. &nbsp;It would also be a great starter project for someone who wants to get into hacking on the Swift compiler.</div><div><br class=""></div><div>-Chris</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><b class="">Motivation</b><br class=""><br class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""><b class="">Example code<br class=""></b><br class="">The following should be default behavior:<br class=""><br class=""><blockquote type="cite" class=""></blockquote><blockquote type="cite" class=""></blockquote><blockquote type="cite" class="">func square(x: Int) -&gt; Int { return x * x }<br class="">square(2) //&nbsp;warning: result of call to 'square' unused<br class=""></blockquote><br class="">Currently, unless annotated by @warn_unused_result, the compiler will not trigger any warnings.<div class=""><br class=""></div><div class=""><b class="">Supporting old behavior</b><br class=""><div class=""><br class=""></div><div class="">If, sometimes, it is okay to not use the result, an opposite @suppress_unused_result attribute could be used instead.</div><div class=""><br class=""></div><div class=""></div><blockquote type="cite" class=""><div class="">@suppress_unused_result func square(x: Int) -&gt; Int { return x * x }</div><div class="">square(2) // no warning<br class=""></div></blockquote><div class=""><br class=""><b class="">Impact on existing code</b><br class=""><br class="">The existing bare @warn_unused_result attributes will become redundant and can be easily removed by migrator. However, if an existing attribute uses message or&nbsp;mutable_variant argument, it may be left intact, since it provides non-obvious informational value.</div><div class=""><br class=""></div><div class="">Implementing the above proposal would definitely make the code clearer and intentional, without compromising any major use cases.</div><div class=""><br class=""></div><div class="">I would be happy to hear your thought on this.</div><div class="">&nbsp;<br class=""><br class="">Regards,<br class="">Adrian Kashivskyy<br class=""><br class=""></div></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=RC5Cq0zAxCHc1sM9Uy3-2BojrrUAw-2F96zH69NULNHPvCvW6m8iXEzRknQwb-2FG5yNz3SqH8qGIj7JPnGdjKsNqpMMZJgdhhUVJHak-2FNm5lr7uBzIvleI8pLrm4o-2F7nF-2FA1oPBYeCNOmKkLxJv3psyO4v3rHZppsRg8eVZqJ4trUz9nKyKah5vovOF-2BNXXrRDXyKVVfCPuq-2BEM6tfYjSYWdT8zvknt1xt2vPk7TRUQ-2BAK9c-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>