Fix #10484 FP knownConditionTrueFalse with static variable and direct initialization / partial fix for #10248 (#3728)

This commit is contained in:
chrchr-github 2022-02-02 22:30:49 +01:00 committed by GitHub
parent 4d9a1427b2
commit a20465eaea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 1 deletions

View File

@ -2699,7 +2699,7 @@ static void getLHSVariablesRecursive(std::vector<const Variable*>& vars, const T
std::vector<const Variable*> getLHSVariables(const Token* tok)
{
std::vector<const Variable*> result;
if (!Token::Match(tok, "%assign%"))
if (!Token::Match(tok, "%assign%|(|{"))
return result;
if (!tok->astOperand1())
return result;

View File

@ -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"