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)
| 5088 | |
| 5089 | |
| 5090 | def CheckEmptyBlockBody(filename, clean_lines, linenum, error): |
| 5091 | """Look for empty loop/conditional body with only a single semicolon. |
| 5092 | |
| 5093 | Args: |
| 5094 | filename: The name of the current file. |
| 5095 | clean_lines: A CleansedLines instance containing the file. |
| 5096 | linenum: The number of the line to check. |
| 5097 | error: The function to call with any errors found. |
| 5098 | """ |
| 5099 | |
| 5100 | # Search for loop keywords at the beginning of the line. Because only |
| 5101 | # whitespaces are allowed before the keywords, this will also ignore most |
| 5102 | # do-while-loops, since those lines should start with closing brace. |
| 5103 | # |
| 5104 | # We also check "if" blocks here, since an empty conditional block |
| 5105 | # is likely an error. |
| 5106 | line = clean_lines.elided[linenum] |
| 5107 | if matched := re.match(r"\s*(for|while|if)\s*\(", line): |
| 5108 | # Find the end of the conditional expression. |
| 5109 | (end_line, end_linenum, end_pos) = CloseExpression(clean_lines, linenum, line.find("(")) |
| 5110 | |
| 5111 | # Output warning if what follows the condition expression is a semicolon. |
| 5112 | # No warning for all other cases, including whitespace or newline, since we |
| 5113 | # have a separate check for semicolons preceded by whitespace. |
| 5114 | if end_pos >= 0 and re.match(r";", end_line[end_pos:]): |
| 5115 | if matched.group(1) == "if": |
| 5116 | error( |
| 5117 | filename, |
| 5118 | end_linenum, |
| 5119 | "whitespace/empty_conditional_body", |
| 5120 | 5, |
| 5121 | "Empty conditional bodies should use {}", |
| 5122 | ) |
| 5123 | else: |
| 5124 | error( |
| 5125 | filename, |
| 5126 | end_linenum, |
| 5127 | "whitespace/empty_loop_body", |
| 5128 | 5, |
| 5129 | "Empty loop bodies should use {} or continue", |
| 5130 | ) |
| 5131 | |
| 5132 | # Check for if statements that have completely empty bodies (no comments) |
| 5133 | # and no else clauses. |
| 5134 | if end_pos >= 0 and matched.group(1) == "if": |
| 5135 | # Find the position of the opening { for the if statement. |
| 5136 | # Return without logging an error if it has no brackets. |
| 5137 | opening_linenum = end_linenum |
| 5138 | opening_line_fragment = end_line[end_pos:] |
| 5139 | # Loop until EOF or find anything that's not whitespace or opening {. |
| 5140 | while not re.search(r"^\s*\{", opening_line_fragment): |
| 5141 | if re.search(r"^(?!\s*$)", opening_line_fragment): |
| 5142 | # Conditional has no brackets. |
| 5143 | return |
| 5144 | opening_linenum += 1 |
| 5145 | if opening_linenum == len(clean_lines.elided): |
| 5146 | # Couldn't find conditional's opening { or any code before EOF. |
| 5147 | return |
no test coverage detected