`unused_assignments` does not trigger on assignment through reference
Nobody has claimed this yet.
- 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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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