Fixed #7543 (False positive boolean result used in bitwise operation)

This commit is contained in:
Daniel Marjamäki 2016-07-29 17:24:22 +02:00
parent a65ae3ce2e
commit 89be630156
2 changed files with 31 additions and 3 deletions

View File

@ -898,9 +898,9 @@ void CheckCondition::clarifyCondition()
// using boolean result in bitwise operation ! x [&|^] // using boolean result in bitwise operation ! x [&|^]
const ValueType* vt1 = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr; const ValueType* vt1 = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr;
const ValueType* vt2 = tok->astOperand2() ? tok->astOperand2()->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); 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); clarifyConditionError(tok, false, true);
} }
} }

View File

@ -78,6 +78,7 @@ private:
TEST_CASE(clarifyCondition5); // #3609 CWinTraits<WS_CHILD|WS_VISIBLE>.. TEST_CASE(clarifyCondition5); // #3609 CWinTraits<WS_CHILD|WS_VISIBLE>..
TEST_CASE(clarifyCondition6); // #3818 TEST_CASE(clarifyCondition6); // #3818
TEST_CASE(clarifyCondition7); TEST_CASE(clarifyCondition7);
TEST_CASE(clarifyCondition8);
TEST_CASE(alwaysTrue); 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()); "[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() { void clarifyCondition3() {
check("void f(int w) {\n" check("void f(int w) {\n"
" if(!w & 0x8000) {}\n" " if(!w & 0x8000) {}\n"
@ -1664,6 +1665,33 @@ private:
ASSERT_EQUALS("", errout.str()); 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() { void testBug5895() {
check("void png_parse(uint64_t init, int buf_size) {\n" check("void png_parse(uint64_t init, int buf_size) {\n"
" if (init == 0x89504e470d0a1a0a || init == 0x8a4d4e470d0a1a0a)\n" " if (init == 0x89504e470d0a1a0a || init == 0x8a4d4e470d0a1a0a)\n"