DANGEROUS_IMPLICIT_AUTOREFS in rustc_lint::autorefs - Rust
Description: The `dangerous_implicit_autorefs` lint checks for implicitly taken references to dereferences of raw pointers.

Skip to main content

## DANGEROUS_IMPLICIT_AUTOREFS

# Static DANGEROUS_IMPLICIT_AUTOREFS

Source

    pub static DANGEROUS_IMPLICIT_AUTOREFS: &Lint

Expand description

The `dangerous_implicit_autorefs` lint checks for implicitly taken references to dereferences of raw pointers.

#### §Example

ⓘ

    unsafe fn fun(ptr: *mut [u8]) -> *mut [u8] {
        unsafe { &raw mut (*ptr)[..16] }
        //                      ^^^^^^ this calls `IndexMut::index_mut(&mut ..., ..16)`,
        //                             implicitly creating a reference
    }

{{produces}}

#### §Explanation

When working with raw pointers it’s usually undesirable to create references, since they inflict additional safety requirements. Unfortunately, it’s possible to take a reference to the dereference of a raw pointer implicitly, which inflicts the usual reference requirements.

If you are sure that you can soundly take a reference, then you can take it explicitly:

    unsafe fn fun(ptr: *mut [u8]) -> *mut [u8] {
        unsafe { &raw mut (&mut *ptr)[..16] }
    }

Otherwise try to find an alternative way to achieve your goals using only raw pointers:

    use std::ptr;

    fn fun(ptr: *mut [u8]) -> *mut [u8] {
        ptr::slice_from_raw_parts_mut(ptr.cast(), 16)
    }