Skip to main content

RAW_BORROWS_VIA_REFERENCES

Static RAW_BORROWS_VIA_REFERENCES 

Source
pub static RAW_BORROWS_VIA_REFERENCES: &Lint
Expand description

The raw_borrows_via_references lint checks for references that decay immediately into raw borrows.

§Example

#![warn(raw_borrows_via_references)]

fn via_ref(x: *const (i32, i32)) -> *const i32 {
    unsafe { &(*x).0 as *const i32 }
}

fn main() {
    let x = (0, 1);
    let _r = via_ref(&x);
}

{{produces}}

§Explanation

Creating unnecessary references is discouraged because it makes code less explicit and can lead to undefined behavior. Creating a reference induces aliasing assumptions that the compiler relies on, so an otherwise-pointless reference can cause undefined behavior even when the reference is never read through. Avoiding them keeps the code more explicit and easier to reason about.

See the Reference for the full set of validity requirements that references must uphold.

This lint is “allow” by default because it will trigger for a large amount of existing Rust code. Eventually it is desired for this to become warn-by-default.