Skip to content

clang-analyzer-alpha.cplusplus.MismatchedIterator false positive #132649

Open
@firewave

Description

@firewave
#include <list>

class C
{
public:
    C(const std::list<int> &files)
        : mFiles(files)
    {
        mItNextFile = mFiles.begin();
    }

    bool func() {
        if (mItNextFile != mFiles.end()) {
            ++mItNextFile;
            return true;
        }

        return false;
    }

private:
    const std::list<int> &mFiles;
    std::list<int>::const_iterator mItNextFile;
};

static void f(C *c)
{
    while (c->func()) {}
}
<source>:13:13: warning: Iterators of different containers used where the same container is expected [clang-analyzer-alpha.cplusplus.MismatchedIterator]
   13 |         if (mItNextFile != mFiles.end()) {
      |             ^
<source>:28:5: note: Loop condition is true.  Entering loop body
   28 |     while (c->func()) {}
      |     ^
<source>:28:12: note: Calling 'C::func'
   28 |     while (c->func()) {}
      |            ^~~~~~~~~
<source>:13:13: note: Iterators of different containers used where the same container is expected
   13 |         if (mItNextFile != mFiles.end()) {
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~

https://godbolt.org/z/PjfExYP4r

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