Looks for misplaced braces (e.g. at the end of line). 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)
| 4750 | |
| 4751 | |
| 4752 | def CheckBraces(filename, clean_lines, linenum, error): |
| 4753 | """Looks for misplaced braces (e.g. at the end of line). |
| 4754 | |
| 4755 | Args: |
| 4756 | filename: The name of the current file. |
| 4757 | clean_lines: A CleansedLines instance containing the file. |
| 4758 | linenum: The number of the line to check. |
| 4759 | error: The function to call with any errors found. |
| 4760 | """ |
| 4761 | |
| 4762 | line = clean_lines.elided[linenum] # get rid of comments and strings |
| 4763 | |
| 4764 | if re.match(r"\s*{\s*$", line): |
| 4765 | # We allow an open brace to start a line in the case where someone is using |
| 4766 | # braces in a block to explicitly create a new scope, which is commonly used |
| 4767 | # to control the lifetime of stack-allocated variables. Braces are also |
| 4768 | # used for brace initializers inside function calls. We don't detect this |
| 4769 | # perfectly: we just don't complain if the last non-whitespace character on |
| 4770 | # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the |
| 4771 | # previous line starts a preprocessor block. We also allow a brace on the |
| 4772 | # following line if it is part of an array initialization and would not fit |
| 4773 | # within the 80 character limit of the preceding line. |
| 4774 | prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] |
| 4775 | if ( |
| 4776 | not re.search(r"[,;:}{(]\s*$", prevline) |
| 4777 | and not re.match(r"\s*#", prevline) |
| 4778 | and not (GetLineWidth(prevline) > _line_length - 2 and "[]" in prevline) |
| 4779 | ): |
| 4780 | error( |
| 4781 | filename, |
| 4782 | linenum, |
| 4783 | "whitespace/braces", |
| 4784 | 4, |
| 4785 | "{ should almost always be at the end of the previous line", |
| 4786 | ) |
| 4787 | |
| 4788 | # An else clause should be on the same line as the preceding closing brace. |
| 4789 | if last_wrong := re.match(r"\s*else\b\s*(?:if\b|\{|$)", line): |
| 4790 | prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] |
| 4791 | if re.match(r"\s*}\s*$", prevline): |
| 4792 | error( |
| 4793 | filename, |
| 4794 | linenum, |
| 4795 | "whitespace/newline", |
| 4796 | 4, |
| 4797 | "An else should appear on the same line as the preceding }", |
| 4798 | ) |
| 4799 | else: |
| 4800 | last_wrong = False |
| 4801 | |
| 4802 | # If braces come on one side of an else, they should be on both. |
| 4803 | # However, we have to worry about "else if" that spans multiple lines! |
| 4804 | if re.search(r"else if\s*\(", line): # could be multi-line if |
| 4805 | brace_on_left = bool(re.search(r"}\s*else if\s*\(", line)) |
| 4806 | # find the ( after the if |
| 4807 | pos = line.find("else if") |
| 4808 | pos = line.find("(", pos) |
| 4809 | if pos > 0: |
no test coverage detected