Menu

bitwiseOnBoolean with bool shift+bitwise OR

2020-01-19
2020-01-20
  • Nikita Leontiev

    Nikita Leontiev - 2020-01-19

    Since cppcheck 1.90 bitwiseOnBoolean occurs with bool shift+bitwise OR:

    #include <inttypes.h>
    #include <stdio.h>
    
    int main()
    {
        bool f1 = true, f2 = false, f3 = true;
        uint32_t num = (f3 << 3) | (f2 << 2) | (f1 << 1);
        printf("%#x\n", num);
        return 0;
    }
    
    test.cpp:7:30: style:inconclusive: Boolean expression 'f3<<3' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]
        uint32_t num = (f3 << 3) | (f2 << 2) | (f1 << 1);
                                 ^
    test.cpp:7:42: style:inconclusive: Boolean expression 'f1<<1' is used in bitwise operation. Did you mean '||'? [bitwiseOnBoolean]
        uint32_t num = (f3 << 3) | (f2 << 2) | (f1 << 1);
                                             ^
    

    Looks like false positive.

     
  • versat

    versat - 2020-01-20

    Yes that looks wrong.
    The boolean variables are cast to integers and the shifted. So clearly there is no boolean any longer. Cppcheck should know that, no reason to mark it inconclusive I guess.

    Maybe this message is intended, I am not sure.
    I created a ticket for this: https://trac.cppcheck.net/ticket/9586

     

    Last edit: versat 2020-01-20

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.