MCPcopy Create free account
hub / github.com/4paradigm/OpenMLDB / CheckCasts

Function CheckCasts

steps/cpplint.py:5706–5822  ·  view source on GitHub ↗

Various cast related checks. 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.

(filename, clean_lines, linenum, error)

Source from the content-addressed store, hash-verified

5704
5705
5706def CheckCasts(filename, clean_lines, linenum, error):
5707 """Various cast related checks.
5708
5709 Args:
5710 filename: The name of the current file.
5711 clean_lines: A CleansedLines instance containing the file.
5712 linenum: The number of the line to check.
5713 error: The function to call with any errors found.
5714 """
5715 line = clean_lines.elided[linenum]
5716
5717 # Check to see if they're using an conversion function cast.
5718 # I just try to capture the most common basic types, though there are more.
5719 # Parameterless conversion functions, such as bool(), are allowed as they are
5720 # probably a member operator declaration or default constructor.
5721 match = Search(
5722 r'(\bnew\s+(?:const\s+)?|\S<\s*(?:const\s+)?)?\b'
5723 r'(int|float|double|bool|char|int32|uint32|int64|uint64)'
5724 r'(\([^)].*)', line)
5725 expecting_function = ExpectingFunctionArgs(clean_lines, linenum)
5726 if match and not expecting_function:
5727 matched_type = match.group(2)
5728
5729 # matched_new_or_template is used to silence two false positives:
5730 # - New operators
5731 # - Template arguments with function types
5732 #
5733 # For template arguments, we match on types immediately following
5734 # an opening bracket without any spaces. This is a fast way to
5735 # silence the common case where the function type is the first
5736 # template argument. False negative with less-than comparison is
5737 # avoided because those operators are usually followed by a space.
5738 #
5739 # function<double(double)> // bracket + no space = false positive
5740 # value < double(42) // bracket + space = true positive
5741 matched_new_or_template = match.group(1)
5742
5743 # Avoid arrays by looking for brackets that come after the closing
5744 # parenthesis.
5745 if Match(r'\([^()]+\)\s*\[', match.group(3)):
5746 return
5747
5748 # Other things to ignore:
5749 # - Function pointers
5750 # - Casts to pointer types
5751 # - Placement new
5752 # - Alias declarations
5753 matched_funcptr = match.group(3)
5754 if (matched_new_or_template is None and
5755 not (matched_funcptr and
5756 (Match(r'\((?:[^() ]+::\s*\*\s*)?[^() ]+\)\s*\(',
5757 matched_funcptr) or
5758 matched_funcptr.startswith('(*)'))) and
5759 not Match(r'\s*using\s+\S+\s*=\s*' + matched_type, line) and
5760 not Search(r'new\(\S+\)\s*' + matched_type, line)):
5761 error(filename, linenum, 'readability/casting', 4,
5762 'Using deprecated casting style. '
5763 'Use static_cast<%s>(...) instead' %

Callers 1

CheckLanguageFunction · 0.85

Calls 6

SearchFunction · 0.85
ExpectingFunctionArgsFunction · 0.85
MatchFunction · 0.85
CheckCStyleCastFunction · 0.85
CloseExpressionFunction · 0.85
NumLinesMethod · 0.80

Tested by

no test coverage detected