Menu

cppcheck 2.10: false positive [knownConditionTrueFalse]

2023-02-09
2023-02-09
  • Sylvain Joubert

    Sylvain Joubert - 2023-02-09

    The following code:

    #include <string>
    
    void test(std::string const& a, std::string const& b)
    {
    
      if (a.empty() && b.empty())
      {
      }
      else if (a.empty() == false && b.empty() == false) // False positives
      {
      }
    
      if (a.empty() && b.empty())
      {
      }
      else if (!a.empty() && !b.empty()) // This is fine
      {
      }
    }
    

    produces the following warnings:

    Checking test.cpp ...
    test.cpp:8:22: style: Condition 'a.empty()==false' is always true [knownConditionTrueFalse]
      else if (a.empty() == false && b.empty() == false)
                         ^
    test.cpp:5:14: note: Assuming that condition 'a.empty()' is not redundant
      if (a.empty() && b.empty())
                 ^
    test.cpp:8:22: note: Condition 'a.empty()==false' is always true
      else if (a.empty() == false && b.empty() == false)
                         ^
    test.cpp:8:44: style: Condition 'b.empty()==false' is always true [knownConditionTrueFalse]
      else if (a.empty() == false && b.empty() == false)
                                               ^
    test.cpp:5:27: note: Assuming that condition 'b.empty()' is not redundant
      if (a.empty() && b.empty())
                              ^
    test.cpp:8:44: note: Condition 'b.empty()==false' is always true
      else if (a.empty() == false && b.empty() == false)
                                               ^
    

    Note that turning the .empty() == false calls into using the not operator like !a.empty() does not produce the warnings.

     
  • CHR

    CHR - 2023-02-09

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

     

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.