Skip to content

False clippy::while_let_loop when doing loop { if let } in spawned thread. #7226

Closed
@KSXGitHub

Description

@KSXGitHub

Lint name: while_let_loop

I tried this code:

(playground)

use std::{
    sync::{Arc, RwLock},
    thread::{sleep, spawn, JoinHandle},
    time::Duration,
};

// NOTE: the following function is a simplified version of the true function whose signature is generic.
pub fn spawn_observer(observed: Arc<RwLock<Option<i32>>>, observer: fn(i32), interval: Duration) -> JoinHandle<()> {
    spawn(move || loop {
        if let Some(value) = *observed.read().unwrap() {
            observer(value);
        } else {
            break;
        }
        sleep(interval);
    })
}

I expected to see this happen: No warnings.

Instead, this happened: It suggests while let loop, which would cause observer to lock the observed, blocking the other threads that also read/mutate the observed.

Meta

  • cargo clippy -V: clippy 0.1.51 (2fd73fa 2021-03-23)
  • rustc -Vv:
    rustc 1.51.0 (2fd73fabe 2021-03-23)
    binary: rustc
    commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
    commit-date: 2021-03-23
    host: x86_64-unknown-linux-gnu
    release: 1.51.0
    LLVM version: 11.0.1
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions