Created redundantInitialization id
This commit is contained in:
parent
324e5e581b
commit
82eec11898
|
@ -433,7 +433,9 @@ void CheckOther::checkRedundantAssignment()
|
||||||
|
|
||||||
// Do not warn about redundant initialization when rhs is trivial
|
// Do not warn about redundant initialization when rhs is trivial
|
||||||
// TODO : do not simplify the variable declarations
|
// TODO : do not simplify the variable declarations
|
||||||
|
bool isInitialization = false;
|
||||||
if (Token::Match(tok->tokAt(-3), "%var% ; %var% =") && tok->previous()->variable() && tok->previous()->variable()->nameToken() == tok->tokAt(-3) && tok->tokAt(-3)->linenr() == tok->previous()->linenr()) {
|
if (Token::Match(tok->tokAt(-3), "%var% ; %var% =") && tok->previous()->variable() && tok->previous()->variable()->nameToken() == tok->tokAt(-3) && tok->tokAt(-3)->linenr() == tok->previous()->linenr()) {
|
||||||
|
isInitialization = true;
|
||||||
bool trivial = true;
|
bool trivial = true;
|
||||||
visitAstNodes(tok->astOperand2(),
|
visitAstNodes(tok->astOperand2(),
|
||||||
[&](const Token *rhs) {
|
[&](const Token *rhs) {
|
||||||
|
@ -507,6 +509,8 @@ void CheckOther::checkRedundantAssignment()
|
||||||
// warn
|
// warn
|
||||||
if (hasCase)
|
if (hasCase)
|
||||||
redundantAssignmentInSwitchError(tok, nextAssign, tok->astOperand1()->expressionString());
|
redundantAssignmentInSwitchError(tok, nextAssign, tok->astOperand1()->expressionString());
|
||||||
|
else if (isInitialization)
|
||||||
|
redundantInitializationError(tok, nextAssign, tok->astOperand1()->expressionString(), inconclusive);
|
||||||
else
|
else
|
||||||
redundantAssignmentError(tok, nextAssign, tok->astOperand1()->expressionString(), inconclusive);
|
redundantAssignmentError(tok, nextAssign, tok->astOperand1()->expressionString(), inconclusive);
|
||||||
}
|
}
|
||||||
|
@ -544,6 +548,15 @@ void CheckOther::redundantAssignmentError(const Token *tok1, const Token* tok2,
|
||||||
"Variable '$symbol' is reassigned a value before the old one has been used.", CWE563, false);
|
"Variable '$symbol' is reassigned a value before the old one has been used.", CWE563, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckOther::redundantInitializationError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive)
|
||||||
|
{
|
||||||
|
const std::list<const Token *> callstack = { tok1, tok2 };
|
||||||
|
reportError(callstack, Severity::style, "redundantInitialization",
|
||||||
|
"$symbol:" + var + "\nRedundant initialization for '$symbol'. The initialized value is never used.",
|
||||||
|
CWE563,
|
||||||
|
inconclusive);
|
||||||
|
}
|
||||||
|
|
||||||
void CheckOther::redundantAssignmentInSwitchError(const Token *tok1, const Token* tok2, const std::string &var)
|
void CheckOther::redundantAssignmentInSwitchError(const Token *tok1, const Token* tok2, const std::string &var)
|
||||||
{
|
{
|
||||||
const std::list<const Token *> callstack = { tok1, tok2 };
|
const std::list<const Token *> callstack = { tok1, tok2 };
|
||||||
|
|
|
@ -230,6 +230,7 @@ private:
|
||||||
void zerodivError(const Token *tok, const ValueFlow::Value *value);
|
void zerodivError(const Token *tok, const ValueFlow::Value *value);
|
||||||
void nanInArithmeticExpressionError(const Token *tok);
|
void nanInArithmeticExpressionError(const Token *tok);
|
||||||
void redundantAssignmentError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive);
|
void redundantAssignmentError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive);
|
||||||
|
void redundantInitializationError(const Token *tok1, const Token* tok2, const std::string& var, bool inconclusive);
|
||||||
void redundantAssignmentInSwitchError(const Token *tok1, const Token *tok2, const std::string &var);
|
void redundantAssignmentInSwitchError(const Token *tok1, const Token *tok2, const std::string &var);
|
||||||
void redundantCopyError(const Token *tok1, const Token* tok2, const std::string& var);
|
void redundantCopyError(const Token *tok1, const Token* tok2, const std::string& var);
|
||||||
void redundantCopyInSwitchError(const Token *tok1, const Token* tok2, const std::string &var);
|
void redundantCopyInSwitchError(const Token *tok1, const Token* tok2, const std::string &var);
|
||||||
|
@ -285,7 +286,6 @@ private:
|
||||||
//performance
|
//performance
|
||||||
c.redundantCopyError(nullptr, "varname");
|
c.redundantCopyError(nullptr, "varname");
|
||||||
c.redundantCopyError(nullptr, nullptr, "var");
|
c.redundantCopyError(nullptr, nullptr, "var");
|
||||||
c.redundantAssignmentError(nullptr, nullptr, "var", false);
|
|
||||||
|
|
||||||
// style/warning
|
// style/warning
|
||||||
c.checkComparisonFunctionIsAlwaysTrueOrFalseError(nullptr, "isless","varName",false);
|
c.checkComparisonFunctionIsAlwaysTrueOrFalseError(nullptr, "isless","varName",false);
|
||||||
|
@ -333,6 +333,8 @@ private:
|
||||||
c.shadowError(nullptr, nullptr, "argument");
|
c.shadowError(nullptr, nullptr, "argument");
|
||||||
c.constArgumentError(nullptr, nullptr, nullptr);
|
c.constArgumentError(nullptr, nullptr, nullptr);
|
||||||
c.comparePointersError(nullptr, nullptr, nullptr);
|
c.comparePointersError(nullptr, nullptr, nullptr);
|
||||||
|
c.redundantAssignmentError(nullptr, nullptr, "var", false);
|
||||||
|
c.redundantInitializationError(nullptr, nullptr, "var", false);
|
||||||
|
|
||||||
const std::vector<const Token *> nullvec;
|
const std::vector<const Token *> nullvec;
|
||||||
c.funcArgOrderDifferent("function", nullptr, nullptr, nullvec, nullvec);
|
c.funcArgOrderDifferent("function", nullptr, nullptr, nullvec, nullvec);
|
||||||
|
|
|
@ -171,6 +171,7 @@ private:
|
||||||
TEST_CASE(redundantVarAssignment_after_switch);
|
TEST_CASE(redundantVarAssignment_after_switch);
|
||||||
TEST_CASE(redundantVarAssignment_pointer);
|
TEST_CASE(redundantVarAssignment_pointer);
|
||||||
TEST_CASE(redundantVarAssignment_pointer_parameter);
|
TEST_CASE(redundantVarAssignment_pointer_parameter);
|
||||||
|
TEST_CASE(redundantInitialization);
|
||||||
TEST_CASE(redundantMemWrite);
|
TEST_CASE(redundantMemWrite);
|
||||||
|
|
||||||
TEST_CASE(varFuncNullUB);
|
TEST_CASE(varFuncNullUB);
|
||||||
|
@ -6548,6 +6549,32 @@ private:
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void redundantInitialization() {
|
||||||
|
check("void f() {\n"
|
||||||
|
" int err = -ENOMEM;\n"
|
||||||
|
" err = dostuff();\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Redundant initialization for 'err'. The initialized value is never used.\n", errout.str());
|
||||||
|
|
||||||
|
check("void f() {\n"
|
||||||
|
" struct S s = {1,2,3};\n"
|
||||||
|
" s = dostuff();\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Redundant initialization for 's'. The initialized value is never used.\n", errout.str());
|
||||||
|
|
||||||
|
check("void f() {\n"
|
||||||
|
" int *p = NULL;\n"
|
||||||
|
" p = dostuff();\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
check("void f() {\n"
|
||||||
|
" struct S s = {0};\n"
|
||||||
|
" s = dostuff();\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
}
|
||||||
|
|
||||||
void redundantMemWrite() {
|
void redundantMemWrite() {
|
||||||
return; // FIXME: temporary hack
|
return; // FIXME: temporary hack
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue