diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 695a70a7b..9ec764bb0 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -267,6 +267,13 @@ bool isSameExpression(bool cpp, const Token *tok1, const Token *tok2, const std: commuative_equals = commuative_equals && isSameExpression(cpp, tok1->astOperand1(), tok2->astOperand2(), constFunctions); + // in c++, "a"+b might be different to b+"a" + if (cpp && commuative_equals && tok1->str() == "+" && + (tok1->astOperand1()->tokType() == Token::eString || tok1->astOperand2()->tokType() == Token::eString)) { + const Token * const other = tok1->astOperand1()->tokType() != Token::eString ? tok1->astOperand1() : tok1->astOperand2(); + return other && astIsIntegral(other,false); + } + return commuative_equals; } diff --git a/test/testother.cpp b/test/testother.cpp index 194249521..91326de20 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4194,6 +4194,11 @@ private: "}"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (style) Same expression on both sides of '|'.\n", errout.str()); + check("void foo(std::string a, std::string b) {\n" + " return a.find(b+\"&\") || a.find(\"&\"+b);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("void foo() {\n" " if ((b > a) | (a > b)) {}\n" // > is not commutative "}");