Issue 150189
Summary [clang-tidy] Incorrect fix-it may cause runtime issue from performance-unnecessary-copy-initialization
Labels clang-tidy
Assignees
Reporter ChuanqiXu9
    Reproducer: https://godbolt.org/z/11W68o11T

```C++
#include <vector>

struct S {
 S();
    ~S();
    S(const S &);
    S &operator =(const S&);

    int a, b, c, d;
};

bool cond();

void consume(const S&);

void func(std::vector<S> &Vecs) {
    auto TargetIt(Vecs.end());
    for (auto It = Vecs.begin(); It != Vecs.end(); It++)
        if (cond())
 TargetIt = It;

    if (TargetIt != Vecs.end()) {
        const S Value(*TargetIt);
        if (cond()) {
            Vecs.erase(TargetIt);
 consume(Value);
        }
    }
}
```

The clang-tidy's auto-fix will add `&` to `const S Value(*TargetIt);`. This is problematic. Since if we erase the underlying iterator later, the new added reference will be a dangling pointer. This caused a failure in our CI.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to