-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Labels
A-lintArea: New lintsArea: New lints
Description
What it does
the borrow checker checks when a reference got out of its scope, but not for raw pointers, where thing might get weird.
for example:
fn local_ptr() -> *const i32 {
let temp: i32 = 123;
&temp as *const i32
}
fn main() {
let p = local_ptr();
unsafe {
println!("{}", *p);
}
}
The above code prints 0 (Because temp
was allocated in the stack???), run it in playground.
I don't think there was anyway to help people avoid such code, if there is, please let me know.
Advantage
- Help reducing undefined behaviors
Drawbacks
Might be hard to cover all the cases while not causing false positive results.
Example
fn local_ptr() -> *const i32 {
let temp: i32 = 123;
&temp as *const i32
}
Could be written as:
fn local_ptr() -> *const i32 {
static temp: i32 = 123;
&temp as *const i32
}
Metadata
Metadata
Assignees
Labels
A-lintArea: New lintsArea: New lints