[Bug c/66425] (void) cast doesn't suppress __attribute__((warn_unused_result))
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 --- Comment #33 from costinc at gmail dot com --- There are legitimate reasons to ignore results, even without additional comments. One use case I ran into is: // ok() checks the same condition as the one returned by f(). while (ok()) { switch (...) { case 1: (void)f(1); case 2: (void)f(2); ... } } I don't think it's necessary to comment on every single call to f(). At some point people might start using if (f()){} on all function calls where they don't use the result, because that works and casting to void doesn't anymore because of this issue. The way to prevent that might be to start warning on that too. What if a static analysis tool decides to warn on if (f()){}? How do you please both gcc and the tool? if ((void)f()){}? Thankfully this is a highly unlikely scenario as it doesn't seem like other compilers/tools have their own unique ideas here. It seems the reason to warn even when using (void) is to implement a ticket system. First, use (void). This will grant you a ticket to ignore results. At some point we'll get angry and decide not to let you do this anymore because you've abused it. Then, use if(f()){}. This will grant you a different ticket so you can have a way to ignore results for a while longer. Then use the next thing. At least now there is [[nodiscard]] and casting to void is quite clearly defined in the standard. But thanks to the present issue it's not quite the portable way to ignore results (some libraries will use nodiscard, others will use WUR).
[Bug c/66425] (void) cast doesn't suppress __attribute__((warn_unused_result))
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 costinc at gmail dot com changed: What|Removed |Added CC||costinc at gmail dot com --- Comment #23 from costinc at gmail dot com --- I've just been bitten by this. The problem isn't the attribute, it's the on-by-default warning. Why force people to move from one convention of ignoring expression results to another? I've just wasted one hour of my life. On the opposite side of the scale there are people using prettier ways to ignore expression results. I really hope someone, somewhere, was deterred from ignoring a result using a much uglier method than (void) and, because of it, fixed a real bug (this must be the only scenario that favors disallowing (void), deterrence through repugnance, am I wrong?). Note that casting to void seems to be mentioned in the standard precisely for discarding expression results: "Any expression can be explicitly converted to type “cv void.” The expression value is discarded." At best, this warning should be restricted to gnu99 or similar. And if the attribute and it's associated warning are supposed to apply only to important security related code then where is the warning for non-security related code? The warning name pretty clearly relates to the general problem. Isn't that confusing? My 2c: Clang did the right thing. Please, at least make the warning off-by-default.