Menu

Request a test for bool return compared against an integer

2024-08-29
2024-08-30
  • Brian Friesen

    Brian Friesen - 2024-08-29

    I would like to suggest a new test for CppCheck - to flag if a function that returns a 'bool' is compared against an integer instead of true/false. Consider the following:

    std::string s;
    ...
    if(s.compare("abc") == 0)
        ...
    

    This code works great. I then updated my code to use "contains" instead of compare, but in doing so I forgot to remove the " == 0"

    std::string s;
    ...
    if(s.contains("abc") == 0)
        ...
    

    I have now flipped the logic and broken my code. But a CppCheck test for bool tested against an integer would flag this issue in my code.

     
  • CHR

    CHR - 2024-08-30

    This is caught by clang-tidy's readability-implicit-bool-conversion.

     

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.