MCPcopy Index your code
hub / github.com/nodejs/node / CheckCStyleCast

Function CheckCStyleCast

tools/cpplint.py:6754–6807  ·  view source on GitHub ↗

Checks for a C-style cast by looking for the pattern. Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. cast_type: The string for the C++ cast to recommend. This is either

(filename, clean_lines, linenum, cast_type, pattern, error)

Source from the content-addressed store, hash-verified

6752
6753
6754def CheckCStyleCast(filename, clean_lines, linenum, cast_type, pattern, error):
6755 """Checks for a C-style cast by looking for the pattern.
6756
6757 Args:
6758 filename: The name of the current file.
6759 clean_lines: A CleansedLines instance containing the file.
6760 linenum: The number of the line to check.
6761 cast_type: The string for the C++ cast to recommend. This is either
6762 reinterpret_cast, static_cast, or const_cast, depending.
6763 pattern: The regular expression used to find C-style casts.
6764 error: The function to call with any errors found.
6765
6766 Returns:
6767 True if an error was emitted.
6768 False otherwise.
6769 """
6770 line = clean_lines.elided[linenum]
6771 match = re.search(pattern, line)
6772 if not match:
6773 return False
6774
6775 # Exclude lines with keywords that tend to look like casts
6776 context = line[0 : match.start(1) - 1]
6777 if re.match(r".*\b(?:sizeof|alignof|alignas|[_A-Z][_A-Z0-9]*)\s*$", context):
6778 return False
6779
6780 # Try expanding current context to see if we one level of
6781 # parentheses inside a macro.
6782 if linenum > 0:
6783 for i in range(linenum - 1, max(0, linenum - 5), -1):
6784 context = clean_lines.elided[i] + context
6785 if re.match(r".*\b[_A-Z][_A-Z0-9]*\s*\((?:\([^()]*\)|[^()])*$", context):
6786 return False
6787
6788 # operator++(int) and operator--(int)
6789 if context.endswith((" operator++", " operator--", "::operator++", "::operator--")):
6790 return False
6791
6792 # A single unnamed argument for a function tends to look like old style cast.
6793 # If we see those, don't issue warnings for deprecated casts.
6794 remainder = line[match.end(0) :]
6795 if re.match(r"^\s*(?:;|const\b|throw\b|final\b|override\b|[=>{),]|->)", remainder):
6796 return False
6797
6798 # At this point, all that should be left is actual casts.
6799 error(
6800 filename,
6801 linenum,
6802 "readability/casting",
6803 4,
6804 f"Using C-style cast. Use {cast_type}<{match.group(1)}>(...) instead",
6805 )
6806
6807 return True
6808
6809
6810def ExpectingFunctionArgs(clean_lines, linenum):

Callers 1

CheckCastsFunction · 0.85

Calls 7

matchMethod · 0.65
rangeFunction · 0.50
maxFunction · 0.50
errorFunction · 0.50
searchMethod · 0.45
startMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…