Open
Description
Code
pub fn foo() -> *const i32 {
let x = 42;
&x
}
Current output
rustc doesn't currently warn about this.
Desired output
A warning that this pointer is referencing a local variable and is going to be dangling immediately. E.g.
error: returning a pointer to stack memory associated with a local variable
--> <source>:12:5
|
LL| &x
| ^^
Rationale and extra context
This code is very unlikely to be correct, which is exactly why a warning should be emitted. This would be similar to Clang's -Wreturn-stack-address
warning.
In case of returning a reference, rustc would catch this and emit a compiler error. While it is fine to create such a pointer, it is very unlikely that the code should be written that way.
Rust Version
rustc 1.84.0-nightly (b8c8287a2 2024-11-03)
binary: rustc
commit-hash: b8c8287a229cd79604aa84c25e1235fc78cd5f2e
commit-date: 2024-11-03
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.3
Anything else?
If this becomes a Warn
lint it will start warning immediately and potentially break users that configured warnings as errors. So maybe this should be Allow
for the beginning?