From f2fdd967f59fc0c71c0883e384d69a15b476c657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 5 Oct 2013 08:53:37 +0200 Subject: [PATCH] Fixed #5046 (False positive: Boolean value assigned to pointer) --- lib/checkbool.cpp | 11 ++++++++--- test/testbool.cpp | 17 +++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index 91fe13dfa..6edb2cde5 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -332,12 +332,17 @@ void CheckBool::checkAssignBoolToPointer() 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% = %bool% ;")) { - const Variable *var1(tok->next()->variable()); + if (Token::Match(tok, "%var% = %bool% ;")) { + // Todo: properly check if there is a deref + // *x.p = true; // <- don't warn + // x.p = true; // <- warn + if (Token::Match(tok->previous(), "[*.)]")) + continue; // Is variable a pointer? + const Variable *var1(tok->variable()); if (var1 && var1->isPointer()) - assignBoolToPointerError(tok->next()); + assignBoolToPointerError(tok); } } } diff --git a/test/testbool.cpp b/test/testbool.cpp index ce25ee5e5..7c8f187c9 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -99,14 +99,19 @@ private: " bool *p;\n" "};\n" "void f() {\n" - " std::vector sv;\n" - " sv.push_back(S());\n" - " S &s = sv[0];\n" + " S s = {0};\n" " *s.p = true;\n" - " *(s.p) = true;\n" "}\n"); - TODO_ASSERT_EQUALS("","[test.cpp:8]: (error) Boolean value assigned to pointer.\n" - "[test.cpp:9]: (error) Boolean value assigned to pointer.\n", errout.str()); + ASSERT_EQUALS("", errout.str()); + + check("struct S {\n" + " bool *p;\n" + "};\n" + "void f() {\n" + " S s = {0};\n" + " s.p = true;\n" + "}\n"); + TODO_ASSERT_EQUALS("error", "", errout.str()); } void comparisonOfBoolExpressionWithInt1() {