Skip to content

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

@Giftzwerg02

Description

@Giftzwerg02

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.L-unused_assignmentsLint: unused_assignmentsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions