Opened 7 years ago

Closed 4 years ago

#9135 closed enhancement (fixed)

"Access of moved variable" not detected in loop

Reported by: kidkat Owned by: pfultz2
Priority: Normal Milestone: 2.9
Component: Improve check Version:
Keywords: c++11 accessMoved valueflow Cc:

Description (last modified by kidkat)

#include <string>

static void func2(std::string&& s)
{
    (void)s;
}

static void func() {
    std::string p;
    while(true) {
        func2(std::move(p));
    }
}

Change History (7)

comment:1 by amai, 6 years ago

Description: modified (diff)

comment:2 by kidkat, 6 years ago

A variation based on the bug fixed in https://github.com/danmar/cppcheck/commit/96704c997143dfd126db9c61d2e9cc129fabe2f1

#include <list>

extern std::list<int> func(std::list<int>&&);

static void f(std::list<int>l)
{
        for(int i = 0; i < 10; ++i) {
                for (auto &j : func(std::move(l))) {}
        }
}
##file /mnt/s/clion/example_lite_2/test.cpp
3: extern std :: list < int > func ( std :: list < int > && ) ;
4:
5: static void f ( std :: list < int > l@var1 )
6: {
7: for (@expr4 int i@var2 = 0 ; i@var2 <@expr5 10 ; ++ i@var2 ) {
8: for (@expr6 auto &@expr7 j@var3 : func (@expr8 std :: move (@expr9 l@var1 ) ) ) { }
9: }
10: }



##Value flow
Line 7
  0 always 0
  10 always 10
Line 8
  ( possible lifetime=l
Last edited 4 years ago by kidkat (previous) (diff)

comment:3 by chrchr, 5 years ago

Keywords: valueflow added

No warning for this either:

void g(std::string&& s);

void f() {
    std::string p;
    for (int i = 0; i < 2; ++i)
        g(std::move(p));
}

comment:4 by kidkat, 4 years ago

Description: modified (diff)

comment:5 by pfultz2, 4 years ago

Milestone: 2.9
Owner: changed from noone to pfultz2
Status: newaccepted

comment:7 by pfultz2, 4 years ago

Resolution: fixed
Status: acceptedclosed
Note: See TracTickets for help on using tickets.