Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ lint_confusable_identifier_pair = found both `{$existing_sym}` and `{$sym}` as i
.current_use = this identifier can be confused with `{$existing_sym}`
.other_use = other identifier used here

lint_dangling_pointers_from_locals = a dangling pointer will be produced because the local variable `{$local_var_name}` will be dropped
.ret_ty = return type of the {$fn_kind} is `{$ret_ty}`
.local_var = `{$local_var_name}` is part the {$fn_kind} and will be dropped at the end of the {$fn_kind}
lint_dangling_pointers_from_locals = {$fn_kind} returns a dangling pointer to dropped local variable `{$local_var_name}`
.ret_ty = return type is `{$ret_ty}`
.local_var = local variable `{$local_var_name}` is dropped at the end of the {$fn_kind}
.created_at = dangling pointer created here
.note = pointers do not have a lifetime; after returning, the `{$local_var_ty}` will be deallocated at the end of the {$fn_kind} because nothing is referencing it as far as the type system is concerned
.note = a dangling pointer is safe, but dereferencing one is undefined behavior

lint_dangling_pointers_from_temporaries = a dangling pointer will be produced because the temporary `{$ty}` will be dropped
.label_ptr = this pointer will immediately be invalid
Expand Down
38 changes: 19 additions & 19 deletions tests/ui/lint/dangling-pointers-from-locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,54 @@ const X: u8 = 5;
fn simple() -> *const u8 {
let x = 0;
&x
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn bindings() -> *const u8 {
let x = 0;
let x = &x;
x
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn bindings_with_return() -> *const u8 {
let x = 42;
let y = &x;
return y;
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn with_simple_cast() -> *const u8 {
let x = 0u8;
&x as *const u8
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn bindings_and_casts() -> *const u8 {
let x = 0u8;
let x = &x as *const u8;
x as *const u8
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn return_with_complex_cast() -> *mut u8 {
let mut x = 0u8;
return &mut x as *mut u8 as *const u8 as *mut u8;
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn with_block() -> *const u8 {
let x = 0;
&{ x }
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn with_many_blocks() -> *const u8 {
let x = 0;
{
{
&{
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
{ x }
}
}
Expand All @@ -65,41 +65,41 @@ fn with_many_blocks() -> *const u8 {
fn simple_return() -> *const u8 {
let x = 0;
return &x;
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn return_mut() -> *mut u8 {
let mut x = 0;
return &mut x;
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn const_and_flow() -> *const u8 {
if false {
let x = 8;
return &x;
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}
&X // not dangling
}

fn vector<T: Default>() -> *const Vec<T> {
let x = vec![T::default()];
&x
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn local_adt() -> *const Adt {
let x = Adt(5);
return &x;
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn closure() -> *const u8 {
let _x = || -> *const u8 {
let x = 8;
return &x;
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
};
&X // not dangling
}
Expand All @@ -111,27 +111,27 @@ fn fn_ptr() -> *const fn() -> u8 {

let x = ret_u8 as fn() -> u8;
&x
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn as_arg(a: Adt) -> *const Adt {
&a
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn fn_ptr_as_arg(a: fn() -> u8) -> *const fn() -> u8 {
&a
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn ptr_as_arg(a: *const Adt) -> *const *const Adt {
&a
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn adt_as_arg(a: &Adt) -> *const &Adt {
&a
//~^ WARN a dangling pointer will be produced
//~^ WARN dangling pointer
}

fn unit() -> *const () {
Expand Down
Loading
Loading