| @@ 3555-3590 (lines=36) @@ | ||
| 3552 | 'Extra space for operator %s' % match.group(1)) |
|
| 3553 | ||
| 3554 | ||
| 3555 | def CheckParenthesisSpacing(filename, clean_lines, linenum, error): |
|
| 3556 | """Checks for horizontal spacing around parentheses. |
|
| 3557 | ||
| 3558 | Args: |
|
| 3559 | filename: The name of the current file. |
|
| 3560 | clean_lines: A CleansedLines instance containing the file. |
|
| 3561 | linenum: The number of the line to check. |
|
| 3562 | error: The function to call with any errors found. |
|
| 3563 | """ |
|
| 3564 | line = clean_lines.elided[linenum] |
|
| 3565 | ||
| 3566 | # No spaces after an if, while, switch, or for |
|
| 3567 | match = Search(r' (if\(|for\(|while\(|switch\()', line) |
|
| 3568 | if match: |
|
| 3569 | error(filename, linenum, 'whitespace/parens', 5, |
|
| 3570 | 'Missing space before ( in %s' % match.group(1)) |
|
| 3571 | ||
| 3572 | # For if/for/while/switch, the left and right parens should be |
|
| 3573 | # consistent about how many spaces are inside the parens, and |
|
| 3574 | # there should either be zero or one spaces inside the parens. |
|
| 3575 | # We don't want: "if ( foo)" or "if ( foo )". |
|
| 3576 | # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed. |
|
| 3577 | match = Search(r'\b(if|for|while|switch)\s*' |
|
| 3578 | r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$', |
|
| 3579 | line) |
|
| 3580 | if match: |
|
| 3581 | if len(match.group(2)) != len(match.group(4)): |
|
| 3582 | if not (match.group(3) == ';' and |
|
| 3583 | len(match.group(2)) == 1 + len(match.group(4)) or |
|
| 3584 | not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)): |
|
| 3585 | error(filename, linenum, 'whitespace/parens', 5, |
|
| 3586 | 'Mismatching spaces inside () in %s' % match.group(1)) |
|
| 3587 | if len(match.group(2)) not in [0, 1]: |
|
| 3588 | error(filename, linenum, 'whitespace/parens', 5, |
|
| 3589 | 'Should have zero or one spaces inside ( and ) in %s' % |
|
| 3590 | match.group(1)) |
|
| 3591 | ||
| 3592 | ||
| 3593 | def CheckCommaSpacing(filename, clean_lines, linenum, error): |
|
| @@ 3555-3590 (lines=36) @@ | ||
| 3552 | 'Extra space for operator %s' % match.group(1)) |
|
| 3553 | ||
| 3554 | ||
| 3555 | def CheckParenthesisSpacing(filename, clean_lines, linenum, error): |
|
| 3556 | """Checks for horizontal spacing around parentheses. |
|
| 3557 | ||
| 3558 | Args: |
|
| 3559 | filename: The name of the current file. |
|
| 3560 | clean_lines: A CleansedLines instance containing the file. |
|
| 3561 | linenum: The number of the line to check. |
|
| 3562 | error: The function to call with any errors found. |
|
| 3563 | """ |
|
| 3564 | line = clean_lines.elided[linenum] |
|
| 3565 | ||
| 3566 | # No spaces after an if, while, switch, or for |
|
| 3567 | match = Search(r' (if\(|for\(|while\(|switch\()', line) |
|
| 3568 | if match: |
|
| 3569 | error(filename, linenum, 'whitespace/parens', 5, |
|
| 3570 | 'Missing space before ( in %s' % match.group(1)) |
|
| 3571 | ||
| 3572 | # For if/for/while/switch, the left and right parens should be |
|
| 3573 | # consistent about how many spaces are inside the parens, and |
|
| 3574 | # there should either be zero or one spaces inside the parens. |
|
| 3575 | # We don't want: "if ( foo)" or "if ( foo )". |
|
| 3576 | # Exception: "for ( ; foo; bar)" and "for (foo; bar; )" are allowed. |
|
| 3577 | match = Search(r'\b(if|for|while|switch)\s*' |
|
| 3578 | r'\(([ ]*)(.).*[^ ]+([ ]*)\)\s*{\s*$', |
|
| 3579 | line) |
|
| 3580 | if match: |
|
| 3581 | if len(match.group(2)) != len(match.group(4)): |
|
| 3582 | if not (match.group(3) == ';' and |
|
| 3583 | len(match.group(2)) == 1 + len(match.group(4)) or |
|
| 3584 | not match.group(2) and Search(r'\bfor\s*\(.*; \)', line)): |
|
| 3585 | error(filename, linenum, 'whitespace/parens', 5, |
|
| 3586 | 'Mismatching spaces inside () in %s' % match.group(1)) |
|
| 3587 | if len(match.group(2)) not in [0, 1]: |
|
| 3588 | error(filename, linenum, 'whitespace/parens', 5, |
|
| 3589 | 'Should have zero or one spaces inside ( and ) in %s' % |
|
| 3590 | match.group(1)) |
|
| 3591 | ||
| 3592 | ||
| 3593 | def CheckCommaSpacing(filename, clean_lines, linenum, error): |
|