Fixed #5041 (Improve check: support 'auto' for 'Iterator used after element has been erased')

This commit is contained in:
Steve Duan 2013-09-28 11:50:45 +02:00 committed by Daniel Marjamäki
parent 4a02ca8202
commit cf0c666d79
2 changed files with 12 additions and 1 deletions

View File

@ -67,7 +67,7 @@ void CheckStl::iterators()
const Variable* var = symbolDatabase->getVariableFromVarId(iteratorId); const Variable* var = symbolDatabase->getVariableFromVarId(iteratorId);
// Check that its an iterator // Check that its an iterator
if (!var || !var->isLocal() || !Token::Match(var->typeEndToken(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator")) if (!var || !var->isLocal() || !Token::Match(var->typeEndToken(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator|auto"))
continue; continue;
if (var->type()) { // If it is defined, ensure that it is defined like an iterator if (var->type()) { // If it is defined, ensure that it is defined like an iterator

View File

@ -125,6 +125,7 @@ private:
TEST_CASE(stabilityOfChecks); // #4684 cppcheck crash in template function call TEST_CASE(stabilityOfChecks); // #4684 cppcheck crash in template function call
TEST_CASE(dereferenceInvalidIterator); TEST_CASE(dereferenceInvalidIterator);
TEST_CASE(dereference_auto);
} }
@ -490,6 +491,16 @@ private:
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6]: (error) Iterator 'iter' used after element has been erased.\n", errout.str()); ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6]: (error) Iterator 'iter' used after element has been erased.\n", errout.str());
} }
void dereference_auto() {
check("void f()\n"
"{\n"
" std::vector<int> ints;\n"
" auto iter = ints.begin() + 2;\n"
" ints.erase(iter);\n"
" std::cout << (*iter) << std::endl;\n"
"}");
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:5]: (error) Iterator 'iter' used after element has been erased.\n", errout.str());
}
void STLSize() { void STLSize() {
check("void foo()\n" check("void foo()\n"