Skip to content

Commit 59cd187

Browse files
committed
Fixed cppcheck-opensource#5467 (False positive incorrectly claiming use after erase)
1 parent a9e5302 commit 59cd187

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎lib/checkstl.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ void CheckStl::iterators()
7777
if (!var || !var->isLocal() || !Token::Match(var->typeEndToken(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator|auto"))
7878
continue;
7979

80+
if (var->typeEndToken()->str() == "auto" && !Token::Match(var->typeEndToken(), "auto %var% ; %var% = %var% . begin|end ( )"))
81+
continue;
82+
8083
if (var->type()) { // If it is defined, ensure that it is defined like an iterator
8184
// look for operator* and operator++
8285
const Function* end = var->type()->getFunction("operator*");

‎test/teststl.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,13 @@ class TestStl : public TestFixture {
506506
" std::cout << (*iter) << std::endl;\n"
507507
"}");
508508
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:5]: (error) Iterator 'iter' used after element has been erased.\n", errout.str());
509+
510+
check("void f() {\n"
511+
" auto x = *myList.begin();\n"
512+
" myList.erase(x);\n"
513+
" auto b = x.first;\n"
514+
"}");
515+
ASSERT_EQUALS("", errout.str());
509516
}
510517

511518
void STLSize() {

0 commit comments

Comments
 (0)