Skip to content

Commit 0e575ce

Browse files
committed
Modernize: make use of 'nullptr' and added a rule-file for finding non-nullptr (zero) initializations.
1 parent c1eb71e commit 0e575ce

File tree

7 files changed

+21
-11
lines changed

7 files changed

+21
-11
lines changed

lib/checkbool.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,8 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt()
339339
if (!tok->isComparisonOp())
340340
continue;
341341

342-
const Token* numTok = 0;
343-
const Token* boolExpr = 0;
342+
const Token* numTok = nullptr;
343+
const Token* boolExpr = nullptr;
344344
bool numInRhs;
345345
if (astIsBool(tok->astOperand1())) {
346346
boolExpr = tok->astOperand1();

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ void CheckClass::copyconstructors()
318318
}
319319

320320
std::set<const Token*> copiedVars;
321-
const Token* copyCtor = 0;
321+
const Token* copyCtor = nullptr;
322322
for (std::list<Function>::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) {
323323
if (func->type == Function::eCopyConstructor) {
324324
copyCtor = func->tokenDef;

lib/checkio.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void CheckIO::checkFileUsage()
180180
}
181181
} else if (Token::Match(tok, "%name% (") && tok->previous() && (!tok->previous()->isName() || Token::Match(tok->previous(), "return|throw"))) {
182182
std::string mode;
183-
const Token* fileTok = 0;
183+
const Token* fileTok = nullptr;
184184
Filepointer::Operation operation = Filepointer::NONE;
185185

186186
if ((tok->str() == "fopen" || tok->str() == "freopen" || tok->str() == "tmpfile" ||
@@ -508,8 +508,8 @@ void CheckIO::checkWrongPrintfScanfArguments()
508508
for (const Token *tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
509509
if (!tok->isName()) continue;
510510

511-
const Token* argListTok = 0; // Points to first va_list argument
512-
const Token* formatStringTok = 0; // Points to format string token
511+
const Token* argListTok = nullptr; // Points to first va_list argument
512+
const Token* formatStringTok = nullptr; // Points to format string token
513513

514514
bool scan = false;
515515
bool scanf_s = false;

lib/checknullpointer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void CheckNullPointer::parseFunctionCall(const Token &tok, std::list<const Token
8484
}
8585

8686
if (Token::Match(&tok, "printf|sprintf|snprintf|fprintf|fnprintf|scanf|sscanf|fscanf|wprintf|swprintf|fwprintf|wscanf|swscanf|fwscanf")) {
87-
const Token* argListTok = 0; // Points to first va_list argument
87+
const Token* argListTok = nullptr; // Points to first va_list argument
8888
std::string formatString;
8989
bool scan = Token::Match(&tok, "scanf|sscanf|fscanf|wscanf|swscanf|fwscanf");
9090

lib/checkother.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,8 @@ void CheckOther::checkUnreachableCode()
994994
const Scope * scope = symbolDatabase->functionScopes[i];
995995

996996
for (const Token* tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
997-
const Token* secondBreak = 0;
998-
const Token* labelName = 0;
997+
const Token* secondBreak = nullptr;
998+
const Token* labelName = nullptr;
999999
if (tok->link() && Token::Match(tok, "(|[|<"))
10001000
tok = tok->link();
10011001
else if (Token::Match(tok, "break|continue ;"))
@@ -1177,7 +1177,7 @@ bool CheckOther::checkInnerScope(const Token *tok, const Variable* var, bool& us
11771177
const Scope* scope = tok->next()->scope();
11781178
bool loopVariable = scope->type == Scope::eFor || scope->type == Scope::eWhile || scope->type == Scope::eDo;
11791179
bool noContinue = true;
1180-
const Token* forHeadEnd = 0;
1180+
const Token* forHeadEnd = nullptr;
11811181
const Token* end = tok->link();
11821182
if (scope->type == Scope::eUnconditional && (tok->strAt(-1) == ")" || tok->previous()->isName())) // Might be an unknown macro like BOOST_FOREACH
11831183
loopVariable = true;

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4840,7 +4840,7 @@ bool Tokenizer::simplifyConditions()
48404840
bool Tokenizer::simplifyConstTernaryOp()
48414841
{
48424842
bool ret = false;
4843-
const Token *templateParameterEnd = 0; // The end of the current template parameter list, if any
4843+
const Token *templateParameterEnd = nullptr; // The end of the current template parameter list, if any
48444844
for (Token *tok = list.front(); tok; tok = tok->next()) {
48454845
if (tok->str() == "<" && TemplateSimplifier::templateParameters(tok))
48464846
templateParameterEnd = tok->findClosingBracket();

rules/suggest_nullptr.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<rule version="1">
3+
<tokenlist>raw</tokenlist> <!-- playground: https://regex101.com/r/vGfGSA/1 -->
4+
<pattern><![CDATA[(\b\w+\b) \* (\b\w+\b) = 0 ;]]></pattern>
5+
<message>
6+
<id>modernizeUseNullPtr</id>
7+
<severity>style</severity>
8+
<summary>Prefer to use a 'nullptr' instead of initialize a pointer with 0.</summary>
9+
</message>
10+
</rule>

0 commit comments

Comments
 (0)