Fixed #8495 (False positive: boolean result used in bitwise operation when & is overloaded)

This commit is contained in:
Daniel Marjamäki 2018-04-17 22:14:17 +02:00
parent 219176c11c
commit 1b4f4d7130
2 changed files with 11 additions and 2 deletions

View File

@ -1150,7 +1150,7 @@ void CheckCondition::clarifyCondition()
const ValueType* vt2 = tok->astOperand2() ? tok->astOperand2()->valueType() : nullptr; 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) if (vt1 && vt1->type == ValueType::BOOL && !Token::Match(tok->astOperand1(), "%name%|(|[|::|.") && countPar(tok->astOperand1(), tok) == 0)
clarifyConditionError(tok, false, true); 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); clarifyConditionError(tok, false, true);
} }
} }

View File

@ -2088,13 +2088,16 @@ private:
" if (x & 3 == 2) {}\n" " if (x & 3 == 2) {}\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Suspicious condition (bitwise operator + comparison); Clarify expression with parentheses.\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 'x&3==2' is always false\n"
"[test.cpp:2]: (style) Condition '3==2' is always false\n", errout.str()); "[test.cpp:2]: (style) Condition '3==2' is always false\n", errout.str());
check("void f() {\n" check("void f() {\n"
" if (a & fred1.x == fred2.y) {}\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 // 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 " if (result != (char *)&inline_result) { }\n" // don't simplify and verify cast
"}"); "}");
ASSERT_EQUALS("", errout.str()); 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 void clarifyCondition4() { // ticket #3110