Menu

False positives for variableScope in structured bindings

2025-11-26
2025-11-26
  • Nicholas Dedekind

    Use of a structured binding in an if statement initializer is an automatic violation of the variableScope rule. The scope of entry is already at the if statement, so it can't actually be reduced futher (unless it's counting the if condition clause itself). It doesn't make much sense in the usage though.

    #include <map>
    
    int main()
    {
            std::map<int,int> m;
            if (auto&& [entry, inserted] = m.emplace(1,2); inserted)
            {
                    return entry->second;
            }
            return 0;
    }
    

    test.cpp:6:14: style: The scope of the variable 'entry' can be reduced. [variableScope] if (auto&& [entry, inserted] = m.emplace(1,2); inserted)

     

    Last edit: Nicholas Dedekind 2025-11-26
  • CHR

    CHR - 2025-11-26

    No repro with head, probably fixed by https://github.com/danmar/cppcheck/pull/7521

     
    👍
    1

Log in to post a comment.