Skip to content

Commit

Permalink
Add test case for false positive
Browse files Browse the repository at this point in the history
But this commit doesn't actually fix the problem.

The problem is that the try statements aren't handled yet and simple
continue with the next block, which in the case of a `while True` loop
creates an infinite loop. Thus the rule triggers on any statements after
the while loop, but this is incorrect.
  • Loading branch information
Thomas de Zeeuw committed Jun 27, 2023
1 parent aa36576 commit 936f428
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
21 changes: 21 additions & 0 deletions crates/ruff/resources/test/fixtures/control-flow-graph/while.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,24 @@ def func():
while True:
if True:
break

'''
TODO: because `try` statements aren't handled this triggers a false positive as
the last statement is reached, but the rules thinks it isn't (it doesn't
see/process the break statement).
# Test case found in the Bokeh repository that trigger a false positive.
def bokeh2(self, host: str = DEFAULT_HOST, port: int = DEFAULT_PORT) -> None:
self.stop_serving = False
while True:
try:
self.server = HTTPServer((host, port), HtmlOnlyHandler)
self.host = host
self.port = port
break
except OSError:
log.debug(f"port {port} is in use, trying to next one")
port += 1
self.thread = threading.Thread(target=self._run_web_server)
'''
21 changes: 21 additions & 0 deletions crates/ruff/resources/test/fixtures/ruff/RUF014.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,24 @@ def bokeh1(self, obj: BytesRep) -> bytes:
self.error(f"can't resolve buffer '{id}'")

return buffer.data

'''
TODO: because `try` statements aren't handled this triggers a false positive as
the last statement is reached, but the rules thinks it isn't (it doesn't
see/process the break statement).
# Test case found in the Bokeh repository that trigger a false positive.
def bokeh2(self, host: str = DEFAULT_HOST, port: int = DEFAULT_PORT) -> None:
self.stop_serving = False
while True:
try:
self.server = HTTPServer((host, port), HtmlOnlyHandler)
self.host = host
self.port = port
break
except OSError:
log.debug(f"port {port} is in use, trying to next one")
port += 1
self.thread = threading.Thread(target=self._run_web_server)
'''
7 changes: 6 additions & 1 deletion crates/ruff/src/rules/ruff/rules/unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,12 @@ mod tests {
.body;

for (i, stmts) in stmts.into_iter().enumerate() {
let func = stmts.function_def_stmt().expect("statement not a function");
let Some(func) = stmts.function_def_stmt() else {
// Shouldn't happen, but if it does it helps with debugging.
#[allow(clippy::print_stderr)]
eprintln!("unexpected statement kind, ignoring");
continue;
};

let got = BasicBlocks::from(&*func.body);
// Basic sanity checks.
Expand Down

0 comments on commit 936f428

Please sign in to comment.