Skip to content

Unexpected warning about unused assignment in Rust 1.92 and newer #151514

@Tiwalun

Description

@Tiwalun

Code

I tried this code, which is using the docsplay crate:

#[derive(docsplay::Display)]
pub enum DocsplayError {
    /// Use field a: {a}, but not b.
    A { a: usize, b: usize },
    /// Generic error in a tuple
    B(usize),
}

I expected to see this happen:

This code should compile, and no warning should be shown.

Instead, this happened:

I get a warning about an unused assignment:

warning: value assigned to `b` is never read
 --> src/lib.rs:4:19
  |
4 |     A { a: usize, b: usize },
  |                   ^
  |
  = help: maybe it is overwritten before being read?
  = note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default

This is unexpected, because there is no visible assignment anywhere. The expanded code for the macro looks like this:

// Expanded version of above code

pub enum DocsplayErrorExpanded {
    /// Use field a: {a}, but not b.
    A { a: usize, b: usize },
    /// Generic error in a tuple
    B(usize),
}
impl ::core::fmt::Display for DocsplayErrorExpanded {
    fn fmt(&self, formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        #[allow(unused_imports)]
        use ::docsplay::{DisplayToDisplayDoc, PathToDisplayDoc};
        #[allow(unused_variables)]
        match self {
            Self::A { a, b } => formatter.write_fmt(format_args!(
                "Use field a: {0}, but not b.",
                (&(a)).__displaydoc_display(),
            )),
            Self::B(_0) => formatter.write_fmt(format_args!("Generic error in a tuple")),
        }
    }
}

There is #[allow(unused_variables)], which means that the unused field b does not trigger that warning, but it is unexpected that the unused_assignments warning is triggered.

Version it worked on

It most recently worked on: 1.91

Version with regression

rustc --version --verbose:

rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: x86_64-unknown-linux-gnu
release: 1.92.0
LLVM version: 21.1.3

Also seems to happen on the latest nighly,, rustc 1.95.0-nightly (eda76d9d1 2026-01-21).

Backtrace

Backtrace

<backtrace>

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.L-unused_assignmentsLint: unused_assignmentsP-highHigh priorityregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions