<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/115743>115743</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-tidy] False-positive with returning a constant reference parameter.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Jhuighuy
</td>
</tr>
</table>
<pre>
Hi everyone,
Consider the following snippet of code:
```cpp
#include <array>
template<class T>
void f(const T& t) {
const auto get = [&t] -> const T& { return t; };
return std::array{get(), get(), get()};
}
```
When I run the latest released `clang-tidy` (19.1.3) over it with a check `bugprone-return-const-ref-from-parameter`, it produces a false-positive warning:
```
$ clang-tidy --version
Homebrew LLVM version 19.1.3
Optimized build.
$ clang-tidy b.cpp "--checks=bugprone-return-const-ref-from-parameter" -- -std=c++23
1 warning generated.
.../b.cpp:5:45: warning: returning a constant reference parameter may cause use-after-free when the parameter is constructed from a temporary [bugprone-return-const-ref-from-parameter]
5 | const auto get = [&t] -> const T& { return t; };
|
```
Clearly, `t` is not returned from the function `f`, and it not a parameter of a lambda.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVE2vqzYQ_TWTzcgIbMjHgkU-XvRavaqbp3ZtYAC3xka2SZT--sqQ3Eur3uqqKoqcj_FM5pw5Z6T3qjNEJRQnKC4bOYXeuvLHflJdPz02lW0e5VeFdCP3sIaAnyG9QHpczrM1XjXkMPSErdXa3pXp0Bs1jhTQtljbhkAc10mwTZdXPY7PX7hQptZTQwjiLJ2TDxBf1jmBhlHLQCDOtZbe4_e3CzerGmyB72trfMDvwLcYgB8QdqflBiLiEpRTsNhRQBAXjJD5NkBxQQbiC67yYXdCR2FyBgOIE8LuAuJV7RnwoYnAxHHpd3fqKADfAz8AP-MHX1Z14ue_8rEG_GtPBn9AN5mZ24jdB3SkSXpqMLKnpelYUM0DtikC32eHJEtERG5v5F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