[PATCH] D17043: Check that the result of a library call w/o side effects is used

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 10 13:30:43 PST 2016


alexfh added a comment.

In http://reviews.llvm.org/D17043#348569, @sidney wrote:

> In http://reviews.llvm.org/D17043#348525, @alexfh wrote:
>
> > I'd like to note that there is a library-side way to mitigate this issue using the `[[clang::warn_unused_result]]` attribute on `vector::empty()` and similar methods:
>
>
> Using an attribute sounds much better than this kind of check and I'd be fine with throwing this patch out completely. IMHO the diagnostic is bad, though. A message like this:
>
> >   q.cc:17:3: warning: ignoring return value of function declared with warn_unused_result attribute [-Wunused-result]
>
> >     v.empty();
>
> >     ^~~~~~~
>
> > 
>
>
> …is way too literal. It could indicate two completely different issues:
>
> 1. `empty()` just tells you whether the container is empty.
> 2. `empty()` has side effects and returns a value which should never be ignored (like recv, send, and scanf).
>
>   Searching for this warning (https://www.google.com/search?q="Wunused-result") turns up tons of confusion.
>
>   I'm not an expert on attributes. Is there a way to attach metadata (like a subtype, or a string description), so that calls without side effects could have a diagnostic more like the one I wrote, and things like `recv(2)` and `scanf(3)` could have rich messages?


The attribute can have arguments (e.g. `[[deprecated("use some other API")]] void f();`), so if there is a good reason, an argument (e.g. a custom message) can be added to the `warn_unused_result` attribute. Richard might have thoughts on the matter.

> I'm going to hold of on addressing any of your other comments until I hear back. Thanks for the review!





http://reviews.llvm.org/D17043





More information about the cfe-commits mailing list