Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,7 @@ bool isSameExpression(bool macro, const Token *tok1, const Token *tok2, const Se
compare = true;
}
}
if (compare && astIsBoolLike(varTok1, settings) && astIsBoolLike(varTok2, settings))
if (compare && varTok1 != varTok2 && astIsBoolLike(varTok1, settings) && astIsBoolLike(varTok2, settings))
return isSameExpression(macro, varTok1, varTok2, settings, pure, followVar, errors);

}
Expand Down
10 changes: 10 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3054,6 +3054,16 @@ void CheckOtherImpl::checkDuplicateExpression()
checkDuplicate(ast1->astOperand1(), tok->astOperand2(), ast1);
ast1 = ast1->astOperand1();
}
if (tok->str() != "=") {
const Token* par = tok->astParent();
while (par && tok->str() == par->str() && precedes(par->astOperand1(), tok)) { // chain of identical operators with parentheses
checkDuplicate(par->astOperand1(), tok->astOperand1(), par);
checkDuplicate(par->astOperand1(), tok->astOperand2(), par);
checkDuplicate(par->astOperand2(), tok->astOperand1(), par);
checkDuplicate(par->astOperand2(), tok->astOperand2(), par);
par = par->astParent();
}
}
}
}
} else if (tok->astOperand1() && tok->astOperand2() && tok->str() == ":" && tok->astParent() && tok->astParent()->str() == "?") {
Expand Down
30 changes: 30 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class TestOther : public TestFixture {
TEST_CASE(duplicateExpression19);
TEST_CASE(duplicateExpression20);
TEST_CASE(duplicateExpression21);
TEST_CASE(duplicateExpression22);
TEST_CASE(duplicateExpressionLoop);
TEST_CASE(duplicateValueTernary);
TEST_CASE(duplicateValueTernarySizeof); // #13773
Expand Down Expand Up @@ -8358,6 +8359,35 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void duplicateExpression22() {
check("int f() {\n" // #14913
" return 0x1 | (0x2 | 0x4) | 0x1;\n"
"}\n"
"int g() {\n"
" return 0x1 | (0x2 | 0x1);\n"
"}\n"
"int h() {\n"
" return 0x1 | (0x1 | 0x2);\n"
"}\n"
"int i() {\n"
" return 0x2 | (0x4 | 0x1) | 0x1;\n"
"}\n"
"int j() {\n"
" return 0x2 | (0x1 | 0x4) | 0x1;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:30]: (style) Same expression '0x1' found multiple times in chain of '|' operators. [duplicateExpression]\n"
"[test.cpp:5:23]: (style) Same expression '0x1' found multiple times in chain of '|' operators. [duplicateExpression]\n"
"[test.cpp:8:23]: (style) Same expression '0x1' found multiple times in chain of '|' operators. [duplicateExpression]\n"
"[test.cpp:11:23]: (style) Same expression '0x1' found multiple times in chain of '|' operators. [duplicateExpression]\n"
"[test.cpp:14:23]: (style) Same expression '0x1' found multiple times in chain of '|' operators. [duplicateExpression]\n",
errout_str());

check("bool f(const int** a, const int** b) {\n"
" return (a[0] != nullptr) != (b[0] != nullptr);\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void duplicateExpressionLoop() {
check("void f() {\n"
" int a = 1;\n"
Expand Down
Loading