MCPcopy Index your code
hub / github.com/cpplint/cpplint / CheckOperatorSpacing

Function CheckOperatorSpacing

cpplint.py:4283–4399  ·  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

4281
4282
4283def CheckOperatorSpacing(filename, clean_lines, linenum, error):
4284 """Checks for horizontal spacing around operators.
4285
4286 Args:
4287 filename: The name of the current file.
4288 clean_lines: A CleansedLines instance containing the file.
4289 linenum: The number of the line to check.
4290 error: The function to call with any errors found.
4291 """
4292 line = clean_lines.elided[linenum]
4293
4294 # Don't try to do spacing checks for operator methods. Do this by
4295 # replacing the troublesome characters with something else,
4296 # preserving column position for all other characters.
4297 #
4298 # The replacement is done repeatedly to avoid false positives from
4299 # operators that call operators.
4300 while True:
4301 match = re.match(r"^(.*\boperator\b)(\S+)(\s*\(.*)$", line)
4302 if match:
4303 line = match.group(1) + ("_" * len(match.group(2))) + match.group(3)
4304 else:
4305 break
4306
4307 # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )".
4308 # Otherwise not. Note we only check for non-spaces on *both* sides;
4309 # sometimes people put non-spaces on one side when aligning ='s among
4310 # many lines (not that this is behavior that I approve of...)
4311 if (
4312 (re.search(r"[\w.]=", line) or re.search(r"=[\w.]", line))
4313 and not re.search(r"\b(if|while|for) ", line)
4314 # Operators taken from [lex.operators] in C++11 standard.
4315 and not re.search(r"(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)", line)
4316 and not re.search(r"operator=", line)
4317 ):
4318 error(filename, linenum, "whitespace/operators", 4, "Missing spaces around =")
4319
4320 # It's ok not to have spaces around binary operators like + - * /, but if
4321 # there's too little whitespace, we get concerned. It's hard to tell,
4322 # though, so we punt on this one for now. TODO(google).
4323
4324 # You should always have whitespace around binary operators.
4325 #
4326 # Check <= and >= first to avoid false positives with < and >, then
4327 # check non-include lines for spacing around < and >.
4328 #
4329 # If the operator is followed by a comma, assume it's be used in a
4330 # macro context and don't do any checks. This avoids false
4331 # positives.
4332 #
4333 # Note that && is not included here. This is because there are too
4334 # many false positives due to RValue references.
4335 match = re.search(r"[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]", line)
4336 if match:
4337 # TODO(google): support alternate operators
4338 error(
4339 filename, linenum, "whitespace/operators", 3, f"Missing spaces around {match.group(1)}"
4340 )

Callers 1

CheckStyleFunction · 0.85

Calls 2

CloseExpressionFunction · 0.85
ReverseCloseExpressionFunction · 0.85

Tested by

no test coverage detected