Menu

FALSE NEGATIVE: `unreadVariable` misses an initialization in an unevaluated short-circuit RHS

Rodri
5 days ago
4 days ago
  • Rodri

    Rodri - 5 days ago

    Hi, I found a false negative in Cppcheck 2.21.0 when a local variable is used only on the unevaluated right-hand side of a short-circuit expression.

    Affected tool

    Cppcheck 2.21.0

    Affected checker

    unreadVariable

    Minimal reproducer

    int test() {
      int source = 0;
      int divisor = source;
      return true || divisor;
    }
    

    Reproduction command

    cppcheck --version
    cppcheck --enable=all --inconclusive --check-level=exhaustive --std=c++17 --output-format=xmlv2 mutant-cppcheck-unreadvariable-fn-short-circuit-rhs.cpp
    

    Current behavior

    Cppcheck produces no unreadVariable diagnostic for divisor.

    Expected behavior

    The right-hand side of true || divisor is not evaluated, so the value stored in divisor is never read. Cppcheck should report the unused initialization.

     
  • CHR

    CHR - 4 days ago

    We don't warn about the expression return true || divisor;, which is arguably dubious, but may be intentional, similar to if (false) etc. So we won't report the induced unreadVariable either.

     

Log in to post a comment.