Skip to main content

IMPLICIT_PROVENANCE_CASTS

Static IMPLICIT_PROVENANCE_CASTS 

Source
pub static IMPLICIT_PROVENANCE_CASTS: &Lint
Expand description

The implicit_provenance_casts lint detects integer-to-pointer and pointer-to-integer casts.

§Example

#![feature(strict_provenance_lints)]
#![warn(implicit_provenance_casts)]

fn main() {
    let x: u8 = 37;
    let addr: usize = &x as *const u8 as usize;
    let _ptr = addr as *const u8;
}

{{produces}}

§Explanation

This lint exists to help migrate code to Strict Provenance APIs where possible, and make remaining uses of Exposed Provenance explicit. For more information on pointer provenance, see the std::ptr documentation.

Earlier versions of Rust did not have a clear answer how integer-to-pointer and pointer-to-integer casts interact with provenance. Such casts are now defined to use the exposed provenance model, but in many cases the code can be updated to strict provenance APIs, which is preferable as it enables more precise reasoning about unsafe code, both by humans and by tools like Miri.

However, there are situations where exposed provenance is required or following the strict provenance model requires major refactorings. In those cases, it’s still useful to replace the as casts with equivalent explicit use of exposed provenance APIs and a comment explaining why they are needed.