Menu

False positive: range-for colon reported as a moved variable

Rodri
4 days ago
4 days ago
  • Rodri

    Rodri - 4 days ago

    Hi, I found a false positive / malformed warning regarding the rule accessMoved.

    In the following program, cppcheck correctly reports that s is accessed after std::move(s). However, it also reports an extra accessMoved warning for the symbol :, which is not a variable.

    #include <string>
    #include <utility>
    
    void foo(std::string);
    
    int main() {
      std::string s = "test";
      foo(std::move(s));
      for (char c : s) {
      }
    }
    

    Actual result

    Cppcheck reports two accessMoved warnings:

    <error id="accessMoved" severity="warning" msg="Access of moved variable ':'." ...>
        <symbol>:</symbol>
    </error>
    
    <error id="accessMoved" severity="warning" msg="Access of moved variable 's'." ...>
        <symbol>s</symbol>
    </error>
    

    The warning for s is expected, but the warning for : is incorrect because : is only the range-for syntax token.

    Expected result

    Cppcheck should only report accessMoved for s, not for :.

    Verification

    The issue was reproduced with:

    cppcheck --output-format=xmlv2 --enable=all accessMoved_rangefor_fp.cpp
    

    Version: 2.21.0

     
  • CHR

    CHR - 4 days ago

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

     

Log in to post a comment.

Monday.com Logo