Fix some FPs in mismatchingContainerExpression (#1402)

This commit is contained in:
Paul Fultz II 2018-09-30 07:49:58 -05:00 committed by Daniel Marjamäki
parent 5c0fd0d5b4
commit 4ed22f1ff8
3 changed files with 18 additions and 3 deletions

View File

@ -338,7 +338,7 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
(Token::Match(tok2, "%name% <") && tok2->next()->link())) {
// non-const template function that is not a dynamic_cast => return false
if (Token::simpleMatch(tok1->next()->link(), "> (") &&
if (pure && Token::simpleMatch(tok1->next()->link(), "> (") &&
!(tok1->function() && tok1->function()->isConst()) &&
tok1->str() != "dynamic_cast")
return false;
@ -362,7 +362,10 @@ bool isSameExpression(bool cpp, bool macro, const Token *tok1, const Token *tok2
// bailout when we see ({..})
if (tok1->str() == "{")
return false;
if (tok1->str() == "(" && tok1->previous() && !tok1->previous()->isName()) { // cast => assert that the casts are equal
// cast => assert that the casts are equal
if (tok1->str() == "(" && tok1->previous() &&
!tok1->previous()->isName() &&
!(tok1->previous()->str() == ">" && tok1->previous()->link())) {
const Token *t1 = tok1->next();
const Token *t2 = tok2->next();
while (t1 && t2 &&

View File

@ -471,7 +471,7 @@ static const Token * getIteratorExpression(const Token * tok)
} else if (Token::Match(tok, "begin|cbegin|rbegin|crbegin|end|cend|rend|crend (")) {
if (Token::Match(tok->previous(), ". %name% ( ) !!."))
return tok->previous()->astOperand1();
if (Token::Match(tok, "%name% ( !!)") && !Token::simpleMatch(tok->linkAt(1), ") ."))
if (!Token::simpleMatch(tok->previous(), ".") && Token::Match(tok, "%name% ( !!)") && !Token::simpleMatch(tok->linkAt(1), ") ."))
return tok->next()->astOperand2();
}
return nullptr;

View File

@ -711,6 +711,13 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
check("template<int N>\n"
"std::vector<int>& f();\n"
"void foo() {\n"
" if(f<1>().begin() == f<1>().end()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" if (a.begin().x == b.begin().x) {}\n"
" if (begin(a).x == begin(b).x) {}\n"
@ -723,6 +730,11 @@ private:
" if (*a.begin() == *b.begin()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo() {\n"
" if(f().begin(1) == f().end()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void iteratorSameExpression() {