Checks for horizontal spacing around parentheses. 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)
| 4468 | |
| 4469 | |
| 4470 | def CheckParenthesisSpacing(filename, clean_lines, linenum, error): |
| 4471 | """Checks for horizontal spacing around parentheses. |
| 4472 | |
| 4473 | Args: |
| 4474 | filename: The name of the current file. |
| 4475 | clean_lines: A CleansedLines instance containing the file. |
| 4476 | linenum: The number of the line to check. |
| 4477 | error: The function to call with any errors found. |
| 4478 | """ |
| 4479 | line = clean_lines.elided[linenum] |
| 4480 | |
| 4481 | # No spaces after an if, while, switch, or for |
| 4482 | match = re.search(r" (if\(|for\(|while\(|switch\()", line) |
| 4483 | if match: |
| 4484 | error( |
| 4485 | filename, linenum, "whitespace/parens", 5, f"Missing space before ( in {match.group(1)}" |
| 4486 | ) |
| 4487 | |
| 4488 | # For if/for/while/switch, the left and right parens should be |
| 4489 | # consistent about how many spaces are inside the parens, and |
| 4490 | # there should either be zero or one spaces inside the parens. |
| 4491 | # We don't want: "if ( foo)" or "if ( foo )". |
| 4492 | # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed. |
| 4493 | match = re.search( |
| 4494 | r"\b(if|for|while|switch)\s*" |
| 4495 | r"\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$", |
| 4496 | line, |
| 4497 | ) |
| 4498 | if match: |
| 4499 | if len(match.group(2)) != len(match.group(4)) and not ( |
| 4500 | match.group(3) == ";" |
| 4501 | and len(match.group(2)) == 1 + len(match.group(4)) |
| 4502 | or not match.group(2) |
| 4503 | and re.search(r"\bfor\s*\(.*; \)", line) |
| 4504 | ): |
| 4505 | error( |
| 4506 | filename, |
| 4507 | linenum, |
| 4508 | "whitespace/parens", |
| 4509 | 5, |
| 4510 | f"Mismatching spaces inside () in {match.group(1)}", |
| 4511 | ) |
| 4512 | if len(match.group(2)) not in [0, 1]: |
| 4513 | error( |
| 4514 | filename, |
| 4515 | linenum, |
| 4516 | "whitespace/parens", |
| 4517 | 5, |
| 4518 | f"Should have zero or one spaces inside ( and ) in {match.group(1)}", |
| 4519 | ) |
| 4520 | |
| 4521 | |
| 4522 | def CheckCommaSpacing(filename, clean_lines, linenum, error): |
no test coverage detected
searching dependent graphs…