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