Fixed #7107 (False positive iterators - using alias for container)

This commit is contained in:
Daniel Marjamäki 2019-12-20 12:13:15 +01:00
parent 4285e16fe6
commit 252c3a17fa
2 changed files with 24 additions and 0 deletions

View File

@ -493,6 +493,21 @@ void CheckStl::iterators()
continue;
}
// Not different containers if a reference is used..
if (containerToken && containerToken->variable() && containerToken->variable()->isReference()) {
const Token *nameToken = containerToken->variable()->nameToken();
if (Token::Match(nameToken, "%name% =")) {
const Token *name1 = nameToken->tokAt(2);
const Token *name2 = tok2;
while (Token::Match(name1, "%name%|.|::") && name2 && name1->str() == name2->str()) {
name1 = name1->next();
name2 = name2->next();
}
if (!Token::simpleMatch(name1, ";") || !Token::Match(name2, "[;,()=]"))
continue;
}
}
// Show error message, mismatching iterator is used.
iteratorsError(tok2, getContainerName(containerToken), getContainerName(tok2));
}

View File

@ -64,6 +64,7 @@ private:
TEST_CASE(iterator19);
TEST_CASE(iterator20);
TEST_CASE(iterator21);
TEST_CASE(iterator22);
TEST_CASE(iteratorExpression);
TEST_CASE(iteratorSameExpression);
@ -1017,7 +1018,15 @@ private:
" }\n"
"}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6] -> [test.cpp:5]: (error) Comparison of iterators from containers 'l1' and 'l2'.\n", errout.str());
}
void iterator22() { // #7107
check("void foo() {\n"
" std::list<int> &l = x.l;\n"
" std::list<int>::iterator it = l.find(123);\n"
" x.l.erase(it);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void iteratorExpression() {