[llvm-dev] Adding [[nodiscard]] to Compiler.h

Justin Bogner via llvm-dev llvm-dev at lists.llvm.org
Sun Sep 4 18:30:11 PDT 2016

Sanjoy Das <sanjoy at playingwithpointers.com> writes:
> Hi Chandler,
> Chandler Carruth wrote:
>> My 2 cents: get rid of LLVM_UNUSED_RESULT, and move to LLVM_NODISCARD.
>> For compilers that support it, it should be a strict superset of
>> features and functionality. The standard feature was
>> written directly based on the clang warn_unused_result stuff.
> Maybe I misunderstood something, but with clang 3.8.1 I cannot write:
> int [[clang::nodiscard]] f();

That's the wrong spot - the attribute would apply to `int` in this
expression. Also there's no [[clang::nodiscard]], it's either
[[nodiscard]] or [[clang::warn_unused_result]].

Thus, you spell it like so:

  [[nodiscard]] int f();

> so I'm not sure if LLVM_NODISCARD provides a superset of the functionality.

As Chandler pointed out, [[nodiscard]] is almost exactly
[[clang::warn_unused_result]], which is itself the same as clang's
implementation of __attribute__((__warn_unused_result__)) modulo C++11
attributes being a bit more strict in terms of placement than gcc

More information about the llvm-dev mailing list