Menu

cppcheck 2.18 knownConditionTrueFalse False Positive

2025-10-10
1 day ago
  • Félix-Antoine Constantin

    The following code will generate a false positive for knownConditionTrueFalse with cppcheck 2.18

    #include <vector>
    
    void foo(const std::vector<int> &bar)
    {
      for(const int &b : bar)
      {
        for(const int &c : bar)
        {
          if (&c == &b)
            continue;
        }
      }
    }
    

    cppcheck --enable=style test.cpp

    test.cpp:9:14: style: The comparison '&c == &b' is always true because '&c' and '&b' represent the same value. [knownConditionTrueFalse]
          if (&c == &b)
                 ^
    test.cpp:7:24: note: 'c' is assigned value 'bar' here.
        for(const int &c : bar)
                           ^
    test.cpp:5:22: note: 'b' is assigned value 'bar' here.
      for(const int &b : bar)
                         ^
    test.cpp:9:14: note: The comparison '&c == &b' is always true because '&c' and '&b' represent the same value.
          if (&c == &b)
    
     
  • CHR

    CHR - 2025-10-12

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14193

     
  • Vit Kucera

    Vit Kucera - 1 day ago

    Might be worth mentioning that if &c == &b is replaced with c < 0 && b > 0, it triggers:

    Logical conjunction always evaluates to false: c < 0 && b > 0. [incorrectLogicOperator]
    
     

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.