Skip to content

unused_assignments lint has trouble tracking that one match guard observes assignment from another match guard. #138069

Closed

Description

@pnkfelix

I tried this code (playground):

fn pnk(x: usize) -> &'static str {
    let mut k1 = "k1";
    let mut h1 = "h1";
    match x & 3 {
        3 if { k1 = "unused?"; false } => (),
        _ if { h1 = k1; true } => (),
        _ => (),
    }
    h1
}

fn main() {
    dbg!(pnk(3));
}

I expected to see this happen: No lint complaints about the assignment to k1 in the first match guard being "unused", since the assignment is subsequently observed by the second match guard.

Instead, this happened: The unused_assignments lint has a false positive on the code above:

warning: value assigned to `k1` is never read
 --> src/main.rs:5:16
  |
5 |         3 if { k1 = "unused?"; false } => (),
  |                ^^
  |
  = help: maybe it is overwritten before being read?
  = note: `#[warn(unused_assignments)]` on by default

warning: `playground` (bin "playground") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.89s
     Running `target/debug/playground`
[src/main.rs:13:5] pnk(3) = "unused?"

Meta

I'm testing on the playground, Rust stable 1.85.0

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Mar 5, 2025
  2. pnkfelix commented on Mar 5, 2025

    @pnkfelix
    ContributorAuthor

    Fun fact: This problem seems like it has been present for the entirety of Rust's support for assignments in pattern guards: I see the same false positive from the lint in Rust 1.39. (And in Rust 1.38, the code is rejected because at that time, assignments in pattern guards were illegal).

  3. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    L-false-positiveLint: False positive (should not have fired).
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-patternsRelating to patterns and pattern matching
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Mar 5, 2025
  4. compiler-errors commented on Mar 5, 2025

    @compiler-errors
    Contributor

    The liveness analysis that powers the unused assignments lint is pretty brittle. Ideally we'd reimplement it on the MIR with a real cfg-aware analysis so that we don't have disagreements between MIR's "uninitialized assignments" and this.

  5. cjgillot commented on Oct 13, 2025

    @cjgillot
    Contributor

    Fixed by #142390

  6. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Oct 13, 2025
  7. KaiTomotake commented on Jan 18, 2026

    @KaiTomotake
    Contributor

    @rustbot claim

  8. added 2 commits that reference this issue on Jan 18, 2026
    926bab3
    5be42ca
  9. added a commit that references this issue on Jan 19, 2026
    3f01ca0
  10. added a commit that references this issue on Jan 19, 2026
    dc6e6fe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.L-false-positiveLint: False positive (should not have fired).L-unused_assignmentsLint: unused_assignmentsT-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