Support member variables in CheckBool::checkAssignBoolToFloat()

This commit is contained in:
PKEuS 2016-02-05 19:46:47 +01:00 committed by PKEuS
parent 8d30670bc8
commit 6215a48dc9
2 changed files with 17 additions and 3 deletions

View File

@ -441,9 +441,14 @@ void CheckBool::checkAssignBoolToFloat()
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
if (Token::Match(tok, "%var% =")) {
const Variable * const var = tok->variable();
if (var && var->isFloatingType() && !var->isArrayOrPointer() && astIsBool(tok->next()->astOperand2()))
if (tok->str() == "=" && astIsBool(tok->astOperand2())) {
const Token *lhs = tok->astOperand1();
while (lhs && (lhs->str() == "." || lhs->str() == "::"))
lhs = lhs->astOperand2();
if (!lhs || !lhs->variable())
continue;
const Variable* var = lhs->variable();
if (var && var->isFloatingType() && !var->isArrayOrPointer())
assignBoolToFloatError(tok->next());
}
}

View File

@ -187,6 +187,15 @@ private:
" unknown = false;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("struct S {\n"
" float p;\n"
"};\n"
"void f() {\n"
" S s = {0};\n"
" s.p = true;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (style) Boolean value assigned to floating point variable.\n", errout.str());
}
void comparisonOfBoolExpressionWithInt1() {