Menu ▾ ▴

FALSE NEGATIVE: `unreadVariable` misses an unused range-for variable

Rodri
2026-09-09
2026-09-10
  • Rodri

    Rodri - 2026-09-09

    Hi, I found a false negative in Cppcheck 2.21.0 and 2.22 dev when a range-based for loop initializes its loop variable but the body never reads it.

    Affected tool

    Cppcheck 2.21.0; also reproduced with Cppcheck 2.22 dev

    Affected checker

    Cppcheck unreadVariable

    Minimal reproducer

    void test(int (&values)[3]) {
      for (int value : values) {
      }
    }
    

    Reproduction command

    cppcheck --version
    cppcheck --enable=style --inconclusive --std=c++17 --checkers-report=checkers.txt --template='{file}:{line}:{column}: [{id}] {message}' cppcheck-unreadvariable-fn-range-for.cpp
    

    Current behavior

    Cppcheck produces no unreadVariable or unusedVariable diagnostic.

    Expected behavior

    Cppcheck should report line 2 because each value assigned to the loop variable is never read.

     
  • CHR

    CHR - 2026-09-10

    This seems like a bad fit for unreadVariable/unusedVariable because there would be no loop without declaring the variable. Currently we don't flag empty loop/if/... scopes.

     

Log in to post a comment.