DANGLING_POINTERS_FROM_LOCALS in rustc_lint::dangling - Rust
Description: The `dangling_pointers_from_locals` lint detects getting a pointer to data of a local that will be dropped at the end of the function.

Skip to main content

## DANGLING_POINTERS_FROM_LOCALS

# Static DANGLING_POINTERS_FROM_LOCALS

Source

    pub static DANGLING_POINTERS_FROM_LOCALS: &Lint

Expand description

The `dangling_pointers_from_locals` lint detects getting a pointer to data of a local that will be dropped at the end of the function.

#### §Example

    fn f() -> *const u8 {
        let x = 0;
        &x // returns a dangling ptr to `x`
    }

{{produces}}

#### §Explanation

Returning a pointer from a local value will not prolong its lifetime, which means that the value can be dropped and the allocation freed while the pointer still exists, making the pointer dangling. This is not an error (as far as the type system is concerned) but probably is not what the user intended either.

If you need stronger guarantees, consider using references instead, as they are statically verified by the borrow-checker to never dangle.