rust-lang / rust-lang/rust

unused_must_use sometimes produces suggestions that then trigger let_underscore_lock

Open
#162,843 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the provided Mutex example on current nightly and trace the lint implementation and its related tests for the unused_must_use suggestion. Done means the suggested binding preserves the lock and no longer triggers let_underscore_lock, with the expected diagnostic covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.