Menu

The same expression on both sides: x - !!x

2023-07-07
2023-07-07
  • Martin Poupě

    Martin Poupě - 2023-07-07

    Hello,
    lets' have following routine:

    unsigned one_lower_if_nonzero(unsigned x)
    {
        return x - !!x;
    }
    

    It should (obviously) return (x - 1) for x positive, zero for x == 0.
    But cppcheck (online demo) complains: [test.cpp:3]: (style) Finding the same expression on both sides of an operator is suspicious and might indicate a cut and paste or logic error. Please examine this code carefully to determine if it is correct.
    Cppcheck 2.11 on my project says: style: Same expression on both sides of '-'. [duplicateExpression]
    I think cppcheck overlooks the double negation "!!".
    Best Regards Martin

     
  • CHR

    CHR - 2023-07-07

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

     

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.