<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/66152>66152</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
-Wthread-safety-reference doesn't warn when a reference to a guarded member is returned
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
pirama-arumuga-nainar
</td>
</tr>
</table>
<pre>
`-Wthread-safety-reference` warns when a guarded member is returned by reference.
```
$ cat thread-safety.cpp
#include <mutex>
#include <string>
extern void bar(std::string &s);
class C {
private:
std::mutex lock;
std::string s __attribute__((guarded_by(lock)));
public:
std::string &foo() {
bar(s); // warning emitted
return s; // no warning
}
};
$ clang++ -Wthread-safety -c thread-safety.cpp -stdlib=libc++ -D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS
thread-safety.cpp:13:11: warning: passing variable 's' by reference requires holding mutex 'lock' [-Wthread-safety-reference]
bar(s);
^
1 warning generated.
```
While returning by reference is potentially a bug, it is not invalid. Adding a `REQUIRES` annotation to this function is valid.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VFGP4jYQ_jXDywiUTCAhD3kIC6grnfauu1ud-hQ58RDcGpvaDnv8-yohC8t1rygyime-L5_9zYzwXrWGuYDFChbriejC3rriqJw4iKlw3aFrxdQIZYSb1FaeC0ij6fewdyzk1Isdh_PU8Y4dm4YhjfBNOOPxbc8GBbadcJIlHvhQs0Pl0XHonGGJ9RmvuBlEa4jKcU2j8bm80hwbEfDuk7PmeMT3eKJMozvJCMnDoQv8A5LNZzEfnDLtLTis_COwM3iySmItHNDSBwlJCUl5yUeg1APlkKw-4hotvMcHhGzcRjw6dRKBe_C4g3glG4Shts3fV6K7-Pgxj1UlQnCq7gJXFdASaDneYlWfgZYDBeXjcy8Kj12tVfO5gttxdtYOxPlH9f1vvIALMQJtgbaDoT2QDyoElmP-xUf0HxKNfc-9cUK2HgVm65_EDsZqYVqgFdAKf6oqnDafeI5TH6RWNSRrrermHbquvjyuHr59qzZP5erLpnr97XlTrquXcrt5_bMqn56-vpavj1-fXsai-Q8xJGWc9EsMSXk9RlLise8Q0-JJOCVqzQiUeaDsrn7R8T-dcuxxb7Xs0y92A2UXuzKExerXbbMYL-negI_ODHe5GCs3vnrSsmEnAsvZ570zrN_3SvNoWA-6U648Hm1gE5TQ-owC664FekAV-pCxAZU5Ca3kDLGUw9kEQho9b37_4_F589K3vDDGBhGUNRgshr3yuOtMM2wojxf4RBaJzJNcTLiI03y-WGQx0WRfiCbPRUbNXGRNupxnxLskijnbURMtOc0nqqCIkiiPiShezmk2z5fxLpG7dJEmTbOLYB7xQSg90_p0mFnXTpT3HRdpGi9ookXN2g8DjsjwGw5BIOrnnSt6zLTuWg_zSCsf_I0lqKC5-KVtKC17A5SFwY_3mXeLB_u_I3DSOV3sQzj6vkGHHmpV2Hf1rLEHoG2vY_ybHp39i5sAtB3Ue6DtcLp_AwAA__-A4sOF">