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)
| 4549 | |
| 4550 | |
| 4551 | def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error): |
| 4552 | """Checks for horizontal spacing near commas. |
| 4553 | |
| 4554 | Args: |
| 4555 | filename: The name of the current file. |
| 4556 | clean_lines: A CleansedLines instance containing the file. |
| 4557 | linenum: The number of the line to check. |
| 4558 | nesting_state: A NestingState instance which maintains information about |
| 4559 | the current stack of nested blocks being parsed. |
| 4560 | error: The function to call with any errors found. |
| 4561 | """ |
| 4562 | line = clean_lines.elided[linenum] |
| 4563 | |
| 4564 | # Except after an opening paren, or after another opening brace (in case of |
| 4565 | # an initializer list, for instance), you should have spaces before your |
| 4566 | # braces when they are delimiting blocks, classes, namespaces etc. |
| 4567 | # And since you should never have braces at the beginning of a line, |
| 4568 | # this is an easy test. Except that braces used for initialization don't |
| 4569 | # follow the same rule; we often don't want spaces before those. |
| 4570 | |
| 4571 | if match := re.match(r"^(.*[^ ({>]){", line): |
| 4572 | # Try a bit harder to check for brace initialization. This |
| 4573 | # happens in one of the following forms: |
| 4574 | # Constructor() : initializer_list_{} { ... } |
| 4575 | # Constructor{}.MemberFunction() |
| 4576 | # Type variable{}; |
| 4577 | # FunctionCall(type{}, ...); |
| 4578 | # LastArgument(..., type{}); |
| 4579 | # LOG(INFO) << type{} << " ..."; |
| 4580 | # map_of_type[{...}] = ...; |
| 4581 | # ternary = expr ? new type{} : nullptr; |
| 4582 | # OuterTemplate<InnerTemplateConstructor<Type>{}> |
| 4583 | # |
| 4584 | # We check for the character following the closing brace, and |
| 4585 | # silence the warning if it's one of those listed above, i.e. |
| 4586 | # "{.;,)<>]:". |
| 4587 | # |
| 4588 | # To account for nested initializer list, we allow any number of |
| 4589 | # closing braces up to "{;,)<". We can't simply silence the |
| 4590 | # warning on first sight of closing brace, because that would |
| 4591 | # cause false negatives for things that are not initializer lists. |
| 4592 | # Silence this: But not this: |
| 4593 | # Outer{ if (...) { |
| 4594 | # Inner{...} if (...){ // Missing space before { |
| 4595 | # }; } |
| 4596 | # |
| 4597 | # There is a false negative with this approach if people inserted |
| 4598 | # spurious semicolons, e.g. "if (cond){};", but we will catch the |
| 4599 | # spurious semicolon with a separate check. |
| 4600 | leading_text = match.group(1) |
| 4601 | (endline, endlinenum, endpos) = CloseExpression(clean_lines, linenum, len(match.group(1))) |
| 4602 | trailing_text = "" |
| 4603 | if endpos > -1: |
| 4604 | trailing_text = endline[endpos:] |
| 4605 | for offset in range(endlinenum + 1, min(endlinenum + 3, clean_lines.NumLines() - 1)): |
| 4606 | trailing_text += clean_lines.elided[offset] |
| 4607 | # We also suppress warnings for `uint64_t{expression}` etc., as the style |
| 4608 | # guide recommends brace initialization for integral types to avoid |
no test coverage detected