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

Function CheckEmptyBlockBody

analytical_engine/misc/cpplint.py:4501–4602  ·  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

4499
4500
4501def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
4502 """Look for empty loop/conditional body with only a single semicolon.
4503
4504 Args:
4505 filename: The name of the current file.
4506 clean_lines: A CleansedLines instance containing the file.
4507 linenum: The number of the line to check.
4508 error: The function to call with any errors found.
4509 """
4510
4511 # Search for loop keywords at the beginning of the line. Because only
4512 # whitespaces are allowed before the keywords, this will also ignore most
4513 # do-while-loops, since those lines should start with closing brace.
4514 #
4515 # We also check "if" blocks here, since an empty conditional block
4516 # is likely an error.
4517 line = clean_lines.elided[linenum]
4518 matched = Match(r'\s*(for|while|if)\s*\(', line)
4519 if matched:
4520 # Find the end of the conditional expression.
4521 (end_line, end_linenum, end_pos) = CloseExpression(
4522 clean_lines, linenum, line.find('('))
4523
4524 # Output warning if what follows the condition expression is a semicolon.
4525 # No warning for all other cases, including whitespace or newline, since we
4526 # have a separate check for semicolons preceded by whitespace.
4527 if end_pos >= 0 and Match(r';', end_line[end_pos:]):
4528 if matched.group(1) == 'if':
4529 error(filename, end_linenum, 'whitespace/empty_conditional_body', 5,
4530 'Empty conditional bodies should use {}')
4531 else:
4532 error(filename, end_linenum, 'whitespace/empty_loop_body', 5,
4533 'Empty loop bodies should use {} or continue')
4534
4535 # Check for if statements that have completely empty bodies (no comments)
4536 # and no else clauses.
4537 if end_pos >= 0 and matched.group(1) == 'if':
4538 # Find the position of the opening { for the if statement.
4539 # Return without logging an error if it has no brackets.
4540 opening_linenum = end_linenum
4541 opening_line_fragment = end_line[end_pos:]
4542 # Loop until EOF or find anything that's not whitespace or opening {.
4543 while not Search(r'^\s*\{', opening_line_fragment):
4544 if Search(r'^(?!\s*$)', opening_line_fragment):
4545 # Conditional has no brackets.
4546 return
4547 opening_linenum += 1
4548 if opening_linenum == len(clean_lines.elided):
4549 # Couldn't find conditional's opening { or any code before EOF.
4550 return
4551 opening_line_fragment = clean_lines.elided[opening_linenum]
4552 # Set opening_line (opening_line_fragment may not be entire opening line).
4553 opening_line = clean_lines.elided[opening_linenum]
4554
4555 # Find the position of the closing }.
4556 opening_pos = opening_line_fragment.find('{')
4557 if opening_linenum == end_linenum:
4558 # We need to make opening_pos relative to the start of the entire line.

Callers 1

CheckStyleFunction · 0.85

Calls 9

CloseExpressionFunction · 0.85
SearchFunction · 0.85
CleanseCommentsFunction · 0.85
findMethod · 0.80
MatchFunction · 0.70
appendMethod · 0.65
groupMethod · 0.45
extendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected