From e3496080c8e51e241bdb8e5801b7911c0bc15ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 12 Jan 2014 07:40:56 +0100 Subject: [PATCH] Fixed #5334 (False positive: same expression on both sides of '||') --- lib/checkother.cpp | 25 +++++++++++++++++++++++-- test/testother.cpp | 20 +++++++++++++------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f8cc4782d..87b8b0678 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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 diff --git a/test/testother.cpp b/test/testother.cpp index 8b83bf95e..02221a730 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3695,13 +3695,6 @@ private: ); ASSERT_EQUALS("", errout.str()); - // #5334 - check("void f(C *src) {\n" - " if (x(src) || x(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(src) || x(src))\n" + " a++;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void f(A *src) {\n" + " if (dynamic_cast(src) || dynamic_cast(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() {