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)
| 4349 | |
| 4350 | |
| 4351 | def CheckOperatorSpacing(filename, clean_lines, linenum, error): |
| 4352 | """Checks for horizontal spacing around operators. |
| 4353 | |
| 4354 | Args: |
| 4355 | filename: The name of the current file. |
| 4356 | clean_lines: A CleansedLines instance containing the file. |
| 4357 | linenum: The number of the line to check. |
| 4358 | error: The function to call with any errors found. |
| 4359 | """ |
| 4360 | line = clean_lines.elided[linenum] |
| 4361 | |
| 4362 | # Don't try to do spacing checks for operator methods. Do this by |
| 4363 | # replacing the troublesome characters with something else, |
| 4364 | # preserving column position for all other characters. |
| 4365 | # |
| 4366 | # The replacement is done repeatedly to avoid false positives from |
| 4367 | # operators that call operators. |
| 4368 | while True: |
| 4369 | match = re.match(r"^(.*\boperator\b)(\S+)(\s*\(.*)$", line) |
| 4370 | if match: |
| 4371 | line = match.group(1) + ("_" * len(match.group(2))) + match.group(3) |
| 4372 | else: |
| 4373 | break |
| 4374 | |
| 4375 | # We allow no-spaces around = within an if: "if ( (a=Foo()) == 0 )". |
| 4376 | # Otherwise not. Note we only check for non-spaces on *both* sides; |
| 4377 | # sometimes people put non-spaces on one side when aligning ='s among |
| 4378 | # many lines (not that this is behavior that I approve of...) |
| 4379 | if ( |
| 4380 | (re.search(r"[\w.]=", line) or re.search(r"=[\w.]", line)) |
| 4381 | and not re.search(r"\b(if|while|for) ", line) |
| 4382 | # Operators taken from [lex.operators] in C++11 standard. |
| 4383 | and not re.search(r"(>=|<=|==|!=|&=|\^=|\|=|\+=|\*=|\/=|\%=)", line) |
| 4384 | and not re.search(r"operator=", line) |
| 4385 | ): |
| 4386 | error(filename, linenum, "whitespace/operators", 4, "Missing spaces around =") |
| 4387 | |
| 4388 | # It's ok not to have spaces around binary operators like + - * /, but if |
| 4389 | # there's too little whitespace, we get concerned. It's hard to tell, |
| 4390 | # though, so we punt on this one for now. TODO(google). |
| 4391 | |
| 4392 | # You should always have whitespace around binary operators. |
| 4393 | # |
| 4394 | # Check <= and >= first to avoid false positives with < and >, then |
| 4395 | # check non-include lines for spacing around < and >. |
| 4396 | # |
| 4397 | # If the operator is followed by a comma, assume it's be used in a |
| 4398 | # macro context and don't do any checks. This avoids false |
| 4399 | # positives. |
| 4400 | # |
| 4401 | # Note that && is not included here. This is because there are too |
| 4402 | # many false positives due to RValue references. |
| 4403 | match = re.search(r"[^<>=!\s](==|!=|<=|>=|\|\|)[^<>=!\s,;\)]", line) |
| 4404 | if match: |
| 4405 | # TODO(google): support alternate operators |
| 4406 | error( |
| 4407 | filename, linenum, "whitespace/operators", 3, f"Missing spaces around {match.group(1)}" |
| 4408 | ) |
no test coverage detected
searching dependent graphs…