Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
firewave opened this issue Mar 23, 2025 · 1 comment
Open

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

firewave opened this issue Mar 23, 2025 · 1 comment
Labels
clang:static analyzer false-positive Warning fires when it should not

Comments

@firewave
Copy link

#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

@firewave firewave added clang:static analyzer false-positive Warning fires when it should not labels Mar 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 23, 2025

@llvm/issue-subscribers-clang-static-analyzer

Author: Oliver Stöneberg (firewave)

```cpp #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
</details>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

2 participants