Menu

Access of a moved variable not reported

2022-11-25
2022-12-09
  • Alexander Khramtsovskiy

    Hello.
    The following code does not generate access of a moved variable warning when it should.

    class MyClass {
    public:
        explicit MyClass(std::vector<int> vec)
            : vec1_(std::move(vec))
            , vec2_(vec)
        {}
    
    private:
        std::vector<int> vec1_;
        std::vector<int> vec2_;
    };
    
     
  • CHR

    CHR - 2022-11-25

    Which version do you use? With head, I get

    bar.cpp:5:17: warning: inconclusive: Access of moved variable 'vec'. [accessMoved]
            , vec2_(vec)
                    ^
    bar.cpp:4:17: note: Calling std::move(vec)
            : vec1_(std::move(vec))
                    ^
    bar.cpp:5:17: note: Access of moved variable 'vec'.
            , vec2_(vec)
                    ^
    
     
  • Alexander Khramtsovskiy

    I am using version 2.9 for Windows, both GUI and console versions ignore this problem.

    $ /c/Program\ Files/Cppcheck/cppcheck.exe --std=c++17 --language=c++ --file-filter=*.cpp --xml --output-file="out.xml" --enable=style .
    Checking main.cpp ...
    

    If I add move and access of vec to constructor body, problem gets reported.
    If I add only access of vec, problem is still not reported.

    I guess I'll just wait for a new version then.

     

    Last edit: Alexander Khramtsovskiy 2022-12-09

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.