summaryrefslogtreecommitdiff
path: root/tools/cpplint.py
diff options
context:
space:
mode:
authorDaniel Bevenius <daniel.bevenius@gmail.com>2017-11-28 18:04:49 +0100
committerDaniel Bevenius <daniel.bevenius@gmail.com>2017-12-01 07:45:05 +0100
commit12c8b4d15471cb6211b39c3a2ca5b10fa4b9f12b (patch)
tree6f8f3cab35a507305d7ddc938e3cac84dd7711b1 /tools/cpplint.py
parente32bbbf4d7adfdb22822c39991ea0e972a0a3453 (diff)
downloadnode-new-12c8b4d15471cb6211b39c3a2ca5b10fa4b9f12b.tar.gz
tools: add cpplint rule for NULL usage
This commit is a suggestion for adding a rule for NULL usages in the code base. This will currently report a number of errors which could be ignored using // NOLINT (readability/null_usage) PR-URL: https://github.com/nodejs/node/pull/17373 Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Diffstat (limited to 'tools/cpplint.py')
-rw-r--r--tools/cpplint.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/tools/cpplint.py b/tools/cpplint.py
index ca42ddeb7b..460c1ecbfe 100644
--- a/tools/cpplint.py
+++ b/tools/cpplint.py
@@ -499,7 +499,6 @@ _ALT_TOKEN_REPLACEMENT = {
_ALT_TOKEN_REPLACEMENT_PATTERN = re.compile(
r'[ =()](' + ('|'.join(_ALT_TOKEN_REPLACEMENT.keys())) + r')(?=[ (]|$)')
-
# These constants define types of headers for use with
# _IncludeState.CheckNextIncludeOrder().
_C_SYS_HEADER = 1
@@ -526,6 +525,8 @@ _SEARCH_C_FILE = re.compile(r'\b(?:LINT_C_FILE|'
# Match string that indicates we're working on a Linux Kernel file.
_SEARCH_KERNEL_FILE = re.compile(r'\b(?:LINT_KERNEL_FILE)')
+_NULL_TOKEN_PATTERN = re.compile(r'\bNULL\b')
+
_regexp_compile_cache = {}
# {str, set(int)}: a map from error categories to sets of linenumbers
@@ -4156,6 +4157,27 @@ def CheckAltTokens(filename, clean_lines, linenum, error):
'Use operator %s instead of %s' % (
_ALT_TOKEN_REPLACEMENT[match.group(1)], match.group(1)))
+def CheckNullTokens(filename, clean_lines, linenum, error):
+ """Check NULL usage.
+
+ Args:
+ filename: The name of the current file.
+ clean_lines: A CleansedLines instance containing the file.
+ linenum: The number of the line to check.
+ error: The function to call with any errors found.
+ """
+ line = clean_lines.elided[linenum]
+
+ # Avoid preprocessor lines
+ if Match(r'^\s*#', line):
+ return
+
+ if line.find('/*') >= 0 or line.find('*/') >= 0:
+ return
+
+ for match in _NULL_TOKEN_PATTERN.finditer(line):
+ error(filename, linenum, 'readability/null_usage', 2,
+ 'Use nullptr instead of NULL')
def GetLineWidth(line):
"""Determines the width of the line in column positions.
@@ -4294,6 +4316,7 @@ def CheckStyle(filename, clean_lines, linenum, file_extension, nesting_state,
CheckSpacingForFunctionCall(filename, clean_lines, linenum, error)
CheckCheck(filename, clean_lines, linenum, error)
CheckAltTokens(filename, clean_lines, linenum, error)
+ CheckNullTokens(filename, clean_lines, linenum, error)
classinfo = nesting_state.InnermostClass()
if classinfo:
CheckSectionSpacing(filename, clean_lines, classinfo, linenum, error)