Menu

False Positive: accessMoved

2023-11-11
2023-11-11
  • Stefan van Kessel

    #include <string>
    
    void printA(std::string str);
    void printB(std::string str);
    
    int main(int argc, char **argv) {
      std::string str = "test it";
      argc>1?printA(std::move(str)):printB(std::move(str));
    }
    

    In the ternary if only one of the two moves will take place.

    .\cppcheck\bin\cppcheck.exe .\test.cpp --enable=warning
    Checking test.cpp ...
    test.cpp:8:50: warning: Access of moved variable 'str'. [accessMoved]
      argc>1?printA(std::move(str)):printB(std::move(str));
                                                     ^
    test.cpp:8:17: note: Calling std::move(str)
      argc>1?printA(std::move(str)):printB(std::move(str));
                    ^
    test.cpp:8:50: note: Access of moved variable 'str'.
      argc>1?printA(std::move(str)):printB(std::move(str));
    
     

    Last edit: Stefan van Kessel 2023-11-11
  • CHR

    CHR - 2023-11-11
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.