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)
| 4230 | |
| 4231 | |
| 4232 | def CheckBraces(filename, clean_lines, linenum, error): |
| 4233 | """Looks for misplaced braces (e.g. at the end of line). |
| 4234 | |
| 4235 | Args: |
| 4236 | filename: The name of the current file. |
| 4237 | clean_lines: A CleansedLines instance containing the file. |
| 4238 | linenum: The number of the line to check. |
| 4239 | error: The function to call with any errors found. |
| 4240 | """ |
| 4241 | |
| 4242 | line = clean_lines.elided[linenum] # get rid of comments and strings |
| 4243 | |
| 4244 | if Match(r'\s*{\s*$', line): |
| 4245 | # We allow an open brace to start a line in the case where someone is using |
| 4246 | # braces in a block to explicitly create a new scope, which is commonly used |
| 4247 | # to control the lifetime of stack-allocated variables. Braces are also |
| 4248 | # used for brace initializers inside function calls. We don't detect this |
| 4249 | # perfectly: we just don't complain if the last non-whitespace character on |
| 4250 | # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the |
| 4251 | # previous line starts a preprocessor block. We also allow a brace on the |
| 4252 | # following line if it is part of an array initialization and would not fit |
| 4253 | # within the 80 character limit of the preceding line. |
| 4254 | prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] |
| 4255 | if (not Search(r'[,;:}{(]\s*$', prevline) and |
| 4256 | not Match(r'\s*#', prevline) and |
| 4257 | not (GetLineWidth(prevline) > _line_length - 2 and '[]' in prevline)): |
| 4258 | error(filename, linenum, 'whitespace/braces', 4, |
| 4259 | '{ should almost always be at the end of the previous line') |
| 4260 | |
| 4261 | # An else clause should be on the same line as the preceding closing brace. |
| 4262 | if Match(r'\s*else\b\s*(?:if\b|\{|$)', line): |
| 4263 | prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0] |
| 4264 | if Match(r'\s*}\s*$', prevline): |
| 4265 | error(filename, linenum, 'whitespace/newline', 4, |
| 4266 | 'An else should appear on the same line as the preceding }') |
| 4267 | |
| 4268 | # If braces come on one side of an else, they should be on both. |
| 4269 | # However, we have to worry about "else if" that spans multiple lines! |
| 4270 | if Search(r'else if\s*\(', line): # could be multi-line if |
| 4271 | brace_on_left = bool(Search(r'}\s*else if\s*\(', line)) |
| 4272 | # find the ( after the if |
| 4273 | pos = line.find('else if') |
| 4274 | pos = line.find('(', pos) |
| 4275 | if pos > 0: |
| 4276 | (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos) |
| 4277 | brace_on_right = endline[endpos:].find('{') != -1 |
| 4278 | if brace_on_left != brace_on_right: # must be brace after if |
| 4279 | error(filename, linenum, 'readability/braces', 5, |
| 4280 | 'If an else has a brace on one side, it should have it on both') |
| 4281 | elif Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line): |
| 4282 | error(filename, linenum, 'readability/braces', 5, |
| 4283 | 'If an else has a brace on one side, it should have it on both') |
| 4284 | |
| 4285 | # Likewise, an else should never have the else clause on the same line |
| 4286 | if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line): |
| 4287 | error(filename, linenum, 'whitespace/newline', 4, |
| 4288 | 'Else clause should never be on same line as else (use 2 lines)') |
| 4289 |
no test coverage detected