MCPcopy Create free account
hub / github.com/dmtcp/dmtcp / CheckEmptyBlockBody

Function CheckEmptyBlockBody

util/cpplint.py:3933–4034  ·  view source on GitHub ↗

Look for empty loop/conditional body with only a single semicolon. 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

3931
3932
3933def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
3934 """Look for empty loop/conditional body with only a single semicolon.
3935
3936 Args:
3937 filename: The name of the current file.
3938 clean_lines: A CleansedLines instance containing the file.
3939 linenum: The number of the line to check.
3940 error: The function to call with any errors found.
3941 """
3942
3943 # Search for loop keywords at the beginning of the line. Because only
3944 # whitespaces are allowed before the keywords, this will also ignore most
3945 # do-while-loops, since those lines should start with closing brace.
3946 #
3947 # We also check "if" blocks here, since an empty conditional block
3948 # is likely an error.
3949 line = clean_lines.elided[linenum]
3950 matched = Match(r'\s*(for|while|if)\s*\(', line)
3951 if matched:
3952 # Find the end of the conditional expression.
3953 (end_line, end_linenum, end_pos) = CloseExpression(
3954 clean_lines, linenum, line.find('('))
3955
3956 # Output warning if what follows the condition expression is a semicolon.
3957 # No warning for all other cases, including whitespace or newline, since we
3958 # have a separate check for semicolons preceded by whitespace.
3959 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
3960 if matched.group(1) == 'if':
3961 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
3962 'Empty conditional bodies should use {}')
3963 else:
3964 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
3965 'Empty loop bodies should use {} or continue')
3966
3967 # Check for if statements that have completely empty bodies (no comments)
3968 # and no else clauses.
3969 if end_pos >= 0 and matched.group(1) == 'if':
3970 # Find the position of the opening { for the if statement.
3971 # Return without logging an error if it has no brackets.
3972 opening_linenum = end_linenum
3973 opening_line_fragment = end_line[end_pos:]
3974 # Loop until EOF or find anything that's not whitespace or opening {.
3975 while not Search(r'^\s*\{', opening_line_fragment):
3976 if Search(r'^(?!\s*$)', opening_line_fragment):
3977 # Conditional has no brackets.
3978 return
3979 opening_linenum += 1
3980 if opening_linenum == len(clean_lines.elided):
3981 # Couldn't find conditional's opening { or any code before EOF.
3982 return
3983 opening_line_fragment = clean_lines.elided[opening_linenum]
3984 # Set opening_line (opening_line_fragment may not be entire opening line).
3985 opening_line = clean_lines.elided[opening_linenum]
3986
3987 # Find the position of the closing }.
3988 opening_pos = opening_line_fragment.find('{')
3989 if opening_linenum == end_linenum:
3990 # We need to make opening_pos relative to the start of the entire line.

Callers 1

CheckStyleFunction · 0.85

Calls 6

MatchFunction · 0.85
CloseExpressionFunction · 0.85
errorFunction · 0.85
SearchFunction · 0.85
CleanseCommentsFunction · 0.85
listClass · 0.85

Tested by

no test coverage detected