llvm / llvm/llvm-project

[analyzer] False positive "moved-from object is moved" inside map

Open
#164,646 4 comments 0 reactions 0 assignees View on GitHub
clang:static analyzer false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

I've got a bit of code which iterates over a map. If the key is a specific value, it will move a variable as part of constructing another object. As this is iterating over a constant map (ignoring for a moment unsafe updates of the map in another thread), this specific case can only be hit once, because each key in a map can only be seen once. However, clang produces a "Moved-from object ... is moved" warning for this line, which is therefore a false positive.

I've tested with a recent main (but not head) as well as 19.0, using clang-cl --analyze.

Reproducible:
```
#include

struct Foo {};

void func(const std::unordered_map &m)
{
Foo f;

for (const auto &[key, value] : m)
{
if (key == 1)
{
Foo f2(std::move(f));
}
}
}
```
Output:
```
C:\telemetry\telemetry-tooling\telemetry-tooling-api>clang-cl.exe --analyze C:\Work\TempWork\StaticAnalysis.cpp
C:\Work\TempWork\StaticAnalysis.cpp(13,17): warning: Moved-from object 'f'
is moved [cplusplus.Move]
13 | Foo f2(std::move(f));
| ^~~~~~~~~~~~~~~~
1 warning generated.
```

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.