I have a C++ project where clang-tidy is suggesting to add [[nodiscard]] everywhere. Is this a good practice ? The understanding I have is that [[nodiscard]] should be used only when ignoring the return value could be fatal for program. I have an object Car and it has a member const unsigned int m_ID. Should the getter unsigned int getID() have [[nodiscard]] ? clang-tidy suggests so.
EDIT:
Of course, I do not want to ignore a getter. BUT
My point is if every function that returns something should have a [[nodiscard]], then the attribute [[nodiscard]] is anyway redundant. Compiler can simply check all functions that return something.
This option is apparently "modernize-use-nodiscard", so you can deactivate that if you prefer.
It should be noted that the rules this option outlines are not the rules the C++ standard committee themselves use for when to apply
[[nodiscard]]. Those rules being:This is why functions like
operator neware[[nodiscard]], while functions likeoptional::valueare not. There is a difference between being your code having a minor mistake and your code being fundamentally broken.[[nodiscard]], as far as the committee is concerned, is for the latter.Note that container
emptymethods are a special case. They seem to fit the "do not use[[nodiscard]]" pattern, but because the name ofemptyis similar to the name forclear, if you don't use the return value ofempty, odds are good that you meant to callclear.Obviously, this cannot be known from just a declaration, so there's no way for Clang-Tidy to implement said rules.