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