Issue 75891
Summary `cppcoreguidelines-missing-std-forward` should consider expressions in lambda captures.
Labels new issue
Assignees
Reporter DXPower
    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

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to