From 0e575ce12c43a7f2650cf56a74cb1ccbd3e89a01 Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Fri, 28 Jul 2017 15:20:43 +0200 Subject: [PATCH] Modernize: make use of 'nullptr' and added a rule-file for finding non-nullptr (zero) initializations. --- lib/checkbool.cpp | 4 ++-- lib/checkclass.cpp | 2 +- lib/checkio.cpp | 6 +++--- lib/checknullpointer.cpp | 2 +- lib/checkother.cpp | 6 +++--- lib/tokenize.cpp | 2 +- rules/suggest_nullptr.xml | 10 ++++++++++ 7 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 rules/suggest_nullptr.xml diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index b6dd9622d..d80d3909e 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -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(); diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index be7cf12fe..425dd570e 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -318,7 +318,7 @@ void CheckClass::copyconstructors() } std::set copiedVars; - const Token* copyCtor = 0; + const Token* copyCtor = nullptr; for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (func->type == Function::eCopyConstructor) { copyCtor = func->tokenDef; diff --git a/lib/checkio.cpp b/lib/checkio.cpp index f8197142d..15cc94dd0 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -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; diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 46f7389ee..8dc95e687 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -84,7 +84,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::listfunctionScopes[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; diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 3b3afdf8e..15f0e0b98 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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(); diff --git a/rules/suggest_nullptr.xml b/rules/suggest_nullptr.xml new file mode 100644 index 000000000..f45e73d8e --- /dev/null +++ b/rules/suggest_nullptr.xml @@ -0,0 +1,10 @@ + + + raw + + + modernizeUseNullPtr + style + Prefer to use a 'nullptr' instead of initialize a pointer with 0. + +