FwdAnalysis; Code cleanup, isNullOperand
This commit is contained in:
parent
0b4e08cac9
commit
1f27cd56c0
|
@ -1080,25 +1080,18 @@ struct FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const
|
|||
|
||||
if (Token::Match(tok, "return|throw")) {
|
||||
// TODO: Handle these better
|
||||
switch (mWhat) {
|
||||
case What::Reassign:
|
||||
return Result(Result::Type::RETURN);
|
||||
case What::UnusedValue:
|
||||
// Is expr variable used in expression?
|
||||
{
|
||||
bool read = false;
|
||||
visitAstNodes(tok->astOperand1(),
|
||||
[&](const Token *tok2) {
|
||||
if (!local && Token::Match(tok2, "%name% ("))
|
||||
read = true;
|
||||
if (tok2->varId() && exprVarIds.find(tok2->varId()) != exprVarIds.end())
|
||||
read = true;
|
||||
return read ? ChildrenToVisit::done : ChildrenToVisit::op1_and_op2;
|
||||
});
|
||||
// Is expr variable used in expression?
|
||||
bool read = false;
|
||||
visitAstNodes(tok->astOperand1(),
|
||||
[&](const Token *tok2) {
|
||||
if (!local && Token::Match(tok2, "%name% ("))
|
||||
read = true;
|
||||
if (tok2->varId() && exprVarIds.find(tok2->varId()) != exprVarIds.end())
|
||||
read = true;
|
||||
return read ? ChildrenToVisit::done : ChildrenToVisit::op1_and_op2;
|
||||
});
|
||||
|
||||
return Result(read ? Result::Type::READ : Result::Type::RETURN);
|
||||
}
|
||||
}
|
||||
return Result(read ? Result::Type::READ : Result::Type::RETURN);
|
||||
}
|
||||
|
||||
if (tok->str() == "}") {
|
||||
|
@ -1307,3 +1300,12 @@ bool FwdAnalysis::possiblyAliased(const Token *expr, const Token *startToken) co
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FwdAnalysis::isNullOperand(const Token *expr)
|
||||
{
|
||||
if (!expr)
|
||||
return false;
|
||||
if (Token::Match(expr, "( %name% %name%| * )") && Token::Match(expr->astOperand1(), "0|NULL|nullptr"))
|
||||
return true;
|
||||
return Token::Match(expr, "NULL|nullptr");
|
||||
}
|
||||
|
|
|
@ -187,6 +187,8 @@ public:
|
|||
/** Is there some possible alias for given expression */
|
||||
bool possiblyAliased(const Token *expr, const Token *startToken) const;
|
||||
|
||||
static bool isNullOperand(const Token *expr);
|
||||
|
||||
private:
|
||||
/** Result of forward analysis */
|
||||
struct Result {
|
||||
|
|
|
@ -449,7 +449,7 @@ void CheckOther::checkRedundantAssignment()
|
|||
}
|
||||
|
||||
// Do not warn about assignment with 0 / NULL
|
||||
if (Token::Match(tok->astOperand2(), "0|NULL|nullptr"))
|
||||
if (Token::simpleMatch(tok->astOperand2(), "0") || FwdAnalysis::isNullOperand(tok->astOperand2()))
|
||||
continue;
|
||||
|
||||
if (tok->astOperand1()->variable() && tok->astOperand1()->variable()->isReference())
|
||||
|
|
|
@ -1254,7 +1254,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
|
|||
continue;
|
||||
}
|
||||
// Do not warn about assignment with NULL
|
||||
if (tok->astOperand1() && tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->pointer && Token::Match(tok->astOperand2(), "0|NULL|nullptr"))
|
||||
if (FwdAnalysis::isNullOperand(tok->astOperand2()))
|
||||
continue;
|
||||
|
||||
if (tok->astOperand1()->variable() && tok->astOperand1()->variable()->isReference() && tok->astOperand1()->variable()->nameToken() != tok->astOperand1())
|
||||
|
|
|
@ -4019,6 +4019,12 @@ private:
|
|||
" p = NULL;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
functionVariableUsage("void f(Foo *p) {\n"
|
||||
" free(p);\n"
|
||||
" p = (Foo *)NULL;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void localvarUnusedGoto() {
|
||||
|
|
Loading…
Reference in New Issue