From fb5cc6fdeda5b8178b9cabd8247552d1383bad9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 23 Feb 2015 16:38:55 +0100 Subject: [PATCH] Fixed #5905 (isSameExpression: comparisons 'ab' are same) --- lib/checkother.cpp | 8 +++++++- test/testother.cpp | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 69782741f..e20888128 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -87,8 +87,14 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::setastOperand2(); if (tok2->str() == "." && tok2->astOperand1() && tok2->astOperand1()->str() == "this") tok2 = tok2->astOperand2(); - if (tok1->varId() != tok2->varId() || tok1->str() != tok2->str()) + if (tok1->varId() != tok2->varId() || tok1->str() != tok2->str()) { + if ((Token::Match(tok1,"<|>") && Token::Match(tok2,"<|>")) || + (Token::Match(tok1,"<=|>=") && Token::Match(tok2,"<=|>="))) { + return isSameExpression(tok1->astOperand1(), tok2->astOperand2(), constFunctions) && + isSameExpression(tok1->astOperand2(), tok2->astOperand1(), constFunctions); + } return false; + } if (tok1->str() == "." && tok1->originalName() != tok2->originalName()) return false; if (tok1->isExpandedMacro() || tok2->isExpandedMacro()) diff --git a/test/testother.cpp b/test/testother.cpp index 109d77a32..b596c6b5e 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3997,6 +3997,16 @@ private: "}"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'.\n", errout.str()); + check("void foo(int a, int b) {\n" + " if ((a < b) && (b > a)) { }\n" + "}"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'.\n", errout.str()); + + check("void foo(int a, int b) {\n" + " if ((a <= b) && (b >= a)) { }\n" + "}"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '&&'.\n", errout.str()); + check("void foo() {\n" " if (x!=2 || y!=3 || x!=2) {}\n" "}");