From cb6f04a16cade3a6cd856699e2a631e301dc42b1 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 17 Sep 2022 18:50:07 +0200 Subject: [PATCH] Fix #11315 FP zerodivcond from enum definition / #11310 FP unassignedVariable with static variable (#4476) * Fix #11315 FP zerodivcond from enum definition * Simplify Boolean expression * Fix #11310 FP unassignedVariable with static variable --- lib/checknullpointer.cpp | 4 +--- lib/checkother.cpp | 2 ++ lib/checkunusedvar.cpp | 6 ++++-- test/testother.cpp | 8 ++++++++ test/testunusedvar.cpp | 7 +++++++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 2a1b21605..dc8878ca6 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -147,9 +147,7 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown) const } static bool isUnevaluated(const Token* tok) { - if (tok && Token::Match(tok->previous(), "sizeof|decltype (")) - return true; - return false; + return tok && Token::Match(tok->previous(), "sizeof|decltype ("); } bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown, const Settings *settings) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 2eb06413b..cc51b00f3 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1990,6 +1990,8 @@ void CheckOther::checkZeroDivision() continue; if (!tok->valueType() || !tok->valueType()->isIntegral()) continue; + if (tok->scope() && tok->scope()->type == Scope::eEnum) // don't warn for compile-time error + continue; // Value flow.. const ValueFlow::Value *value = tok->astOperand2()->getValue(0LL); diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 7b6db527d..6c8562d6f 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -1337,9 +1337,11 @@ void CheckUnusedVar::checkFunctionVariableUsage() } } // variable has not been written but has been modified - else if (usage._modified && !usage._write && !usage._allocateMemory && var && !var->isStlType()) + else if (usage._modified && !usage._write && !usage._allocateMemory && var && !var->isStlType()) { + if (var->isStatic()) // static variables are initialized by default + continue; unassignedVariableError(usage._var->nameToken(), varname); - + } // variable has been read but not written else if (!usage._write && !usage._allocateMemory && var && !var->isStlType() && !isEmptyType(var->type())) unassignedVariableError(usage._var->nameToken(), varname); diff --git a/test/testother.cpp b/test/testother.cpp index 844c6117c..d536bbbb0 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -794,6 +794,14 @@ private: " return remainder;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // #11315 + checkP("#define STATIC_ASSERT(c) \\\n" + "do { enum { sa = 1/(int)(!!(c)) }; } while (0)\n" + "void f() {\n" + " STATIC_ASSERT(sizeof(int) == sizeof(FOO));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void nanInArithmeticExpression() { diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index ac9acbbc3..cdd247163 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -5473,6 +5473,13 @@ private: " array[value] = 1;\n" "}"); ASSERT_EQUALS("", errout.str()); + + functionVariableUsage("int fun() {\n" // #11310 + " static int k;\n" + " k++;\n" + " return k;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void localvarextern() {