parameters: (clean_lines, line_number, error)
docstring: Looks for misplaced braces (e.g. at the end of line).
  
      Args:
        clean_lines: A CleansedLines instance containing the file.
        line_number: 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 check_braces(clean_lines, line_number, error):
      """Looks for misplaced braces (e.g. at the end of line).
  
      Args:
        clean_lines: A CleansedLines instance containing the file.
        line_number: The number of the line to check.
        error: The function to call with any errors found.
      """
  
      line = clean_lines.elided[line_number]  # 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 for function definition or 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 ') const' and doesn't begin with 'if|for|while|switch|else'.
          # We also allow '#' for #endif and '=' for array initialization,
          # and '- (' and '+ (' for Objective-C methods.
          previous_line = get_previous_non_blank_line(clean_lines, line_number)[0]
          if ((not search(r'[;:}{)=]\s*$|\)\s*((const|override|const override)\s*)?(->\s*\S+)?\s*$', previous_line)
               or search(r'\b(if|for|while|switch|else|NS_ENUM)\b', previous_line))
              and previous_line.find('#') < 0
              and previous_line.find('- (') != 0
              and previous_line.find('+ (') != 0):
              error(line_number, 'whitespace/braces', 4,
                    'This { should be at the end of the previous line')
      elif (search(r'\)\s*(((const|override)\s*)*\s*)?{\s*$', line)
            and line.count('(') == line.count(')')
            and not search(r'(\s*(if|for|while|switch|NS_ENUM|@synchronized)|} @catch)\b', line)
            and line.find("](") < 0
            and not match(r'\s+[A-Z_][A-Z_0-9]+\b', line)):
          error(line_number, 'whitespace/braces', 4,
                'Place brace on its own line for function definitions.')
  
      if (match(r'\s*}\s*(else\s*({\s*)?)?$', line) and line_number > 1):
          # We check if a closed brace has started a line to see if a
          # one line control statement was previous.
          previous_line = clean_lines.elided[line_number - 2]
          last_open_brace = previous_line.rfind('{')
          if (last_open_brace != -1 and previous_line.find('}', last_open_brace) == -1
              and search(r'\b(if|for|while|else)\b', previous_line)):
              error(line_number, 'whitespace/braces', 4,
                    'One line control clauses should not use braces.')
  
      # An else clause should be on the same line as the preceding closing brace.
      if match(r'\s*else\s*', line):
          previous_line = get_previous_non_blank_line(clean_lines, line_number)[0]
          if match(r'\s*}\s*$', previous_line):
              error(line_number, 'whitespace/newline', 4,
                    'An else should appear on the same line as the preceding }')
  
      # 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(line_number, '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(line_number, 'whitespace/newline', 4,
                'do/while clauses should not be on a single line')
  
      # Multi line control clauses should use braces. We check the
      # indentation level of the statements.
      if (match(r'^\s*\b(if|for|while|else)\b\s', line)
          and match(r'.*[^{]$', line)
          and len(clean_lines.elided) > line_number + 2):
          has_braces = False
          begin_line = line
          begin_line_number = line_number
          while (clean_lines.elided[begin_line_number + 1].strip().startswith("&&")
              or clean_lines.elided[begin_line_number + 1].strip().startswith("||")
              or search(r'^#\S*', clean_lines.elided[begin_line_number + 1])):
              begin_line_number = begin_line_number + 1
              begin_line = clean_lines.elided[begin_line_number]
  
          if search(r'.*{(.*?\\)?$', begin_line):
              has_braces = True
  
          next_line = clean_lines.elided[begin_line_number + 1]
          after_next_line = clean_lines.elided[begin_line_number + 2]
          control_indent = search(r'^(?P<indentation>\s*).*', line).group('indentation')
          next_line_indent = search(r'^(?P<indentation>\s*).*', next_line).group('indentation')
          after_next_line_indent = search(r'^(?P<indentation>\s*).*', after_next_line).group('indentation')
          if (after_next_line != ''
              and not has_braces
              and control_indent < next_line_indent
              and control_indent < after_next_line_indent):
              error(line_number, 'whitespace/braces', 4, 'Multi line control clauses should use braces.')
  
      # 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.
      previous_line_number = line_number
      while True:
          (previous_line, previous_line_number) = get_previous_non_blank_line(clean_lines, previous_line_number)
          if match(r'\s+{.*}\s*;', line) and not previous_line.count(';'):
              line = previous_line + line
          else:
              break
      if (search(r'{.*}\s*;', line)
          and line.count('{') == line.count('}')
          and not search(r'struct|class|enum|\s*=\s*{', line)):
          error(line_number, 'readability/braces', 4,
                "You don't need a ; after a }")
function_tokens: def check_braces ( clean_lines , line_number , error ) : line = clean_lines . elided [ line_number ] # 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 for function definition or 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 ') const' and doesn't begin with 'if|for|while|switch|else'. # We also allow '#' for #endif and '=' for array initialization, # and '- (' and '+ (' for Objective-C methods. previous_line = get_previous_non_blank_line ( clean_lines , line_number ) [ 0 ] if ( ( not search ( r'[;:}{)=]\s*$|\)\s*((const|override|const override)\s*)?(->\s*\S+)?\s*$' , previous_line ) or search ( r'\b(if|for|while|switch|else|NS_ENUM)\b' , previous_line ) ) and previous_line . find ( '#' ) < 0 and previous_line . find ( '- (' ) != 0 and previous_line . find ( '+ (' ) != 0 ) : error ( line_number , 'whitespace/braces' , 4 , 'This { should be at the end of the previous line' ) elif ( search ( r'\)\s*(((const|override)\s*)*\s*)?{\s*$' , line ) and line . count ( '(' ) == line . count ( ')' ) and not search ( r'(\s*(if|for|while|switch|NS_ENUM|@synchronized)|} @catch)\b' , line ) and line . find ( "](" ) < 0 and not match ( r'\s+[A-Z_][A-Z_0-9]+\b' , line ) ) : error ( line_number , 'whitespace/braces' , 4 , 'Place brace on its own line for function definitions.' ) if ( match ( r'\s*}\s*(else\s*({\s*)?)?$' , line ) and line_number > 1 ) : # We check if a closed brace has started a line to see if a # one line control statement was previous. previous_line = clean_lines . elided [ line_number - 2 ] last_open_brace = previous_line . rfind ( '{' ) if ( last_open_brace != - 1 and previous_line . find ( '}' , last_open_brace ) == - 1 and search ( r'\b(if|for|while|else)\b' , previous_line ) ) : error ( line_number , 'whitespace/braces' , 4 , 'One line control clauses should not use braces.' ) # An else clause should be on the same line as the preceding closing brace. if match ( r'\s*else\s*' , line ) : previous_line = get_previous_non_blank_line ( clean_lines , line_number ) [ 0 ] if match ( r'\s*}\s*$' , previous_line ) : error ( line_number , 'whitespace/newline' , 4 , 'An else should appear on the same line as the preceding }' ) # 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 ( line_number , '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 ( line_number , 'whitespace/newline' , 4 , 'do/while clauses should not be on a single line' ) # Multi line control clauses should use braces. We check the # indentation level of the statements. if ( match ( r'^\s*\b(if|for|while|else)\b\s' , line ) and match ( r'.*[^{]$' , line ) and len ( clean_lines . elided ) > line_number + 2 ) : has_braces = False begin_line = line begin_line_number = line_number while ( clean_lines . elided [ begin_line_number + 1 ] . strip ( ) . startswith ( "&&" ) or clean_lines . elided [ begin_line_number + 1 ] . strip ( ) . startswith ( "||" ) or search ( r'^#\S*' , clean_lines . elided [ begin_line_number + 1 ] ) ) : begin_line_number = begin_line_number + 1 begin_line = clean_lines . elided [ begin_line_number ] if search ( r'.*{(.*?\\)?$' , begin_line ) : has_braces = True next_line = clean_lines . elided [ begin_line_number + 1 ] after_next_line = clean_lines . elided [ begin_line_number + 2 ] control_indent = search ( r'^(?P<indentation>\s*).*' , line ) . group ( 'indentation' ) next_line_indent = search ( r'^(?P<indentation>\s*).*' , next_line ) . group ( 'indentation' ) after_next_line_indent = search ( r'^(?P<indentation>\s*).*' , after_next_line ) . group ( 'indentation' ) if ( after_next_line != '' and not has_braces and control_indent < next_line_indent and control_indent < after_next_line_indent ) : error ( line_number , 'whitespace/braces' , 4 , 'Multi line control clauses should use braces.' ) # 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. previous_line_number = line_number while True : ( previous_line , previous_line_number ) = get_previous_non_blank_line ( clean_lines , previous_line_number ) if match ( r'\s+{.*}\s*;' , line ) and not previous_line . count ( ';' ) : line = previous_line + line else : break if ( search ( r'{.*}\s*;' , line ) and line . count ( '{' ) == line . count ( '}' ) and not search ( r'struct|class|enum|\s*=\s*{' , line ) ) : error ( line_number , 'readability/braces' , 4 , "You don't need a ; after a }" )