I would find it useful if there was a macro or some other option to mark types like std::error_code, std::errc, and std::expected as [[nodiscard]] so developers will be forced to handle the errors.
FYI MSVC STL currently applies [[nodiscard]] to std::lock_guard and std::scoped_lock.
Right, libc++ do that as well since 2019 - https://reviews.llvm.org/D65900?id=213975. Although with the RAII classes I think you need to [[nodiscard]] the constructor which I think (?) is a compiler extension. And you're also much less likely to have false positives with that so you can actually do that unconditionally :)
That doesn't currently work with GCC, so there's not much point doing it.
The linked bug is about nodiscard applied to the class together with ignoring temporary objects. But GCC will when the type is the return value or when the nodiscard is applied to the constructor: class [[nodiscard]] err {}; err f(); void g() { f(); // Warning emitted } struct RAII { [[nodiscard]] RAII(int x); }; void h() { RAII(0); // Warning emitted }
https://quuxplusone.github.io/blog/2024/12/08/should-expected-be-nodiscard/ https://github.com/microsoft/STL/pull/5174 As of this past Friday, Microsoft STL's std::expected is finally marked nodiscard. LLVM/Clang's own `llvm::Expected` type has been marked nodiscard since 2016. I encourage libstdc++ to follow suit, for `expected` specifically. (Personally I'm less sure that nodiscard belongs at all on `error_code` or `errc`; Microsoft doesn't mark those yet. And on the exception hierarchy, which Microsoft also marked last Friday, I think it's harmless and in keeping with Microsoft's aggressive marking strategy, but perhaps not super helpful either.)
Also worth mentioning that clang-tidy diagnoses this under bugprone-unused-return-value since May 23.