Fixed #5905 (isSameExpression: comparisons 'a<b' and 'a>b' are same)

This commit is contained in:
Daniel Marjamäki 2015-02-23 16:38:55 +01:00
parent 830d1eb3fd
commit fb5cc6fded
2 changed files with 17 additions and 1 deletions

View File

@ -87,8 +87,14 @@ bool isSameExpression(const Token *tok1, const Token *tok2, const std::set<std::
tok1 = tok1->astOperand2(); tok1 = tok1->astOperand2();
if (tok2->str() == "." && tok2->astOperand1() && tok2->astOperand1()->str() == "this") if (tok2->str() == "." && tok2->astOperand1() && tok2->astOperand1()->str() == "this")
tok2 = tok2->astOperand2(); 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; return false;
}
if (tok1->str() == "." && tok1->originalName() != tok2->originalName()) if (tok1->str() == "." && tok1->originalName() != tok2->originalName())
return false; return false;
if (tok1->isExpandedMacro() || tok2->isExpandedMacro()) if (tok1->isExpandedMacro() || tok2->isExpandedMacro())

View File

@ -3997,6 +3997,16 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '||'.\n", errout.str()); 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" check("void foo() {\n"
" if (x!=2 || y!=3 || x!=2) {}\n" " if (x!=2 || y!=3 || x!=2) {}\n"
"}"); "}");