diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index fc22d8de4..55cfe91bb 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -905,7 +905,10 @@ void CheckCondition::clarifyCondition() } else if (!tok2->isName() && !tok2->isNumber() && tok2->str() != ".") break; } - } else if (tok->tokType() == Token::eBitOp) { + } else if (tok->tokType() == Token::eBitOp && (tok->str() != "&" || tok->astOperand2())) { + if (tok->astOperand2() && tok->astOperand2()->variable() && tok->astOperand2()->variable()->nameToken() == tok->astOperand2()) + continue; + // using boolean result in bitwise operation ! x [&|^] const ValueType* vt1 = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr; const ValueType* vt2 = tok->astOperand2() ? tok->astOperand2()->valueType() : nullptr; diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 628255bcc..dad7a9b05 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -77,6 +77,7 @@ private: TEST_CASE(clarifyCondition4); // ticket #3110 TEST_CASE(clarifyCondition5); // #3609 CWinTraits.. TEST_CASE(clarifyCondition6); // #3818 + TEST_CASE(clarifyCondition7); TEST_CASE(alwaysTrue); @@ -1568,6 +1569,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void clarifyCondition7() { + // Ensure that binary and unary &, and & in declarations are distinguished properly + check("void f(bool error) {\n" + " bool & withoutSideEffects=found.first->second;\n" // Declaring a reference to a boolean; & is no operator at all + " execute(secondExpression, &programMemory, &result, &error);\n" // Unary & + "}"); + ASSERT_EQUALS("", errout.str()); + } + void testBug5895() { check("void png_parse(uint64_t init, int buf_size) {\n" " if (init == 0x89504e470d0a1a0a || init == 0x8a4d4e470d0a1a0a)\n"