You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There may be important temporary values in the if/while expression, like a mutex lock guard. In case of an if-statement-with-break, these are dropped after the statement. In case of a while-let loop, these are retained for the whole loop body.
I would expect that the lint might recognise this kind of thing and not warn. Seems hard though. But blindly "fixing" can lead to bad errors.
Lint Name
while_let_loop
Reproducer
I tried this code:
use std::sync::{Arc,Mutex};fnhandle_items(q:Arc<Mutex<Vec<i32>>>){loop{let item = match q.lock().unwrap().pop(){Some(item) => item,None => break,};// lock guard dropped hereprintln!("process: {}", item);if item > 10{// deadlocks here if while-let
q.lock().unwrap().insert(0, item - 1);}}}fnmain(){let q = Arc::new(Mutex::new(vec![1, 2, 3, 12, 7]));handle_items(q);}
I saw this happen:
warning: this loop could be written as a `while let` loop
--> src/main.rs:4:2
|
4 | / loop {
5 | | let item = match q.lock().unwrap().pop() {
6 | | Some(item) => item,
7 | | None => break,
... |
14 | | }
15 | | }
| |_____^ help: try: `while let Some(item) = q.lock().unwrap().pop() { .. }`
|
= note: `#[warn(clippy::while_let_loop)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_let_loop
Summary
There may be important temporary values in the if/while expression, like a mutex lock guard. In case of an if-statement-with-break, these are dropped after the statement. In case of a while-let loop, these are retained for the whole loop body.
I would expect that the lint might recognise this kind of thing and not warn. Seems hard though. But blindly "fixing" can lead to bad errors.
Lint Name
while_let_loop
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
Nothing. The suggested code will deadlock.
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: