Menu ▾ ▴

FALSE NEGATIVE: `unreadVariable` misses overwritten structured-binding values

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 both values from a structured binding are overwritten before either initial value is read.

    Affected tool

    Cppcheck 2.21.0; also reproduced with Cppcheck 2.22 dev

    Affected checker

    Cppcheck unreadVariable

    Minimal reproducer

    struct Pair {
      int left;
      int right;
    };
    
    Pair make_pair();
    
    int test() {
      auto [left, right] = make_pair();
      left = 1;
      right = 2;
      return left + right;
    }
    

    Reproduction command

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

    Current behavior

    Cppcheck produces no unreadVariable or redundantAssignment diagnostic.

    Expected behavior

    Cppcheck should report line 9 because the initial values assigned to left and right are overwritten before being read.

     
  • CHR

    CHR - 2026-09-10

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

     

Log in to post a comment.