diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index dec6843c2..4775ccead 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -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()); } } diff --git a/test/testbool.cpp b/test/testbool.cpp index 2e535bd3c..a0307bc47 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -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() {