pub static DANGLING_POINTERS_FROM_TEMPORARIES: &Lint
Expand description
The dangling_pointers_from_temporaries
lint detects getting a pointer to data
of a temporary that will immediately get dropped.
§Example
fn gather_and_use(bytes: impl Iterator<Item = u8>) {
let x: *const u8 = bytes.collect::<Vec<u8>>().as_ptr();
unsafe { use_data(x) }
}
{{produces}}
§Explanation
Getting a pointer from a temporary 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.