-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<mutex>: lock_guard can emit nodiscard warnings for valid code #1742
Comments
If casting to |
|
Compilers happen so support |
As we are anyway wrapping #ifdef _NODISCARD_LOCK_SUPPRESS
#define _NODISCARD_LOCK
#define _NODISCARD_CTOR_LOCK
#else // ^^^ defined(_NODISCARD_LOCK_SUPPRESS) ^^^ / vvv !defined(_NODISCARD_LOCK_SUPPRESS) vvv
#define _NODISCARD_LOCK \
_NODISCARD_MSG( \
"A lock should be saved in a variable to protect the scope. (If the intetion is to protect the rest of the " \
"current statement, using comma operator, please use cast to void to suppress this warning. " \
"Alternatively, define _NODISCARD_LOCK_SUPPRESS.)")
#define _NODISCARD_CTOR_LOCK \
_NODISCARD_CTOR_MSG( \
"A lock should be saved in a variable to protect the scope. (If the intetion is to protect the rest of the " \
"current statement, using comma operator, please use cast to void to suppress this warning)" \
"Alternatively, define _NODISCARD_LOCK_SUPPRESS.)")
#endif // ^^^ !defined(_NODISCARD_LOCK_SUPPRESS) ^^^ |
Reported as DevCom-1368582, caused by #1495 (unrelated to #1514):
This code is valid and reasonable - it's not doing anything terribly weird. There are other ways to write the code (including ways that don't involve assigning in the constructor body - e.g. a helper function could lock the
mutex
and read theint
), but there's nothing inherently wrong with this way.Our usual policy for
[[nodiscard]]
is to strongly prefer avoiding false positives - to the point where if 10% of uses are valid and 90% are bogus, we would prefer to not warn. (unique_ptr::release()
is a good example where we don't warn.)We need to decide what to do in this case. My feeling is that while this use of the comma operator is valid, it doesn't seem common at the 10% level (my general impression is that usage of the comma operator is quite rare, as most programmers are unfamiliar with the rule that temporaries live until the end of the full-expression). I also feel that it is extremely hazardous to mistakenly discard a
lock_guard
, because the code will appear to work most of the time. (Whereas a discardedunique_ptr::release()
is a memory leak, which can be noticed later.) As we don't have a way to tell the compiler to treat LHS comma subexpressions differently from expression-statements, I think we should resolve this as by design.Fortunately, there is a workaround that allows this code to continue to compile without the need for push-disable-pop. Casting the LHS to
(void)
is sufficient to avoid the warning: https://godbolt.org/z/b5f6vzThe text was updated successfully, but these errors were encountered: