rust-lang / rust-lang/rust

`unused_assignments` does not trigger on assignment through reference

Open
#144,031 3 comments 1 reaction 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
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

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

Start by reproducing the unused_assignments behavior with the store32 example on Rust 1.88.0, then trace the compiler's handling of assignments through references to temporaries. Read the linked Zulip discussion for context. Done means the buggy form emits the requested warning while the corrected form remains valid.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.