From 0bd6a03edf6d0f9957aa85625232c11bf49ee164 Mon Sep 17 00:00:00 2001 From: Marijn Schouten Date: Fri, 31 Oct 2025 21:41:12 +0000 Subject: [PATCH] dangling ptr lint cleanup --- compiler/rustc_lint/messages.ftl | 8 +- .../ui/lint/dangling-pointers-from-locals.rs | 38 ++--- .../lint/dangling-pointers-from-locals.stderr | 152 +++++++++--------- 3 files changed, 99 insertions(+), 99 deletions(-) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 1f6a382175b7c..8aa90c070acd3 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -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 diff --git a/tests/ui/lint/dangling-pointers-from-locals.rs b/tests/ui/lint/dangling-pointers-from-locals.rs index e321df2f42790..a274ab8582b93 100644 --- a/tests/ui/lint/dangling-pointers-from-locals.rs +++ b/tests/ui/lint/dangling-pointers-from-locals.rs @@ -8,46 +8,46 @@ 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 { @@ -55,7 +55,7 @@ fn with_many_blocks() -> *const u8 { { { &{ - //~^ WARN a dangling pointer will be produced + //~^ WARN dangling pointer { x } } } @@ -65,20 +65,20 @@ 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 } @@ -86,20 +86,20 @@ fn const_and_flow() -> *const u8 { fn vector() -> *const Vec { 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 } @@ -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 () { diff --git a/tests/ui/lint/dangling-pointers-from-locals.stderr b/tests/ui/lint/dangling-pointers-from-locals.stderr index e1d28bf22a0c1..45acc74ac34e5 100644 --- a/tests/ui/lint/dangling-pointers-from-locals.stderr +++ b/tests/ui/lint/dangling-pointers-from-locals.stderr @@ -1,105 +1,105 @@ -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:10:5 | LL | fn simple() -> *const u8 { - | --------- return type of the function is `*const u8` + | --------- return type is `*const u8` LL | let x = 0; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | &x | ^^ | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 = note: `#[warn(dangling_pointers_from_locals)]` on by default -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:17:5 | LL | fn bindings() -> *const u8 { - | --------- return type of the function is `*const u8` + | --------- return type is `*const u8` LL | let x = 0; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | let x = &x; | -- dangling pointer created here LL | x | ^ | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:24:12 | LL | fn bindings_with_return() -> *const u8 { - | --------- return type of the function is `*const u8` + | --------- return type is `*const u8` LL | let x = 42; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | let y = &x; | -- dangling pointer created here LL | return y; | ^ | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:30:5 | LL | fn with_simple_cast() -> *const u8 { - | --------- return type of the function is `*const u8` + | --------- return type is `*const u8` LL | let x = 0u8; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | &x as *const u8 | --^^^^^^^^^^^^^ | | | dangling pointer created here | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:37:5 | LL | fn bindings_and_casts() -> *const u8 { - | --------- return type of the function is `*const u8` + | --------- return type is `*const u8` LL | let x = 0u8; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | let x = &x as *const u8; | -- dangling pointer created here LL | x as *const u8 | ^^^^^^^^^^^^^^ | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:43:12 | LL | fn return_with_complex_cast() -> *mut u8 { - | ------- return type of the function is `*mut u8` + | ------- return type is `*mut u8` LL | let mut x = 0u8; - | ----- `x` is part the function and will be dropped at the end of the function + | ----- local variable `x` is dropped at the end of the function LL | return &mut x as *mut u8 as *const u8 as *mut u8; | ------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | dangling pointer created here | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:49:5 | LL | fn with_block() -> *const u8 { - | --------- return type of the function is `*const u8` + | --------- return type is `*const u8` LL | let x = 0; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | &{ x } | ^^^^^^ | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:57:13 | LL | fn with_many_blocks() -> *const u8 { - | --------- return type of the function is `*const u8` + | --------- return type is `*const u8` LL | let x = 0; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function ... LL | / &{ LL | | @@ -107,141 +107,141 @@ LL | | { x } LL | | } | |_____________^ | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:67:12 | LL | fn simple_return() -> *const u8 { - | --------- return type of the function is `*const u8` + | --------- return type is `*const u8` LL | let x = 0; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | return &x; | ^^ | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:73:12 | LL | fn return_mut() -> *mut u8 { - | ------- return type of the function is `*mut u8` + | ------- return type is `*mut u8` LL | let mut x = 0; - | ----- `x` is part the function and will be dropped at the end of the function + | ----- local variable `x` is dropped at the end of the function LL | return &mut x; | ^^^^^^ | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:80:16 | LL | fn const_and_flow() -> *const u8 { - | --------- return type of the function is `*const u8` + | --------- return type is `*const u8` LL | if false { LL | let x = 8; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | return &x; | ^^ | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:88:5 | LL | fn vector() -> *const Vec { - | ------------- return type of the function is `*const Vec` + | ------------- return type is `*const Vec` LL | let x = vec![T::default()]; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | &x | ^^ | - = note: pointers do not have a lifetime; after returning, the `Vec` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:94:12 | LL | fn local_adt() -> *const Adt { - | ---------- return type of the function is `*const Adt` + | ---------- return type is `*const Adt` LL | let x = Adt(5); - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | return &x; | ^^ | - = note: pointers do not have a lifetime; after returning, the `Adt` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: closure returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:101:16 | LL | let _x = || -> *const u8 { - | --------- return type of the closure is `*const u8` + | --------- return type is `*const u8` LL | let x = 8; - | - `x` is part the closure and will be dropped at the end of the closure + | - local variable `x` is dropped at the end of the closure LL | return &x; | ^^ | - = note: pointers do not have a lifetime; after returning, the `u8` will be deallocated at the end of the closure 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 -warning: a dangling pointer will be produced because the local variable `x` will be dropped +warning: function returns a dangling pointer to dropped local variable `x` --> $DIR/dangling-pointers-from-locals.rs:113:5 | LL | fn fn_ptr() -> *const fn() -> u8 { - | ----------------- return type of the function is `*const fn() -> u8` + | ----------------- return type is `*const fn() -> u8` ... LL | let x = ret_u8 as fn() -> u8; - | - `x` is part the function and will be dropped at the end of the function + | - local variable `x` is dropped at the end of the function LL | &x | ^^ | - = note: pointers do not have a lifetime; after returning, the `fn() -> u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `a` will be dropped +warning: function returns a dangling pointer to dropped local variable `a` --> $DIR/dangling-pointers-from-locals.rs:118:5 | LL | fn as_arg(a: Adt) -> *const Adt { - | - ---------- return type of the function is `*const Adt` + | - ---------- return type is `*const Adt` | | - | `a` is part the function and will be dropped at the end of the function + | local variable `a` is dropped at the end of the function LL | &a | ^^ | - = note: pointers do not have a lifetime; after returning, the `Adt` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `a` will be dropped +warning: function returns a dangling pointer to dropped local variable `a` --> $DIR/dangling-pointers-from-locals.rs:123:5 | LL | fn fn_ptr_as_arg(a: fn() -> u8) -> *const fn() -> u8 { - | - ----------------- return type of the function is `*const fn() -> u8` + | - ----------------- return type is `*const fn() -> u8` | | - | `a` is part the function and will be dropped at the end of the function + | local variable `a` is dropped at the end of the function LL | &a | ^^ | - = note: pointers do not have a lifetime; after returning, the `fn() -> u8` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `a` will be dropped +warning: function returns a dangling pointer to dropped local variable `a` --> $DIR/dangling-pointers-from-locals.rs:128:5 | LL | fn ptr_as_arg(a: *const Adt) -> *const *const Adt { - | - ----------------- return type of the function is `*const *const Adt` + | - ----------------- return type is `*const *const Adt` | | - | `a` is part the function and will be dropped at the end of the function + | local variable `a` is dropped at the end of the function LL | &a | ^^ | - = note: pointers do not have a lifetime; after returning, the `*const Adt` will be deallocated at the end of the function 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 -warning: a dangling pointer will be produced because the local variable `a` will be dropped +warning: function returns a dangling pointer to dropped local variable `a` --> $DIR/dangling-pointers-from-locals.rs:133:5 | LL | fn adt_as_arg(a: &Adt) -> *const &Adt { - | - ----------- return type of the function is `*const &Adt` + | - ----------- return type is `*const &Adt` | | - | `a` is part the function and will be dropped at the end of the function + | local variable `a` is dropped at the end of the function LL | &a | ^^ | - = note: pointers do not have a lifetime; after returning, the `&Adt` will be deallocated at the end of the function 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 warning: 19 warnings emitted