<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/75891>75891</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
`cppcoreguidelines-missing-std-forward` should consider expressions in lambda captures.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
DXPower
</td>
</tr>
</table>
<pre>
The following code triggers a false positive in Clang-Tidy 17 and many versions prior:
```cpp
#include <utility>
template<typename F>
auto MakeFunc(F&& func) {
return [func2 = std::forward<F>(func)]() {
func2();
};
}
```
```
[warning: forwarding reference parameter 'func' is never forwarded inside the function body [cppcoreguidelines-missing-std-forward]
```
However, this is a false positive because the `func` variable is captured by an expression in which `std::forward` is indeed called on it. It's pretty easy to test that `std::forward` is being called too:
```cpp
#include<utility>
template<typename F>auto MakeFunc(F&&func){
return[func=std::forward<F>(func)]{
func();
};
}
#include<iostream>
struct foo{
foo(){std::cout<<"default construction\n";}
foo(const foo&){std::cout<<"copy construction\n";}
foo(foo&&){std::cout<<"move construction\n";}
foo&operator=(const foo&){std::cout<<"copy assignment\n";return*this;}
foo&operator=(foo&&){std::cout<<"move assignment\n";return*this;}
void operator()()const&{std::cout<<"lvalue\n";}
void operator()()const&&{std::cout<<"rvalue\n";}
};
int main(){
MakeFunc(foo())();
foo f;
MakeFunc(f)();
}
```
Prints:
```
default construction
move construction
lvalue
default construction
copy construction
lvalue
```
`cppcoreguidelines-missing-std-forward` should consider expressions in lambda captures.
Godbolt's for the above snippets:
https://godbolt.org/z/9sh3szr7c
https://godbolt.org/z/sYxhsss14
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vk-T4rYT_TTi0gVlZGzDgcMM_Pglh1TtYQ_JUZbathJZckltWPbTp2QbhlkWduaQKYqx_rzXr9VPbUQIuraIW5a9smw_Ez01zm_3f35xJ_Sz0qnz9muDUDlj3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