<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/89388>89388</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`scoped_lock` constructor could be `nodiscard` like in `libstdc++`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
enhancement,
libc++
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
steakhal
</td>
</tr>
</table>
<pre>
Consider this [code](https://godbolt.org/z/fror15jfd):
```c++
#include <mutex>
int num;
std::mutex m;
void top() {
if (std::scoped_lock{m}; num > 10) { // no-warning :(
num = 1;
}
if (std::lock_guard{m}; num > 11) { // warn: nodiscard
num = 2;
}
}
```
The constructor of `std::scoped_lock` should have the `[[nodiscard]]` attribute to catch mistakes like the one in the example, creating a temporary `scoped_lock` object and leaving the access of the global variable unguarded.
If we use libstdc++, we get the following warning `ignoring temporary created by a constructor declared with 'nodiscard' attribute [-Wunused-value]`. Maybe we could also have this `nodiscard` attribute too.
Note that for instance `lock_guard` works as expected, and has the ``nodiscard` attribute.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VMGO4jgQ_ZriUmrk2CSBQw4NDNIedk8r7XHk2BXiwbGR7UD3fv3IgSZ0zwyyINiu96pevZSM0RwdUQPlFsr9Qo6p96GJieSpl3bRev3e7LyLRlPA1JuIUG6V1wTlHvi6T-kcQbwCPwA_HL1uvU1LH47AD_8DP3TBh6L80Wngm3yN7YG9QsVuSwHf5nXb5cI4ZUdNCGI3jIneQHy7n03fxiV04wDiHhGTzpjidbqM88HFG43Jn4GvgW8Q6vs-IqLpEPj6ERqVP5P-br06Qb0doN6D2GYWBPENC3aPx1uB6PzLVQZn3BGnqtczcP7c4vZYPFLBjPgn8sz6_TjKoH_HXXzhzsQgXtF5baLKQTfcD1I-kyLOvPPDh-zPmv7bEyrvYgqjSj6g7xAq9lt5Koax96PV2MsLYeopX52Ms51zKvd5VQxlSsG0YyJMHpVMqsfBxCRPFNGa0w3AO0Ljpkd6k8PZEvAdqkAyZY0lJhrOPsjwPqX1ORvf_iCVUDqNluQlB2QgqRTFmCvJ_47Wt9LiRQYjW0s4uklw0stnGf7q8Eo4RkJr2pj0hzP5Lu8fKU1YnbfWXzPPwwQVM0fnw8T9SHXKnzS27yg_qatJWRlI49WkHoHXs268flIMyu3Lf6MbI-mXi7Qj3TRd4t_yvaWckpoaIW30H93Ir2bFZsAvLfCf6v3H581eJux8QONikk5N_XzyZMXw6sMpooxIb2dSiXRWJAvey_hwwJ9YlwvdCL0RG7mgpqgLUYpabOpF3xS6qCtNfF2xuhPdSrOi6IpNLbRcMyrZwjSc8RVbFZtiJSpRLjnValXWre5Up9pNBStGgzR2ae1lyANnYWIcqVlvxHq9sLIlG6ehxjm5Phc3kEvAOfAdcG5N-2gxz4MvNBnopR2PEVbMmpjiDJ1MstT8asDn1t4a0tIvTZi8btyk7SdvVWwxBtt8GaEm9WO7VH4AfsgJ3H9ezsFntwM_TIVG4Iep1p8BAAD__4GXxjA">