Skip to content

let_underscore_* lints usage in place expression contexts #164746

Description

@theemathas

This was discovered by @traviscross at #164414 (comment)

The let_underscore_lock lint, the let_underscore_drop lint, and the upcoming let_underscore_drop_guard lint all detect when a pattern (or assignee expression) written as _ is matched against some relevant types. The lints assume that when such a thing happens, a value of the relevant type is dropped. This assumption is incorrect when the pattern is matched against a place expression. For example:

use std::sync::Mutex;
pub fn foo(x: Mutex<i32>) {
    let y = x.lock().unwrap();
    let _ = y;
}
error: non-binding let on a synchronization lock
 --> src/lib.rs:4:9
  |
4 |     let _ = y;
  |         ^ 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
  |
4 |     let _unused = y;
  |          ++++++
help: consider immediately dropping the value
  |
4 -     let _ = y;
4 +     drop(y);
  |

error: could not compile `playground` (lib) due to 1 previous error

The let _ = y; doesn't actually drop the y at all. The lint message is wrong.

Meta

Reproducible on the playground with rust version 1.99.1.

Activity

  1. 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.
    C-bugCategory: This is a bug.
    A-patternsRelating to patterns and pattern matching
    on Oct 22, 2026
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 22, 2026
  3. added
    D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.
    L-false-positiveLint: False positive (should not have fired).
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 22, 2026
  4. yashaggarwal85d commented on Oct 26, 2026

    @yashaggarwal85d
  5. yashaggarwal85d commented on Oct 26, 2026

    @yashaggarwal85d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.L-false-positiveLint: False positive (should not have fired).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