parameters: (filename, clean_lines, linenum, error)
docstring: 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.
docstring_summary: Looks for misplaced braces (e.g. at the end of line).
docstring_tokens: Looks for misplaced braces ( e . g . at the end of line ) .
function: def CheckBraces(filename, clean_lines, linenum, error):
    """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.
    """
  
    line = clean_lines.elided[linenum]        # get rid of comments and strings
  
    if Match(r'\s*{\s*$', line):
      # We allow an open brace to start a line in the case where someone
      # is using braces in a block to explicitly create a new scope,
      # which is commonly used to control the lifetime of
      # stack-allocated variables.  We don't detect this perfectly: we
      # just don't complain if the last non-whitespace character on the
      # previous non-blank line is ';', ':', '{', or '}', or if the previous
      # line starts a preprocessor block.
      prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
      if (not Search(r'[;:}{]\s*$', prevline) and
          not Match(r'\s*#', prevline)):
        error(filename, linenum, 'whitespace/braces', 4,
              '{ should almost always be at the end of the previous line')
  
    # An else clause should be on the same line as the preceding closing brace.
    if Match(r'\s*else\s*', line):
      prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
      if Match(r'\s*}\s*$', prevline):
        error(filename, linenum, 'whitespace/newline', 4,
              'An else should appear on the same line as the preceding }')
  
    # If braces come on one side of an else, they should be on both.
    # However, we have to worry about "else if" that spans multiple lines!
    if Search(r'}\s*else[^{]*$', line) or Match(r'[^}]*else\s*{', line):
      if Search(r'}\s*else if([^{]*)$', line):       # could be multi-line if
        # find the ( after the if
        pos = line.find('else if')
        pos = line.find('(', pos)
        if pos > 0:
          (endline, _, endpos) = CloseExpression(clean_lines, linenum, pos)
          if endline[endpos:].find('{') == -1:    # must be brace after if
            error(filename, linenum, 'readability/braces', 5,
                  'If an else has a brace on one side, it should have it on both')
      else:            # common case: else not followed by a multi-line if
        error(filename, linenum, 'readability/braces', 5,
              'If an else has a brace on one side, it should have it on both')
  
    # Likewise, an else should never have the else clause on the same line
    if Search(r'\belse [^\s{]', line) and not Search(r'\belse if\b', line):
      error(filename, linenum, 'whitespace/newline', 4,
            'Else clause should never be on same line as else (use 2 lines)')
  
    # In the same way, a do/while should never be on one line
    if Match(r'\s*do [^\s{]', line):
      error(filename, linenum, 'whitespace/newline', 4,
            'do/while clauses should not be on a single line')
  
    # Braces shouldn't be followed by a ; unless they're defining a struct
    # or initializing an array.
    # We can't tell in general, but we can for some common cases.
    prevlinenum = linenum
    while True:
      (prevline, prevlinenum) = GetPreviousNonBlankLine(clean_lines, prevlinenum)
      if Match(r'\s+{.*}\s*;', line) and not prevline.count(';'):
        line = prevline + line
      else:
        break
    if (Search(r'{.*}\s*;', line) and
        line.count('{') == line.count('}') and
        not Search(r'struct|class|enum|\s*=\s*{', line)):
      error(filename, linenum, 'readability/braces', 4,
            "You don't need a ; after a }")
function_tokens: def CheckBraces ( filename , clean_lines , linenum , error ) : line = clean_lines . elided [ linenum ] # get rid of comments and strings if Match ( r'\s*{\s*$' , line ) : # We allow an open brace to start a line in the case where someone # is using braces in a block to explicitly create a new scope, # which is commonly used to control the lifetime of # stack-allocated variables.  We don't detect this perfectly: we # just don't complain if the last non-whitespace character on the # previous non-blank line is ';', ':', '{', or '}', or if the previous # line starts a preprocessor block. prevline = GetPreviousNonBlankLine ( clean_lines , linenum ) [ 0 ] if ( not Search ( r'[;:}{]\s*$' , prevline ) and not Match ( r'\s*#' , prevline ) ) : error ( filename , linenum , 'whitespace/braces' , 4 , '{ should almost always be at the end of the previous line' ) # An else clause should be on the same line as the preceding closing brace. if Match ( r'\s*else\s*' , line ) : prevline = GetPreviousNonBlankLine ( clean_lines , linenum ) [ 0 ] if Match ( r'\s*}\s*$' , prevline ) : error ( filename , linenum , 'whitespace/newline' , 4 , 'An else should appear on the same line as the preceding }' ) # If braces come on one side of an else, they should be on both. # However, we have to worry about "else if" that spans multiple lines! if Search ( r'}\s*else[^{]*$' , line ) or Match ( r'[^}]*else\s*{' , line ) : if Search ( r'}\s*else if([^{]*)$' , line ) : # could be multi-line if # find the ( after the if pos = line . find ( 'else if' ) pos = line . find ( '(' , pos ) if pos > 0 : ( endline , _ , endpos ) = CloseExpression ( clean_lines , linenum , pos ) if endline [ endpos : ] . find ( '{' ) == - 1 : # must be brace after if error ( filename , linenum , 'readability/braces' , 5 , 'If an else has a brace on one side, it should have it on both' ) else : # common case: else not followed by a multi-line if error ( filename , linenum , 'readability/braces' , 5 , 'If an else has a brace on one side, it should have it on both' ) # Likewise, an else should never have the else clause on the same line if Search ( r'\belse [^\s{]' , line ) and not Search ( r'\belse if\b' , line ) : error ( filename , linenum , 'whitespace/newline' , 4 , 'Else clause should never be on same line as else (use 2 lines)' ) # In the same way, a do/while should never be on one line if Match ( r'\s*do [^\s{]' , line ) : error ( filename , linenum , 'whitespace/newline' , 4 , 'do/while clauses should not be on a single line' ) # Braces shouldn't be followed by a ; unless they're defining a struct # or initializing an array. # We can't tell in general, but we can for some common cases. prevlinenum = linenum while True : ( prevline , prevlinenum ) = GetPreviousNonBlankLine ( clean_lines , prevlinenum ) if Match ( r'\s+{.*}\s*;' , line ) and not prevline . count ( ';' ) : line = prevline + line else : break if ( Search ( r'{.*}\s*;' , line ) and line . count ( '{' ) == line . count ( '}' ) and not Search ( r'struct|class|enum|\s*=\s*{' , line ) ) : error ( filename , linenum , 'readability/braces' , 4 , "You don't need a ; after a }" )