Emit the [[maybe_unused]] attribute on anonymous objects to silence C++ compiler warnings#969
Emit the [[maybe_unused]] attribute on anonymous objects to silence C++ compiler warnings#969bluetarpmedia wants to merge 1 commit intohsutter:mainfrom
[[maybe_unused]] attribute on anonymous objects to silence C++ compiler warnings#969Conversation
…to silence C++ compiler warnings about unused variables or members
The motivation wasn't to fix the warnings in the tests (that's just a byproduct), but rather for cases like this: It's arguable that programmers should always check return values, but in this case the user has deliberately written a placeholder to say that they don't care about the return value. But this results in an unused variable warning, even though the user said they don't want it. Godbolt |
|
Well, you shouldn't make an object out of it, but instead discard it |
Oh, of course, I completely missed that! Hmm, so maybe cppfront could suggest to write |
C++ compilers at high warning levels warn about unused variables or class members.
This PR adds
[[maybe_unused]]to anonymous objects (named_) at function and class scope (excludes namespace scope, since it's not needed there).Using this code as an example:
Before this PR, Clang produces these warnings (with
-Wall). Similar warnings with MSVC and GCC.After this PR, there are no warnings, since the relevant code lowers to:
and