Skip to content

unused_assignments does not trigger on assignment through reference #144031

@briansmith

Description

@briansmith

Code

fn store32(output: &mut [u8; 16], i: usize, value: u32) {
    let output: &mut [u8; 4] = &mut output[i..][..4].try_into().unwrap();
    *output = u32::to_le_bytes(value);
}

fn main() {
    let mut r = [0u8; 16];
    store32(&mut r, 0, 0x12345678);
    println!("{r:?}")
}

Current output

None

Desired output

warning: value assigned to `output` is never read
note: `#[warn(unused_assignments)]` on by default

The fix is:

-    let output: &mut [u8; 4] = &mut output[i..][..4].try_into().unwrap();
+    let output: &mut [u8; 4] = (&mut output[i..][..4]).try_into().unwrap();

The compiler should have recognized that output in the buggy version is a reference to a temporary. The temporary is written (through output) but never read.

Rationale and extra context

The author of this code overlooked that .try_into().unwrap() binds tighter than &mut , so the store32 function is buggy: It doesn't write anything to output like it intends. It doesn't make sense to modify a temporary and then never read from it.

Other cases

Rust Version

% rustc --version --verbose
rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: aarch64-apple-darwin
release: 1.88.0
LLVM version: 20.1.5

Anything else?

See the thread https://rust-lang.zulipchat.com/#narrow/channel/257328-clippy/topic/Accidental.20borrow.20of.20temporary.2Faccidental.20lifetime.20extension/with/528940661

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