From 1b4f4d7130708bd628b3680b9c860ee69d914997 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 17 Apr 2018 22:14:17 +0200 Subject: [PATCH] Fixed #8495 (False positive: boolean result used in bitwise operation when & is overloaded) --- lib/checkcondition.cpp | 2 +- test/testcondition.cpp | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 289286e6e..2438a3ac7 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -1150,7 +1150,7 @@ void CheckCondition::clarifyCondition() const ValueType* vt2 = tok->astOperand2() ? tok->astOperand2()->valueType() : nullptr; if (vt1 && vt1->type == ValueType::BOOL && !Token::Match(tok->astOperand1(), "%name%|(|[|::|.") && countPar(tok->astOperand1(), tok) == 0) clarifyConditionError(tok, false, true); - else if (vt2 && vt2->type == ValueType::BOOL && !Token::Match(tok->astOperand1(), "%name%|(|[|::|.") && countPar(tok, tok->astOperand2()) == 0) + else if (vt2 && vt2->type == ValueType::BOOL && !Token::Match(tok->astOperand2(), "%name%|(|[|::|.") && countPar(tok, tok->astOperand2()) == 0) clarifyConditionError(tok, false, true); } } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 9723ac993..90a017f00 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -2088,13 +2088,16 @@ private: " if (x & 3 == 2) {}\n" "}"); ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious condition (bitwise operator + comparison); Clarify expression with parentheses.\n" + "[test.cpp:2]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses.\n" "[test.cpp:2]: (style) Condition 'x&3==2' is always false\n" "[test.cpp:2]: (style) Condition '3==2' is always false\n", errout.str()); check("void f() {\n" " if (a & fred1.x == fred2.y) {}\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious condition (bitwise operator + comparison); Clarify expression with parentheses.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious condition (bitwise operator + comparison); Clarify expression with parentheses.\n" + "[test.cpp:2]: (style) Boolean result is used in bitwise operation. Clarify expression with parentheses.\n" + , errout.str()); } // clarify condition that uses ! operator and then bitwise operator @@ -2140,6 +2143,12 @@ private: " if (result != (char *)&inline_result) { }\n" // don't simplify and verify cast "}"); ASSERT_EQUALS("", errout.str()); + + // #8495 + check("void f(bool a, bool b) {\n" + " C & a & b;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void clarifyCondition4() { // ticket #3110