llvm / llvm/llvm-project

False positve bugprone-use-after-move when object is modified in lambda

Open
#172,018 1 comment 0 reactions 1 assignee Claimed by @zeyi2 View on GitHub
clang-tidy false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

~~~c++
#include
#include

void h(std::string x) { std::cout << x << '\n'; }

int main() {
std::string s;
char c = 'A';
auto g = [&]() {
if (c <= 'Z') {
s = std::string(100, c);
++c;
return true;
}
return false;
};
while (g()) {
if (!s.empty()) {
h(std::move(s));
}
}
}
~~~
~~~
:18:14: warning: 's' used after it was moved [bugprone-use-after-move]
18 | if (!s.empty()) {
| ^
:19:15: note: move occurred here
19 | h(std::move(s));
| ^
:18:14: note: the use happens in a later loop iteration than the move
18 | if (!s.empty()) {
| ^
1 warning generated.
~~~
https://godbolt.org/z/YhYjvKP6z

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.