Skip to content

unused_must_use sometimes produces suggestions that then trigger let_underscore_lock #162843

Description

@RalfJung

Code

use std::sync::Mutex;

fn main() {
    let m = Mutex::new(0);

    m.lock().unwrap();
}

Current output

warning: unused `std::sync::MutexGuard` that must be used
 --> src/main.rs:6:5
  |
6 |     m.lock().unwrap();
  |     ^^^^^^^^^^^^^^^^^
  |
  = note: if unused the Mutex will immediately unlock
  = note: `#[warn(unused_must_use)]` (part of `#[warn(unused)]`) on by default
help: use `let _ = ...` to ignore the resulting value
  |
6 |     let _ = m.lock().unwrap();
  |     +++++++

Desired output

warning: unused `std::sync::MutexGuard` that must be used
 --> src/main.rs:6:5
  |
6 |     m.lock().unwrap();
  |     ^^^^^^^^^^^^^^^^^
  |
  = note: if unused the Mutex will immediately unlock
  = note: `#[warn(unused_must_use)]` (part of `#[warn(unused)]`) on by default
help: use `let _guard = ...` to ignore the resulting value while keeping the Mutex locked
  |
6 |     let _guard = m.lock().unwrap();
  |     ++++++++++++

Rationale and extra context

The suggestion we provide causes another lint to trigger:

error: non-binding let on a synchronization lock
 --> src/main.rs:6:9
  |
6 |     let _ = m.lock().unwrap();
  |         ^ this lock is not assigned to a binding and is immediately dropped
  |
  = note: `#[deny(let_underscore_lock)]` (part of `#[deny(let_underscore)]`) on by default
help: consider binding to an unused variable to avoid immediately dropping the value
  |
6 |     let _unused = m.lock().unwrap();
  |          ++++++
help: consider immediately dropping the value
  |
6 -     let _ = m.lock().unwrap();
6 +     drop(m.lock().unwrap());
  |

That's a bad user experience.

Other cases

Rust Version

current nightly

Anything else?

Cc @kpreid

No activity

Activity on this issue will appear here.

Activity

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-diagnosticsArea: Messages for errors, warnings, and lintsT-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