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)
| 5156 | |
| 5157 | |
| 5158 | def CheckEmptyBlockBody(filename, clean_lines, linenum, error): |
| 5159 | """Look for empty loop/conditional body with only a single semicolon. |
| 5160 | |
| 5161 | Args: |
| 5162 | filename: The name of the current file. |
| 5163 | clean_lines: A CleansedLines instance containing the file. |
| 5164 | linenum: The number of the line to check. |
| 5165 | error: The function to call with any errors found. |
| 5166 | """ |
| 5167 | |
| 5168 | # Search for loop keywords at the beginning of the line. Because only |
| 5169 | # whitespaces are allowed before the keywords, this will also ignore most |
| 5170 | # do-while-loops, since those lines should start with closing brace. |
| 5171 | # |
| 5172 | # We also check "if" blocks here, since an empty conditional block |
| 5173 | # is likely an error. |
| 5174 | line = clean_lines.elided[linenum] |
| 5175 | if matched := re.match(r"\s*(for|while|if)\s*\(", line): |
| 5176 | # Find the end of the conditional expression. |
| 5177 | (end_line, end_linenum, end_pos) = CloseExpression(clean_lines, linenum, line.find("(")) |
| 5178 | |
| 5179 | # Output warning if what follows the condition expression is a semicolon. |
| 5180 | # No warning for all other cases, including whitespace or newline, since we |
| 5181 | # have a separate check for semicolons preceded by whitespace. |
| 5182 | if end_pos >= 0 and re.match(r";", end_line[end_pos:]): |
| 5183 | if matched.group(1) == "if": |
| 5184 | error( |
| 5185 | filename, |
| 5186 | end_linenum, |
| 5187 | "whitespace/empty_conditional_body", |
| 5188 | 5, |
| 5189 | "Empty conditional bodies should use {}", |
| 5190 | ) |
| 5191 | else: |
| 5192 | error( |
| 5193 | filename, |
| 5194 | end_linenum, |
| 5195 | "whitespace/empty_loop_body", |
| 5196 | 5, |
| 5197 | "Empty loop bodies should use {} or continue", |
| 5198 | ) |
| 5199 | |
| 5200 | # Check for if statements that have completely empty bodies (no comments) |
| 5201 | # and no else clauses. |
| 5202 | if end_pos >= 0 and matched.group(1) == "if": |
| 5203 | # Find the position of the opening { for the if statement. |
| 5204 | # Return without logging an error if it has no brackets. |
| 5205 | opening_linenum = end_linenum |
| 5206 | opening_line_fragment = end_line[end_pos:] |
| 5207 | # Loop until EOF or find anything that's not whitespace or opening {. |
| 5208 | while not re.search(r"^\s*\{", opening_line_fragment): |
| 5209 | if re.search(r"^(?!\s*$)", opening_line_fragment): |
| 5210 | # Conditional has no brackets. |
| 5211 | return |
| 5212 | opening_linenum += 1 |
| 5213 | if opening_linenum == len(clean_lines.elided): |
| 5214 | # Couldn't find conditional's opening { or any code before EOF. |
| 5215 | return |
no test coverage detected
searching dependent graphs…