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)
| 3734 | |
| 3735 | |
| 3736 | def CheckBraces(filename, clean_lines, linenum, error): |
| 3737 | """Looks for misplaced braces (e.g. at the end of line). |
| 3738 | |
| 3739 | Args: |
| 3740 | filename: The name of the current file. |
| 3741 | clean_lines: A CleansedLines instance containing the file. |
| 3742 | linenum: The number of the line to check. |
| 3743 | error: The function to call with any errors found. |
| 3744 | """ |
| 3745 | |
| 3746 | line = clean_lines.elided[linenum] # get rid of comments and strings |
| 3747 | |
| 3748 | if Match(r'\s*{\s*$', line): |
| 3749 | # We allow an open brace to start a line in the case where someone is using |
| 3750 | # braces in a block to explicitly create a new scope, which is commonly used |
| 3751 | # to control the lifetime of stack-allocated variables. Braces are also |
| 3752 | # used for brace initializers inside function calls. We don't detect this |
| 3753 | # perfectly: we just don't complain if the last non-whitespace character on |
| 3754 | # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the |
| 3755 | # previous line starts a preprocessor block. We also allow a brace on the |
| 3756 | # following line if it is part of an array initialization and would not fit |
| 3757 | # within the 80 character limit of the preceding line. |
| 3758 | prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] |
| 3759 | if (not Search(r'[,;:}{(]\s*$', prevline) and |
| 3760 | not Match(r'\s*#', prevline) and |
| 3761 | not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)): |
| 3762 | error(filename, linenum, 'whitespace/braces', 4, |
| 3763 | '{ should almost always be at the end of the previous line') |
| 3764 | |
| 3765 | # An else clause should be on the same line as the preceding closing brace. |
| 3766 | if Match(r'\s*else\b\s*(?:if\b|\{|$)', line): |
| 3767 | prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] |
| 3768 | if Match(r'\s*}\s*$', prevline): |
| 3769 | error(filename, linenum, 'whitespace/newline', 4, |
| 3770 | 'An else should appear on the same line as the preceding }') |
| 3771 | |
| 3772 | # If braces come on one side of an else, they should be on both. |
| 3773 | # However, we have to worry about "else if" that spans multiple lines! |
| 3774 | if Search(r'else if\s*\(', line): # could be multi-line if |
| 3775 | brace_on_left = bool(Search(r'}\s*else if\s*\(', line)) |
| 3776 | # find the ( after the if |
| 3777 | pos = line.find('else if') |
| 3778 | pos = line.find('(', pos) |
| 3779 | if pos > 0: |
| 3780 | (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos) |
| 3781 | brace_on_right = endline[endpos:].find('{') != -1 |
| 3782 | if brace_on_left != brace_on_right: # must be brace after if |
| 3783 | error(filename, linenum, 'readability/braces', 5, |
| 3784 | 'If an else has a brace on one side, it should have it on both') |
| 3785 | elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line): |
| 3786 | error(filename, linenum, 'readability/braces', 5, |
| 3787 | 'If an else has a brace on one side, it should have it on both') |
| 3788 | |
| 3789 | # Likewise, an else should never have the else clause on the same line |
| 3790 | if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line): |
| 3791 | error(filename, linenum, 'whitespace/newline', 4, |
| 3792 | 'Else clause should never be on same line as else (use 2 lines)') |
| 3793 |
no test coverage detected