MCPcopy Create free account
hub / github.com/alibaba/GraphScope / CheckBraces

Function CheckBraces

analytical_engine/misc/cpplint.py:4235–4351  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 1

CheckStyleFunction · 0.85

Calls 8

GetPreviousNonBlankLineFunction · 0.85
SearchFunction · 0.85
GetLineWidthFunction · 0.85
CloseExpressionFunction · 0.85
GetIndentLevelFunction · 0.85
findMethod · 0.80
MatchFunction · 0.70
endMethod · 0.65

Tested by

no test coverage detected