From 89be6301560546fc2c58f7459faf77412131f6d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 29 Jul 2016 17:24:22 +0200 Subject: [PATCH] Fixed #7543 (False positive boolean result used in bitwise operation) --- lib/checkcondition.cpp | 4 ++-- test/testcondition.cpp | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index d3a3c9c93..8be58a03e 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -898,9 +898,9 @@ void CheckCondition::clarifyCondition() // 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; - if (vt1 && vt1->type == ValueType::BOOL) + if (vt1 && vt1->type == ValueType::BOOL && !Token::Match(tok->astOperand1(), "(|[|::|.")) clarifyConditionError(tok, false, true); - else if (vt2 && vt2->type == ValueType::BOOL) + else if (vt2 && vt2->type == ValueType::BOOL && !Token::Match(tok->astOperand1(), "(|[|::|.")) clarifyConditionError(tok, false, true); } } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index e3b887a08..8aaeccca4 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -78,6 +78,7 @@ private: TEST_CASE(clarifyCondition5); // #3609 CWinTraits.. TEST_CASE(clarifyCondition6); // #3818 TEST_CASE(clarifyCondition7); + TEST_CASE(clarifyCondition8); TEST_CASE(alwaysTrue); @@ -1596,7 +1597,7 @@ private: "[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 + // clarify condition that uses ! operator and then bitwise operator void clarifyCondition3() { check("void f(int w) {\n" " if(!w & 0x8000) {}\n" @@ -1664,6 +1665,33 @@ private: ASSERT_EQUALS("", errout.str()); } + void clarifyCondition8() { + // don't warn when boolean result comes from function call, array index, etc + // the operator precedence is not unknown then + check("bool a();\n" + "bool f(bool b) {\n" + " return (a() & b);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("bool f(bool *a, bool b) {\n" + " return (a[10] & b);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("struct A { bool a; };\n" + "bool f(struct A a, bool b) {\n" + " return (a.a & b);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("struct A { bool a; };\n" + "bool f(struct A a, bool b) {\n" + " return (A::a & b);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void testBug5895() { check("void png_parse(uint64_t init, int buf_size) {\n" " if (init == 0x89504e470d0a1a0a || init == 0x8a4d4e470d0a1a0a)\n"