Skip to content

Commit 08f9947

Browse files
committed
instantly_dangling_pointer: rename to dangling_pointers_from_temporaries
1 parent f403c05 commit 08f9947

File tree

14 files changed

+106
-111
lines changed

14 files changed

+106
-111
lines changed

compiler/rustc_lint/src/dangling.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ declare_lint! {
3535
// FIXME: does not catch UnsafeCell::get
3636
// FIXME: does not catch getting a ref to a temporary and then converting it to a ptr
3737
declare_lint! {
38-
/// The `instantly_dangling_pointer` lint detects getting a pointer to data
38+
/// The `dangling_pointers_from_temporaries` lint detects getting a pointer to data
3939
/// of a temporary that will immediately get dropped.
4040
///
4141
/// ### Example
@@ -63,20 +63,12 @@ declare_lint! {
6363
///
6464
/// If you need stronger guarantees, consider using references instead,
6565
/// as they are statically verified by the borrow-checker to never dangle.
66-
///
67-
/// Note: This lint does **not** get triggered by methods & functions
68-
/// that intentionally produce dangling pointers, such as:
69-
///
70-
/// - `core::ptr::dangling` & `core::ptr::dangling_mut`
71-
/// - `core::ptr::NonNull::dangling`
72-
/// - `std::alloc::Layout::dangling`
73-
///
74-
pub INSTANTLY_DANGLING_POINTER,
66+
pub DANGLING_POINTERS_FROM_TEMPORARIES,
7567
Warn,
76-
"detects getting a pointer that will immediately dangle"
68+
"detects getting a pointer from a temporary"
7769
}
7870

79-
declare_lint_pass!(DanglingPointers => [TEMPORARY_CSTRING_AS_PTR, INSTANTLY_DANGLING_POINTER]);
71+
declare_lint_pass!(DanglingPointers => [TEMPORARY_CSTRING_AS_PTR, DANGLING_POINTERS_FROM_TEMPORARIES]);
8072

8173
impl<'tcx> LateLintPass<'tcx> for DanglingPointers {
8274
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
@@ -111,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for DanglingPointers {
111103
&& is_interesting(cx, ty)
112104
{
113105
cx.emit_span_lint(
114-
INSTANTLY_DANGLING_POINTER,
106+
DANGLING_POINTERS_FROM_TEMPORARIES,
115107
method.ident.span,
116108
InstantlyDangling {
117109
callee: method.ident.name,

library/alloc/tests/boxed.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ use core::ptr::NonNull;
55

66
#[test]
77
// FIXME(GrigorenkoPV)
8-
#[allow(unknown_lints, reason = "`instantly_dangling_pointer` does not exist at stage 0 yet")]
9-
#[allow(instantly_dangling_pointer)]
8+
#[allow(
9+
unknown_lints,
10+
reason = "`dangling_pointers_from_temporaries` does not exist at stage 0 yet"
11+
)]
12+
#[allow(dangling_pointers_from_temporaries)]
1013
fn uninitialized_zero_size_box() {
1114
assert_eq!(
1215
&*Box::<()>::new_uninit() as *const _,

tests/ui/consts/zst_no_llvm_alloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() {
1717

1818
// The exact addresses returned by these library functions are not necessarily stable guarantees
1919
// but for now we assert that we're still matching.
20-
#[expect(instantly_dangling_pointer)]
20+
#[expect(dangling_pointers_from_temporaries)]
2121
{
2222
assert_eq!(<Vec<i32>>::new().as_ptr(), <&[i32]>::default().as_ptr());
2323
assert_eq!(<Box<[i32]>>::default().as_ptr(), (&[]).as_ptr());

tests/ui/lint/dangling-ptr/instantly-dangling-pointer-allow.rs renamed to tests/ui/lint/dangling-ptr/allow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// This should not ICE.
44

5-
#![allow(instantly_dangling_pointer)]
5+
#![allow(dangling_pointers_from_temporaries)]
66

77
fn main() {
88
dbg!(String::new().as_ptr());

tests/ui/lint/dangling-ptr/instantly-dangling-pointer-methods.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/ui/lint/dangling-ptr/instantly-dangling-pointer-types.rs

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/ui/lint/dangling-ptr/instantly-dangling-pointer-issue123613.rs renamed to tests/ui/lint/dangling-ptr/issue123613.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#![deny(instantly_dangling_pointer)]
1+
#![deny(dangling_pointers_from_temporaries)]
22

33
const MAX_PATH: usize = 260;
44
fn main() {
55
let str1 = String::with_capacity(MAX_PATH).as_mut_ptr();
6-
//~^ ERROR [instantly_dangling_pointer]
6+
//~^ ERROR [dangling_pointers_from_temporaries]
77
let str2 = String::from("TotototototototototototototototototoT").as_ptr();
8-
//~^ ERROR [instantly_dangling_pointer]
8+
//~^ ERROR [dangling_pointers_from_temporaries]
99
unsafe {
1010
std::ptr::copy_nonoverlapping(str2, str1, 30);
1111
println!("{:?}", String::from_raw_parts(str1, 30, 30));

tests/ui/lint/dangling-ptr/instantly-dangling-pointer-issue123613.stderr renamed to tests/ui/lint/dangling-ptr/issue123613.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: getting a pointer from a temporary `String` will result in a dangling pointer
2-
--> $DIR/instantly-dangling-pointer-issue123613.rs:5:48
2+
--> $DIR/issue123613.rs:5:48
33
|
44
LL | let str1 = String::with_capacity(MAX_PATH).as_mut_ptr();
55
| ------------------------------- ^^^^^^^^^^ this pointer will immediately be invalid
@@ -9,13 +9,13 @@ LL | let str1 = String::with_capacity(MAX_PATH).as_mut_ptr();
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
1010
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
1111
note: the lint level is defined here
12-
--> $DIR/instantly-dangling-pointer-issue123613.rs:1:9
12+
--> $DIR/issue123613.rs:1:9
1313
|
14-
LL | #![deny(instantly_dangling_pointer)]
15-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
LL | #![deny(dangling_pointers_from_temporaries)]
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616

1717
error: getting a pointer from a temporary `String` will result in a dangling pointer
18-
--> $DIR/instantly-dangling-pointer-issue123613.rs:7:70
18+
--> $DIR/issue123613.rs:7:70
1919
|
2020
LL | let str2 = String::from("TotototototototototototototototototoT").as_ptr();
2121
| ----------------------------------------------------- ^^^^^^ this pointer will immediately be invalid

tests/ui/lint/dangling-ptr/methods.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![deny(dangling_pointers_from_temporaries)]
2+
3+
fn main() {
4+
vec![0u8].as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
5+
vec![0u8].as_mut_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
6+
}

tests/ui/lint/dangling-ptr/instantly-dangling-pointer-methods.stderr renamed to tests/ui/lint/dangling-ptr/methods.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: getting a pointer from a temporary `Vec<u8>` will result in a dangling pointer
2-
--> $DIR/instantly-dangling-pointer-methods.rs:4:15
2+
--> $DIR/methods.rs:4:15
33
|
44
LL | vec![0u8].as_ptr();
55
| --------- ^^^^^^ this pointer will immediately be invalid
@@ -9,13 +9,13 @@ LL | vec![0u8].as_ptr();
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
1010
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
1111
note: the lint level is defined here
12-
--> $DIR/instantly-dangling-pointer-methods.rs:1:9
12+
--> $DIR/methods.rs:1:9
1313
|
14-
LL | #![deny(instantly_dangling_pointer)]
15-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
LL | #![deny(dangling_pointers_from_temporaries)]
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616

1717
error: getting a pointer from a temporary `Vec<u8>` will result in a dangling pointer
18-
--> $DIR/instantly-dangling-pointer-methods.rs:5:15
18+
--> $DIR/methods.rs:5:15
1919
|
2020
LL | vec![0u8].as_mut_ptr();
2121
| --------- ^^^^^^^^^^ this pointer will immediately be invalid

tests/ui/lint/dangling-ptr/instantly-dangling-pointer-temporaries.rs renamed to tests/ui/lint/dangling-ptr/temporaries.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(unused)]
2-
#![deny(instantly_dangling_pointer)]
2+
#![deny(dangling_pointers_from_temporaries)]
33

44
fn string() -> String {
55
"hello".into()
@@ -18,16 +18,16 @@ fn main() {
1818
}
1919

2020
// Call
21-
string().as_ptr(); //~ ERROR [instantly_dangling_pointer]
21+
string().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
2222

2323
// MethodCall
24-
"hello".to_string().as_ptr(); //~ ERROR [instantly_dangling_pointer]
24+
"hello".to_string().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
2525

2626
// Tup
2727
// impossible
2828

2929
// Binary
30-
(string() + "hello").as_ptr(); //~ ERROR [instantly_dangling_pointer]
30+
(string() + "hello").as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
3131

3232
// Path
3333
{
@@ -63,30 +63,30 @@ fn main() {
6363
// If
6464
{
6565
(if true { String::new() } else { "hello".into() }).as_ptr();
66-
//~^ ERROR [instantly_dangling_pointer]
66+
//~^ ERROR [dangling_pointers_from_temporaries]
6767
}
6868

6969
// Loop
7070
{
7171
(loop {
7272
break String::new();
7373
})
74-
.as_ptr(); //~ ERROR [instantly_dangling_pointer]
74+
.as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
7575
}
7676

7777
// Match
7878
{
7979
match string() {
8080
s => s,
8181
}
82-
.as_ptr(); //~ ERROR [instantly_dangling_pointer]
82+
.as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
8383
}
8484

8585
// Closure
8686
// impossible
8787

8888
// Block
89-
{ string() }.as_ptr(); //~ ERROR [instantly_dangling_pointer]
89+
{ string() }.as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
9090

9191
// Assign, AssignOp
9292
// impossible
@@ -125,5 +125,5 @@ fn main() {
125125
// impossible
126126

127127
// Macro
128-
vec![0u8].as_ptr(); //~ ERROR [instantly_dangling_pointer]
128+
vec![0u8].as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
129129
}

tests/ui/lint/dangling-ptr/instantly-dangling-pointer-temporaries.stderr renamed to tests/ui/lint/dangling-ptr/temporaries.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: getting a pointer from a temporary `String` will result in a dangling pointer
2-
--> $DIR/instantly-dangling-pointer-temporaries.rs:21:14
2+
--> $DIR/temporaries.rs:21:14
33
|
44
LL | string().as_ptr();
55
| -------- ^^^^^^ this pointer will immediately be invalid
@@ -9,13 +9,13 @@ LL | string().as_ptr();
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
1010
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
1111
note: the lint level is defined here
12-
--> $DIR/instantly-dangling-pointer-temporaries.rs:2:9
12+
--> $DIR/temporaries.rs:2:9
1313
|
14-
LL | #![deny(instantly_dangling_pointer)]
15-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
LL | #![deny(dangling_pointers_from_temporaries)]
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616

1717
error: getting a pointer from a temporary `String` will result in a dangling pointer
18-
--> $DIR/instantly-dangling-pointer-temporaries.rs:24:25
18+
--> $DIR/temporaries.rs:24:25
1919
|
2020
LL | "hello".to_string().as_ptr();
2121
| ------------------- ^^^^^^ this pointer will immediately be invalid
@@ -26,7 +26,7 @@ LL | "hello".to_string().as_ptr();
2626
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
2727

2828
error: getting a pointer from a temporary `String` will result in a dangling pointer
29-
--> $DIR/instantly-dangling-pointer-temporaries.rs:30:26
29+
--> $DIR/temporaries.rs:30:26
3030
|
3131
LL | (string() + "hello").as_ptr();
3232
| -------------------- ^^^^^^ this pointer will immediately be invalid
@@ -37,7 +37,7 @@ LL | (string() + "hello").as_ptr();
3737
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
3838

3939
error: getting a pointer from a temporary `String` will result in a dangling pointer
40-
--> $DIR/instantly-dangling-pointer-temporaries.rs:65:61
40+
--> $DIR/temporaries.rs:65:61
4141
|
4242
LL | (if true { String::new() } else { "hello".into() }).as_ptr();
4343
| --------------------------------------------------- ^^^^^^ this pointer will immediately be invalid
@@ -48,7 +48,7 @@ LL | (if true { String::new() } else { "hello".into() }).as_ptr();
4848
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
4949

5050
error: getting a pointer from a temporary `String` will result in a dangling pointer
51-
--> $DIR/instantly-dangling-pointer-temporaries.rs:74:10
51+
--> $DIR/temporaries.rs:74:10
5252
|
5353
LL | / (loop {
5454
LL | | break String::new();
@@ -61,7 +61,7 @@ LL | .as_ptr();
6161
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
6262

6363
error: getting a pointer from a temporary `String` will result in a dangling pointer
64-
--> $DIR/instantly-dangling-pointer-temporaries.rs:82:10
64+
--> $DIR/temporaries.rs:82:10
6565
|
6666
LL | / match string() {
6767
LL | | s => s,
@@ -74,7 +74,7 @@ LL | .as_ptr();
7474
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
7575

7676
error: getting a pointer from a temporary `String` will result in a dangling pointer
77-
--> $DIR/instantly-dangling-pointer-temporaries.rs:89:18
77+
--> $DIR/temporaries.rs:89:18
7878
|
7979
LL | { string() }.as_ptr();
8080
| ------------ ^^^^^^ this pointer will immediately be invalid
@@ -85,7 +85,7 @@ LL | { string() }.as_ptr();
8585
= help: for more information, see https://doc.rust-lang.org/reference/destructors.html
8686

8787
error: getting a pointer from a temporary `Vec<u8>` will result in a dangling pointer
88-
--> $DIR/instantly-dangling-pointer-temporaries.rs:128:15
88+
--> $DIR/temporaries.rs:128:15
8989
|
9090
LL | vec![0u8].as_ptr();
9191
| --------- ^^^^^^ this pointer will immediately be invalid

tests/ui/lint/dangling-ptr/types.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#![deny(dangling_pointers_from_temporaries)]
2+
3+
use std::cell::Cell;
4+
use std::ffi::{CStr, CString};
5+
use std::mem::MaybeUninit;
6+
7+
struct AsPtrFake;
8+
9+
impl AsPtrFake {
10+
fn as_ptr(&self) -> *const () {
11+
std::ptr::null()
12+
}
13+
}
14+
15+
fn declval<T>() -> T {
16+
loop {}
17+
}
18+
19+
fn main() {
20+
declval::<CString>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
21+
declval::<String>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
22+
declval::<Vec<u8>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
23+
declval::<Box<CString>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
24+
declval::<Box<[u8]>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
25+
declval::<Box<str>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
26+
declval::<Box<CStr>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
27+
declval::<[u8; 10]>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
28+
declval::<Box<[u8; 10]>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
29+
declval::<Box<Vec<u8>>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
30+
declval::<Box<String>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
31+
declval::<Box<Box<Box<Box<[u8]>>>>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
32+
declval::<Cell<u8>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
33+
declval::<MaybeUninit<u8>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
34+
declval::<Vec<AsPtrFake>>().as_ptr(); //~ ERROR [dangling_pointers_from_temporaries]
35+
declval::<Box<AsPtrFake>>().as_ptr();
36+
declval::<AsPtrFake>().as_ptr();
37+
}

0 commit comments

Comments
 (0)