Fixed #5334 (False positive: same expression on both sides of '||')

This commit is contained in:
Daniel Marjamäki 2014-01-12 07:40:56 +01:00
parent f58e1ab80e
commit e3496080c8
2 changed files with 36 additions and 9 deletions

View File

@ -67,9 +67,30 @@ static bool isSameExpression(const Token *tok1, const Token *tok2, const std::se
else if (tok1->function() && !tok1->function()->isConst)
return false;
}
// templates/casts
if ((Token::Match(tok1, "%var% <") && tok1->next()->link()) ||
(Token::Match(tok2, "%var% <") && tok2->next()->link()))
return false;
(Token::Match(tok2, "%var% <") && tok2->next()->link())) {
// non-const template function that is not a dynamic_cast => return false
if (Token::Match(tok1->next()->link(), "> (") &&
!(tok1->function() && tok1->function()->isConst) &&
tok1->str() != "dynamic_cast")
return false;
// some template/cast stuff.. check that the template arguments are same
const Token *t1 = tok1->next();
const Token *t2 = tok2->next();
const Token *end1 = tok1->next()->link();
const Token *end2 = tok2->next()->link();
while (t1 && t2 && t1 != end1 && t2 != end2) {
if (t1->str() != t2->str())
return false;
t1 = t1->next();
t2 = t2->next();
}
if (t1 != end1 || t2 != end2)
return false;
}
if (Token::Match(tok1, "++|--"))
return false;
if (tok1->str() == "(" && tok1->previous() && !tok1->previous()->isName()) { // cast => assert that the casts are equal

View File

@ -3695,13 +3695,6 @@ private:
);
ASSERT_EQUALS("", errout.str());
// #5334
check("void f(C *src) {\n"
" if (x<A*>(src) || x<B*>(src))\n"
" a++;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(int x) {\n"
" if ((x == 1) && (x == 0x00000001))\n"
" a++;\n"
@ -4833,6 +4826,19 @@ private:
" if (bar(a) && !strcmp(a, b) && bar(a) && !strcmp(a, b)) {}\n"
"}");
ASSERT_EQUALS("", errout.str());
// #5334
check("void f(C *src) {\n"
" if (x<A*>(src) || x<B*>(src))\n"
" a++;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(A *src) {\n"
" if (dynamic_cast<B*>(src) || dynamic_cast<B*>(src)) {}\n"
"}\n", "test.cpp", false, false, false, false); // don't run simplifications
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'.\n", errout.str());
}
void duplicateExpression4() {