-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.L-unused_assignmentsLint: unused_assignmentsLint: unused_assignmentsT-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
let mut a = 0;
let a_ptr = &mut a as *mut _;
a = 3; // <-- produces warning for this assignment
println!("a: {}", unsafe { *a_ptr });Current output
warning: value assigned to `a` is never read
--> src/main.rs:43:5
|
43 | a = 3;
| ^^^^^
|
= help: maybe it is overwritten before being read?
= note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default
warning: 1 warning emittedDesired output
Ideally no error at all (?)Rationale and extra context
I first got this warning when using the libc-crate, which requires passing raw pointers, e.g.:
fn main() {
use libc;
use std::time::Duration;
let timeout = Duration::from_secs(1);
let mut ts = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
let ts_ptr = &mut ts as *mut _;
unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, ts_ptr) };
// these writes cause a "value assigned is never read" warning
ts.tv_sec += timeout.as_secs() as i64;
ts.tv_nsec += timeout.subsec_nanos() as i64;
let mut rc = 0;
while /* exit-condition && */ rc == 0 {
// ts_ptr used again here
rc = unsafe { libc::pthread_cond_timedwait(todo!("cond"), todo!("lock"), ts_ptr) }
}
}Other cases
Rust Version
$ 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.3Anything else?
No response
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.L-unused_assignmentsLint: unused_assignmentsLint: unused_assignmentsT-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.