https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91777
Bug ID: 91777 Summary: No warning for iterator going out of scope Product: gcc Version: 9.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: Hi-Angel at yandex dot ru Target Milestone: --- The testcase below would've worked if instead of `return {l,…` was used `return {move(l),…`. But it wasn't, and due to lack of borrow-semantics in C++ language such mistakes are easy to make. In these circumstances programmers has to rely on compiler warnings, so it would be amazing if GCC warn about iterators still referencing the out-of-scope container. # Steps to reproduce (in terms of terminal commands) $ cat test.cpp #include <cstdio> #include <list> using namespace std; pair<list<int>, list<int>::const_iterator> foo() { list<int> l = {1,2,3}; return {l, l.begin()}; } int main() { pair p = foo(); printf("%i\n", *p.second); } $ g++ test.cpp -o a -O3 -Wall -Wextra -Wsign-conversion -std=c++17 -fsanitize=address,undefined ## Expected A warning about the second element in `return {l, l.begin()}` become invalid once `l` goes out of scope. ## Actual No output.