-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
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?
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.