Consider the following C++ code snippet:
static [[nodiscard]] int GetSomething()
{
return 64;
}
If you invoke this function like this:
int main()
{
GetSomething();
}
the code compiles successfully with MSVC!
Even at warning level 4 (/W4, which I use on almost all my VC++ projects)!
Why am I upset? Because in the above code I do discard the return value of GetSomething(), and even with the [[nodiscard]] attribute applied to the function, and compiling at /W4, the MSVC C++ compiler does not complain. I do want the compiler to complain in this case! 🙂
Dear MSVC C++ compiler, speak up! Say something like: “Hey, you discarded the return value of GetSomething(), which is marked with the [[nodiscard]] attribute. Please fix your code.”
The interesting thing is that, if you change the position of the [[nodiscard]] attribute, putting it before “static”, you get the expected warning:
// Was:
// static [[nodiscard]] int GetSomething()
//
// Now put [[nodiscard]] at the beginning of the line,
// before "static":
//
[[nodiscard]] static int GetSomething()
{
return 64;
}
Now the C++ compiler complains when trying to compile the above code snippet inside main(), with: “warning C4834: discarding return value of function with [[nodiscard]] attribute”. Great!
The problem is that, a wrong behavior is being silently accepted, even at warning level 4! The MSVC compiler gave me a false sense of security!
I compiled the above code in C++20 mode at /W4 with Visual Studio 2022.
Interestingly, both Clang and GCC refuse to compile the code with [[nodiscard]] in the wrong position, while unfortunately MSVC does silently swallow that.
For example, Clang emits the following clear error message:
![Clang correctly emits an error when [[nodiscard]] is written in the wrong position.](https://i0.wp.com/giodicanio.com/wp-content/uploads/2026/10/cpp_nodiscard_clang_error.png?resize=828%2C192&ssl=1)
On Windows, my favorite IDE for C++ (and some C#) programming is Visual Studio, and I use the Microsoft Visual C++ compiler (MSVC) to compile my C++ code. I hope that this silent sneaky compiler bug is fixed ASAP.
EDIT (November 28th, 2026): If you come reading this blog post from the future, this bug was fixed in the current MSVC C++ compiler that ships with Visual Studio 2026, i.e.: Microsoft C++ (MSVC) Build Tools version 14.52. Considering that Visual Studio 2022 is still in LTSC “long-term” support, I would have liked this bug to be fixed in the MSVC that ships with VS 2022, too.
P.S. At least by default, the VS 2022 IDE seems nicer to me than VS 2026; for example: instead of the solid nice recognizable icons representing folders in Solution Explorer in VS 2022, VS 2026 uses ugly outlines of folders, which are harder to recognize. Microsoft: please stop regressing IDE UIs and bring back a VS 2022-style Blue Theme for VS 2026. Again.














