Skip to content

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

@chrchr-github

Description

@chrchr-github
#include <string>
#include <iostream>

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));
        }
    }
}
<source>:18:14: warning: 's' used after it was moved [bugprone-use-after-move]
   18 |         if (!s.empty()) {
      |              ^
<source>:19:15: note: move occurred here
   19 |             h(std::move(s));
      |               ^
<source>: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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions