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

Function CheckOperatorSpacing

analytical_engine/misc/cpplint.py:3799–3911  ·  view source on GitHub ↗

Checks for horizontal spacing around operators. 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

3797
3798
3799def CheckOperatorSpacing(filename, clean_lines, linenum, error):
3800 """Checks for horizontal spacing around operators.
3801
3802 Args:
3803 filename: The name of the current file.
3804 clean_lines: A CleansedLines instance containing the file.
3805 linenum: The number of the line to check.
3806 error: The function to call with any errors found.
3807 """
3808 line = clean_lines.elided[linenum]
3809
3810 # Don't try to do spacing checks for operator methods. Do this by
3811 # replacing the troublesome characters with something else,
3812 # preserving column position for all other characters.
3813 #
3814 # The replacement is done repeatedly to avoid false positives from
3815 # operators that call operators.
3816 while True:
3817 match = Match(r'^(.*\boperator\b)(\S+)(\s*\(.*)$', line)
3818 if match:
3819 line = match.group(1) + ('_' * len(match.group(2))) + match.group(3)
3820 else:
3821 break
3822
3823 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
3824 # Otherwise not. Note we only check for non-spaces on *both* sides;
3825 # sometimes people put non-spaces on one side when aligning ='s among
3826 # many lines (not that this is behavior that I approve of...)
3827 if ((Search(r'[\w.]=', line) or
3828 Search(r'=[\w.]', line))
3829 and not Search(r'\b(if|while|for) ', line)
3830 # Operators taken from [lex.operators] in C++11 standard.
3831 and not Search(r'(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)', line)
3832 and not Search(r'operator=', line)):
3833 error(filename, linenum, 'whitespace/operators', 4,
3834 'Missing spaces around =')
3835
3836 # It's ok not to have spaces around binary operators like + - * /, but if
3837 # there's too little whitespace, we get concerned. It's hard to tell,
3838 # though, so we punt on this one for now. TODO.
3839
3840 # You should always have whitespace around binary operators.
3841 #
3842 # Check <= and >= first to avoid false positives with < and >, then
3843 # check non-include lines for spacing around < and >.
3844 #
3845 # If the operator is followed by a comma, assume it's be used in a
3846 # macro context and don't do any checks. This avoids false
3847 # positives.
3848 #
3849 # Note that && is not included here. This is because there are too
3850 # many false positives due to RValue references.
3851 match = Search(r'[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]', line)
3852 if match:
3853 error(filename, linenum, 'whitespace/operators', 3,
3854 'Missing spaces around %s' % match.group(1))
3855 elif not Match(r'#.*include', line):
3856 # Look for < that is not surrounded by spaces. This is only

Callers 1

CheckStyleFunction · 0.85

Calls 5

SearchFunction · 0.85
CloseExpressionFunction · 0.85
ReverseCloseExpressionFunction · 0.85
MatchFunction · 0.70
groupMethod · 0.45

Tested by

no test coverage detected