Fixed #7107 (False positive iterators - using alias for container)
This commit is contained in:
parent
4285e16fe6
commit
252c3a17fa
|
@ -493,6 +493,21 @@ void CheckStl::iterators()
|
||||||
continue;
|
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.
|
// Show error message, mismatching iterator is used.
|
||||||
iteratorsError(tok2, getContainerName(containerToken), getContainerName(tok2));
|
iteratorsError(tok2, getContainerName(containerToken), getContainerName(tok2));
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,6 +64,7 @@ private:
|
||||||
TEST_CASE(iterator19);
|
TEST_CASE(iterator19);
|
||||||
TEST_CASE(iterator20);
|
TEST_CASE(iterator20);
|
||||||
TEST_CASE(iterator21);
|
TEST_CASE(iterator21);
|
||||||
|
TEST_CASE(iterator22);
|
||||||
TEST_CASE(iteratorExpression);
|
TEST_CASE(iteratorExpression);
|
||||||
TEST_CASE(iteratorSameExpression);
|
TEST_CASE(iteratorSameExpression);
|
||||||
|
|
||||||
|
@ -1017,7 +1018,15 @@ private:
|
||||||
" }\n"
|
" }\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:6] -> [test.cpp:5]: (error) Comparison of iterators from containers 'l1' and 'l2'.\n", errout.str());
|
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() {
|
void iteratorExpression() {
|
||||||
|
|
Loading…
Reference in New Issue