Menu

False positive accessMoved on move capture

2024-07-19
2024-07-19
  • Frank Winklmeier

    Hi,

    the following code results in a false positive (tested with 2.15 dev from 2024-07-17):

    #include <string>
    
    void foo(std::string bar) {
      auto lambda = [ bar {std::move(bar)} ]() {
        std::string bar2 = std::move(bar);
      };
    }
    
    test.cxx:5:34: warning: Access of moved variable 'bar'. [accessMoved]
        std::string bar2 = std::move(bar);
                                     ^
    test.cxx:4:24: note: Calling std::move(bar)
      auto lambda = [ bar {std::move(bar)} ]() {
                           ^
    test.cxx:5:34: note: Access of moved variable 'bar'.
        std::string bar2 = std::move(bar);
                                     ^
    

    Changing the move capture to the syntax

      auto lambda = [ bar = std::move(bar) ]() {
    

    makes the warning go away.

    -Frank

     

    Last edit: Frank Winklmeier 2024-07-19
  • CHR

    CHR - 2024-07-19

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

     

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.