Modernize: make use of 'nullptr' and added a rule-file for finding non-nullptr (zero) initializations.

This commit is contained in:
orbitcowboy 2017-07-28 15:20:43 +02:00
parent c1eb71e84d
commit 0e575ce12c
7 changed files with 21 additions and 11 deletions

View File

@ -339,8 +339,8 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt()
if (!tok->isComparisonOp())
continue;
const Token* numTok = 0;
const Token* boolExpr = 0;
const Token* numTok = nullptr;
const Token* boolExpr = nullptr;
bool numInRhs;
if (astIsBool(tok->astOperand1())) {
boolExpr = tok->astOperand1();

View File

@ -318,7 +318,7 @@ void CheckClass::copyconstructors()
}
std::set<const Token*> copiedVars;
const Token* copyCtor = 0;
const Token* copyCtor = nullptr;
for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
if (func->type == Function::eCopyConstructor) {
copyCtor = func->tokenDef;

View File

@ -180,7 +180,7 @@ void CheckIO::checkFileUsage()
}
} else if (Token::Match(tok, "%name% (") && tok->previous() && (!tok->previous()->isName() || Token::Match(tok->previous(), "return|throw"))) {
std::string mode;
const Token* fileTok = 0;
const Token* fileTok = nullptr;
Filepointer::Operation operation = Filepointer::NONE;
if ((tok->str() == "fopen" || tok->str() == "freopen" || tok->str() == "tmpfile" ||
@ -508,8 +508,8 @@ void CheckIO::checkWrongPrintfScanfArguments()
for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (!tok->isName()) continue;
const Token* argListTok = 0; // Points to first va_list argument
const Token* formatStringTok = 0; // Points to format string token
const Token* argListTok = nullptr; // Points to first va_list argument
const Token* formatStringTok = nullptr; // Points to format string token
bool scan = false;
bool scanf_s = false;

View File

@ -84,7 +84,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
}
if (Token::Match(&tok, "printf|sprintf|snprintf|fprintf|fnprintf|scanf|sscanf|fscanf|wprintf|swprintf|fwprintf|wscanf|swscanf|fwscanf")) {
const Token* argListTok = 0; // Points to first va_list argument
const Token* argListTok = nullptr; // Points to first va_list argument
std::string formatString;
bool scan = Token::Match(&tok, "scanf|sscanf|fscanf|wscanf|swscanf|fwscanf");

View File

@ -994,8 +994,8 @@ void CheckOther::checkUnreachableCode()
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
const Token* secondBreak = 0;
const Token* labelName = 0;
const Token* secondBreak = nullptr;
const Token* labelName = nullptr;
if (tok->link() && Token::Match(tok, "(|[|<"))
tok = tok->link();
else if (Token::Match(tok, "break|continue ;"))
@ -1177,7 +1177,7 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
const Scope* scope = tok->next()->scope();
bool loopVariable = scope->type == Scope::eFor || scope->type == Scope::eWhile || scope->type == Scope::eDo;
bool noContinue = true;
const Token* forHeadEnd = 0;
const Token* forHeadEnd = nullptr;
const Token* end = tok->link();
if (scope->type == Scope::eUnconditional && (tok->strAt(-1) == ")" || tok->previous()->isName())) // Might be an unknown macro like BOOST_FOREACH
loopVariable = true;

View File

@ -4840,7 +4840,7 @@ bool Tokenizer::simplifyConditions()
bool Tokenizer::simplifyConstTernaryOp()
{
bool ret = false;
const Token *templateParameterEnd = 0; // The end of the current template parameter list, if any
const Token *templateParameterEnd = nullptr; // The end of the current template parameter list, if any
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (tok->str() == "<" && TemplateSimplifier::templateParameters(tok))
templateParameterEnd = tok->findClosingBracket();

10
rules/suggest_nullptr.xml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0"?>
<rule version="1">
<tokenlist>raw</tokenlist> <!-- playground: https://regex101.com/r/vGfGSA/1 -->
<pattern><![CDATA[(\b\w+\b) \* (\b\w+\b) = 0 ;]]></pattern>
<message>
<id>modernizeUseNullPtr</id>
<severity>style</severity>
<summary>Prefer to use a 'nullptr' instead of initialize a pointer with 0.</summary>
</message>
</rule>