Menu

False positive [mismatchingContainerExpression]

Andrew
2025-04-14
2025-05-09
  • Andrew

    Andrew - 2025-04-14

    Hi,
    cppcheck reports an mismatchingContainerExpression warning for the following code. Seems like false positive to me. I've tested locally on 2.12.1 and online demo (which is 2.10 - http://cppcheck.net/demo/). Currently I am unable to verify it on newest version. Perhaps you could check on 2.17 and if it is still reproducible then create ticket for it :) Similar issue was already reported here: https://trac.cppcheck.net/ticket/10012#ticket

    #include <vector>
    
    class foo
    {
      int mBegin = 0;
      int mEnd = 0;
    
    public:
      foo() = default;
    
      int begin() const { return mBegin ; }
      int end() const { return mEnd ; }
    };
    
    
    int main()
    {
      foo first;
      foo second;
    
      std::vector<foo> vec1;
      vec1.push_back(first);
    
      std::vector<foo> vec2;
      vec2.push_back(second);
    
      if (vec1.front().begin() == vec2.front().end())
        return 1;
    
      return 0;
    }
    

    Thanks in advance.

     
  • CHR

    CHR - 2025-05-06

    I can't reproduce this with current head.

     
  • Oliver Stöneberg

    The issue was fixed in 2.7 and the fix has been bisected to https://github.com/danmar/cppcheck/commit/57f5b19b343a2921abe80e0060d71a30246b7bde.

    input.cpp:27:19: warning: Iterators to containers from different expressions 'vec1.front()' and 'vec2.front()' are used together. [mismatchingContainerExpression]
    if (vec1.front().begin() == vec2.front().end())
    ^

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.