From a20465eaea4997f7e1873d97f7bdeb2127b7cf17 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 2 Feb 2022 22:30:49 +0100 Subject: [PATCH] Fix #10484 FP knownConditionTrueFalse with static variable and direct initialization / partial fix for #10248 (#3728) --- lib/astutils.cpp | 2 +- test/testcondition.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 8df5aadb3..2478f3f57 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -2699,7 +2699,7 @@ static void getLHSVariablesRecursive(std::vector& vars, const T std::vector getLHSVariables(const Token* tok) { std::vector result; - if (!Token::Match(tok, "%assign%")) + if (!Token::Match(tok, "%assign%|(|{")) return result; if (!tok->astOperand1()) return result; diff --git a/test/testcondition.cpp b/test/testcondition.cpp index e7b0a05dc..e6dce2d1d 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -3867,6 +3867,95 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + // #10484 + check("void f() {\n" + " static bool init = true;\n" + " if (init)\n" + " init = false;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" + " static bool init(true);\n" + " if (init)\n" + " init = false;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" + " static bool init{ true };\n" + " if (init)\n" + " init = false;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + // #10248 + check("void f() {\n" + " static int var(1);\n" + " if (var == 1) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" + " static int var{ 1 };\n" + " if (var == 1) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void Fun();\n" + "using Fn = void (*)();\n" + "void f() {\n" + " static Fn logger = nullptr;\n" + " if (logger == nullptr)\n" + " logger = Fun;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void Fun();\n" + "using Fn = void (*)();\n" + "void f() {\n" + " static Fn logger(nullptr);\n" + " if (logger == nullptr)\n" + " logger = Fun;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void Fun();\n" + "using Fn = void (*)();\n" + "void f() {\n" + " static Fn logger{ nullptr };\n" + " if (logger == nullptr)\n" + " logger = Fun;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void Fun();\n" + "typedef void (*Fn)();\n" + "void f() {\n" + " static Fn logger = nullptr;\n" + " if (logger == nullptr)\n" + " logger = Fun;\n" + "}\n"); + TODO_ASSERT_EQUALS("", "[test.cpp:5]: (style) Condition 'logger==nullptr' is always true\n", errout.str()); + + check("void Fun();\n" + "typedef void (*Fn)();\n" + "void f() {\n" + " static Fn logger(nullptr);\n" + " if (logger == nullptr)\n" + " logger = Fun;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void Fun();\n" + "typedef void (*Fn)();\n" + "void f() {\n" + " static Fn logger{ nullptr };\n" + " if (logger == nullptr)\n" + " logger = Fun;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // #9256 check("bool f() {\n" " bool b = false;\n"