MCPcopy Create free account
hub / github.com/alibaba/GraphScope / CheckCasts

Function CheckCasts

analytical_engine/misc/cpplint.py:5696–5812  ·  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

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

Callers 1

CheckLanguageFunction · 0.85

Calls 7

SearchFunction · 0.85
ExpectingFunctionArgsFunction · 0.85
CheckCStyleCastFunction · 0.85
CloseExpressionFunction · 0.85
NumLinesMethod · 0.80
MatchFunction · 0.70
groupMethod · 0.45

Tested by

no test coverage detected