rust-lang / rust-lang/rust

`unused_assignments` when writing to variable and then reading from its raw pointer

Open
#150,656 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-lints L-unused_assignments T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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 emitted
Desired 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.3
Anything else?

No response

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 with the reproducer in src/main.rs and confirm the unused_assignments warning on the assignment followed by a raw-pointer read. Read the rustc handling of this lint and raw-pointer accesses, then verify that the reported examples no longer warn while ordinary unused assignments still do.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.