Skip to content

Commit a574fda

Browse files
committed
Fixed cppcheck-opensource#6935 (Wrong duplicate expression)
1 parent 27f72d7 commit a574fda

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

lib/astutils.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,13 @@ bool isSameExpression(bool cpp, const Token *tok1, const Token *tok2, const std:
267267
commuative_equals = commuative_equals &&
268268
isSameExpression(cpp, tok1->astOperand1(), tok2->astOperand2(), constFunctions);
269269

270+
// in c++, "a"+b might be different to b+"a"
271+
if (cpp && commuative_equals && tok1->str() == "+" &&
272+
(tok1->astOperand1()->tokType() == Token::eString || tok1->astOperand2()->tokType() == Token::eString)) {
273+
const Token * const other = tok1->astOperand1()->tokType() != Token::eString ? tok1->astOperand1() : tok1->astOperand2();
274+
return other && astIsIntegral(other,false);
275+
}
276+
270277
return commuative_equals;
271278
}
272279

test/testother.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4194,6 +4194,11 @@ class TestOther : public TestFixture {
41944194
"}");
41954195
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '|'.\n", errout.str());
41964196

4197+
check("void foo(std::string a, std::string b) {\n"
4198+
" return a.find(b+\"&\") || a.find(\"&\"+b);\n"
4199+
"}");
4200+
ASSERT_EQUALS("", errout.str());
4201+
41974202
check("void foo() {\n"
41984203
" if ((b > a) | (a > b)) {}\n" // > is not commutative
41994204
"}");

0 commit comments

Comments
 (0)