Skip to content

Commit 9206ba5

Browse files
authored
Rollup merge of #132983 - Anthony-Eid:dangling-pointers-lint, r=Urgau
Edit dangling pointers Closes: #132283
2 parents dee7d0e + 12214db commit 9206ba5

File tree

10 files changed

+86
-2
lines changed

10 files changed

+86
-2
lines changed

compiler/rustc_lint/messages.ftl

+3-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ lint_dangling_pointers_from_temporaries = a dangling pointer will be produced be
209209
.label_ptr = this pointer will immediately be invalid
210210
.label_temporary = this `{$ty}` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
211211
.note = pointers do not have a lifetime; when calling `{$callee}` the `{$ty}` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
212-
.help = for more information, see <https://doc.rust-lang.org/reference/destructors.html>
212+
.help_bind = you must make sure that the variable you bind the `{$ty}` to lives at least as long as the pointer returned by the call to `{$callee}`
213+
.help_returned = in particular, if this pointer is returned from the current function, binding the `{$ty}` inside the function will not suffice
214+
.help_visit = for more information, see <https://doc.rust-lang.org/reference/destructors.html>
213215
214216
lint_default_hash_types = prefer `{$preferred}` over `{$used}`, it has better performance
215217
.note = a `use rustc_data_structures::fx::{$preferred}` may be necessary

compiler/rustc_lint/src/lints.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,9 @@ pub(crate) struct IgnoredUnlessCrateSpecified<'a> {
11391139
#[derive(LintDiagnostic)]
11401140
#[diag(lint_dangling_pointers_from_temporaries)]
11411141
#[note]
1142-
#[help]
1142+
#[help(lint_help_bind)]
1143+
#[help(lint_help_returned)]
1144+
#[help(lint_help_visit)]
11431145
// FIXME: put #[primary_span] on `ptr_span` once it does not cause conflicts
11441146
pub(crate) struct DanglingPointersFromTemporaries<'tcx> {
11451147
pub callee: Symbol,

tests/ui/lint/dangling-pointers-from-temporaries/allow.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LL | dbg!(String::new().as_ptr());
77
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
88
|
99
= note: pointers do not have a lifetime; when calling `as_ptr` the `String` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
10+
= help: you must make sure that the variable you bind the `String` to lives at least as long as the pointer returned by the call to `as_ptr`
11+
= help: in particular, if this pointer is returned from the current function, binding the `String` inside the function will not suffice
1012
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
1113
note: the lint level is defined here
1214
--> $DIR/allow.rs:7:12
@@ -23,6 +25,8 @@ LL | dbg!(String::new().as_ptr());
2325
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
2426
|
2527
= note: pointers do not have a lifetime; when calling `as_ptr` the `String` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
28+
= help: you must make sure that the variable you bind the `String` to lives at least as long as the pointer returned by the call to `as_ptr`
29+
= help: in particular, if this pointer is returned from the current function, binding the `String` inside the function will not suffice
2630
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
2731
note: the lint level is defined here
2832
--> $DIR/allow.rs:18:12

tests/ui/lint/dangling-pointers-from-temporaries/calls.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LL | let ptr = cstring().as_ptr();
77
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
88
|
99
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
10+
= help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
11+
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
1012
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
1113
note: the lint level is defined here
1214
--> $DIR/calls.rs:1:9
@@ -23,6 +25,8 @@ LL | let ptr = cstring().as_ptr();
2325
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
2426
|
2527
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
28+
= help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
29+
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
2630
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
2731

2832
error: a dangling pointer will be produced because the temporary `CString` will be dropped
@@ -34,6 +38,8 @@ LL | let _ptr: *const u8 = cstring().as_ptr().cast();
3438
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
3539
|
3640
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
41+
= help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
42+
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
3743
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
3844

3945
error: a dangling pointer will be produced because the temporary `CString` will be dropped
@@ -45,6 +51,8 @@ LL | let _ptr: *const u8 = { cstring() }.as_ptr().cast();
4551
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
4652
|
4753
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
54+
= help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
55+
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
4856
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
4957

5058
error: a dangling pointer will be produced because the temporary `CString` will be dropped
@@ -56,6 +64,8 @@ LL | let _ptr: *const u8 = { cstring().as_ptr() }.cast();
5664
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
5765
|
5866
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
67+
= help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
68+
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
5969
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
6070

6171
error: aborting due to 5 previous errors

tests/ui/lint/dangling-pointers-from-temporaries/cstring-as-ptr.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ LL | let s = CString::new("some text").unwrap().as_ptr();
1515
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
1616
|
1717
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
18+
= help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
19+
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
1820
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
1921
note: the lint level is defined here
2022
--> $DIR/cstring-as-ptr.rs:2:9
@@ -34,6 +36,8 @@ LL | mymacro!();
3436
| ---------- in this macro invocation
3537
|
3638
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
39+
= help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
40+
= help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
3741
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
3842
= note: this error originates in the macro `mymacro` (in Nightly builds, run with -Z macro-backtrace for more info)
3943

tests/ui/lint/dangling-pointers-from-temporaries/example-from-issue123613.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LL | let str1 = String::with_capacity(MAX_PATH).as_mut_ptr();
77
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
88
|
99
= note: pointers do not have a lifetime; when calling `as_mut_ptr` the `String` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
10+
= help: you must make sure that the variable you bind the `String` to lives at least as long as the pointer returned by the call to `as_mut_ptr`
11+
= help: in particular, if this pointer is returned from the current function, binding the `String` inside the function will not suffice
1012
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
1113
note: the lint level is defined here
1214
--> $DIR/example-from-issue123613.rs:1:9
@@ -23,6 +25,8 @@ LL | let str2 = String::from("TotototototototototototototototototoT").as_ptr
2325
| this `String` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
2426
|
2527
= note: pointers do not have a lifetime; when calling `as_ptr` the `String` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
28+
= help: you must make sure that the variable you bind the `String` to lives at least as long as the pointer returned by the call to `as_ptr`
29+
= help: in particular, if this pointer is returned from the current function, binding the `String` inside the function will not suffice
2630
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
2731

2832
error: aborting due to 2 previous errors

tests/ui/lint/dangling-pointers-from-temporaries/ext.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LL | let _ptr1 = Vec::<u32>::new().as_ptr().dbg();
77
| this `Vec<u32>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
88
|
99
= note: pointers do not have a lifetime; when calling `as_ptr` the `Vec<u32>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
10+
= help: you must make sure that the variable you bind the `Vec<u32>` to lives at least as long as the pointer returned by the call to `as_ptr`
11+
= help: in particular, if this pointer is returned from the current function, binding the `Vec<u32>` inside the function will not suffice
1012
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
1113
note: the lint level is defined here
1214
--> $DIR/ext.rs:1:9
@@ -23,6 +25,8 @@ LL | let _ptr2 = vec![0].as_ptr().foo();
2325
| this `Vec<u32>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
2426
|
2527
= note: pointers do not have a lifetime; when calling `as_ptr` the `Vec<u32>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
28+
= help: you must make sure that the variable you bind the `Vec<u32>` to lives at least as long as the pointer returned by the call to `as_ptr`
29+
= help: in particular, if this pointer is returned from the current function, binding the `Vec<u32>` inside the function will not suffice
2630
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
2731

2832
error: aborting due to 2 previous errors

tests/ui/lint/dangling-pointers-from-temporaries/methods.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LL | vec![0u8].as_ptr();
77
| this `Vec<u8>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
88
|
99
= note: pointers do not have a lifetime; when calling `as_ptr` the `Vec<u8>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
10+
= help: you must make sure that the variable you bind the `Vec<u8>` to lives at least as long as the pointer returned by the call to `as_ptr`
11+
= help: in particular, if this pointer is returned from the current function, binding the `Vec<u8>` inside the function will not suffice
1012
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
1113
note: the lint level is defined here
1214
--> $DIR/methods.rs:1:9
@@ -23,6 +25,8 @@ LL | vec![0u8].as_mut_ptr();
2325
| this `Vec<u8>` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
2426
|
2527
= note: pointers do not have a lifetime; when calling `as_mut_ptr` the `Vec<u8>` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
28+
= help: you must make sure that the variable you bind the `Vec<u8>` to lives at least as long as the pointer returned by the call to `as_mut_ptr`
29+
= help: in particular, if this pointer is returned from the current function, binding the `Vec<u8>` inside the function will not suffice
2630
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
2731

2832
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)