Skip to content

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

@jh7370

Description

@jh7370

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 <unordered_map>

struct Foo {};

void func(const std::unordered_map<int, int> &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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions