Skip to content

Different behavior of the let_underscore_lock lint in rustc compared to clippy #102771

Description

@kamulos

I tried this code with the new rustc lint:

fn main() {
    let _ = std::sync::Mutex::new(()).lock();
}

Clippy warns here, but rustc doesn't. This might be intentional, but I think this could be a source of mistakes...

The rustc lint is triggered, as soon as I use .lock().unwrap()

Meta

rustc --version --verbose:

rustc 1.66.0-nightly (0ca356586 2022-10-06)
binary: rustc
commit-hash: 0ca356586fed56002b10920fd21ddf6fb12de797
commit-date: 2022-10-06
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.2

Activity

  1. kadiwa4 commented on Mar 1, 2024

    @kadiwa4
    Contributor

    Fixed by #119710:

    fn main() {
        let _ = parking_lot::Mutex::new(()).lock();
        let _ = std::sync::Mutex::new(()).lock();
    }

    results in

    result
    error: non-binding `let` on a synchronization lock
     --> src/main.rs:2:5
      |
    2 |     let _ = parking_lot::Mutex::new(()).lock();
      |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |
      = help: consider using an underscore-prefixed named binding or dropping explicitly with `std::mem::drop`
      = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_underscore_lock
      = note: `#[deny(clippy::let_underscore_lock)]` on by default
    
    error: non-binding let on a synchronization lock
     --> src/main.rs:3:9
      |
    3 |     let _ = std::sync::Mutex::new(()).lock();
      |         ^ this lock is not assigned to a binding and is immediately dropped
      |
      = note: `#[deny(let_underscore_lock)]` on by default
    help: consider binding to an unused variable to avoid immediately dropping the value
      |
    3 |     let _unused = std::sync::Mutex::new(()).lock();
      |         ~~~~~~~
    help: consider immediately dropping the value
      |
    3 |     drop(std::sync::Mutex::new(()).lock());
      |     ~~~~~                                +
    
    error: could not compile `playground` (bin "playground") due to 2 previous errors
    

    Tested in tests/ui/lint/let_underscore/let_underscore_lock.rs

  2. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed on Apr 17, 2024
  3. fmease commented on Apr 17, 2024

    @fmease
    Member

    Closing as fixed.

  4. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    and removed
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    on Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions