Checks for horizontal spacing near commas. Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. nesting_state: A NestingState instance which maintains information about
(filename, clean_lines, linenum, nesting_state, error)
| 4617 | |
| 4618 | |
| 4619 | def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error): |
| 4620 | """Checks for horizontal spacing near commas. |
| 4621 | |
| 4622 | Args: |
| 4623 | filename: The name of the current file. |
| 4624 | clean_lines: A CleansedLines instance containing the file. |
| 4625 | linenum: The number of the line to check. |
| 4626 | nesting_state: A NestingState instance which maintains information about |
| 4627 | the current stack of nested blocks being parsed. |
| 4628 | error: The function to call with any errors found. |
| 4629 | """ |
| 4630 | line = clean_lines.elided[linenum] |
| 4631 | |
| 4632 | # Except after an opening paren, or after another opening brace (in case of |
| 4633 | # an initializer list, for instance), you should have spaces before your |
| 4634 | # braces when they are delimiting blocks, classes, namespaces etc. |
| 4635 | # And since you should never have braces at the beginning of a line, |
| 4636 | # this is an easy test. Except that braces used for initialization don't |
| 4637 | # follow the same rule; we often don't want spaces before those. |
| 4638 | |
| 4639 | if match := re.match(r"^(.*[^ ({>]){", line): |
| 4640 | # Try a bit harder to check for brace initialization. This |
| 4641 | # happens in one of the following forms: |
| 4642 | # Constructor() : initializer_list_{} { ... } |
| 4643 | # Constructor{}.MemberFunction() |
| 4644 | # Type variable{}; |
| 4645 | # FunctionCall(type{}, ...); |
| 4646 | # LastArgument(..., type{}); |
| 4647 | # LOG(INFO) << type{} << " ..."; |
| 4648 | # map_of_type[{...}] = ...; |
| 4649 | # ternary = expr ? new type{} : nullptr; |
| 4650 | # OuterTemplate<InnerTemplateConstructor<Type>{}> |
| 4651 | # |
| 4652 | # We check for the character following the closing brace, and |
| 4653 | # silence the warning if it's one of those listed above, i.e. |
| 4654 | # "{.;,)<>]:". |
| 4655 | # |
| 4656 | # To account for nested initializer list, we allow any number of |
| 4657 | # closing braces up to "{;,)<". We can't simply silence the |
| 4658 | # warning on first sight of closing brace, because that would |
| 4659 | # cause false negatives for things that are not initializer lists. |
| 4660 | # Silence this: But not this: |
| 4661 | # Outer{ if (...) { |
| 4662 | # Inner{...} if (...){ // Missing space before { |
| 4663 | # }; } |
| 4664 | # |
| 4665 | # There is a false negative with this approach if people inserted |
| 4666 | # spurious semicolons, e.g. "if (cond){};", but we will catch the |
| 4667 | # spurious semicolon with a separate check. |
| 4668 | leading_text = match.group(1) |
| 4669 | (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, len(match.group(1))) |
| 4670 | trailing_text = "" |
| 4671 | if endpos > -1: |
| 4672 | trailing_text = endline[endpos:] |
| 4673 | for offset in range(endlinenum + 1, min(endlinenum + 3, clean_lines.NumLines() - 1)): |
| 4674 | trailing_text += clean_lines.elided[offset] |
| 4675 | # We also suppress warnings for `uint64_t{expression}` etc., as the style |
| 4676 | # guide recommends brace initialization for integral types to avoid |
no test coverage detected
searching dependent graphs…