Skip to content

Commit e62b652

Browse files
authored
Rollup merge of #148348 - hkBst:dangling-ptr-lint-1, r=Urgau
dangling ptr lint cleanup This cleans up the dangling_pointers_from_locals lint.
2 parents df1098d + 0bd6a03 commit e62b652

File tree

3 files changed

+99
-99
lines changed

3 files changed

+99
-99
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ lint_confusable_identifier_pair = found both `{$existing_sym}` and `{$sym}` as i
193193
.current_use = this identifier can be confused with `{$existing_sym}`
194194
.other_use = other identifier used here
195195
196-
lint_dangling_pointers_from_locals = a dangling pointer will be produced because the local variable `{$local_var_name}` will be dropped
197-
.ret_ty = return type of the {$fn_kind} is `{$ret_ty}`
198-
.local_var = `{$local_var_name}` is part the {$fn_kind} and will be dropped at the end of the {$fn_kind}
196+
lint_dangling_pointers_from_locals = {$fn_kind} returns a dangling pointer to dropped local variable `{$local_var_name}`
197+
.ret_ty = return type is `{$ret_ty}`
198+
.local_var = local variable `{$local_var_name}` is dropped at the end of the {$fn_kind}
199199
.created_at = dangling pointer created here
200-
.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
200+
.note = a dangling pointer is safe, but dereferencing one is undefined behavior
201201
202202
lint_dangling_pointers_from_temporaries = a dangling pointer will be produced because the temporary `{$ty}` will be dropped
203203
.label_ptr = this pointer will immediately be invalid

tests/ui/lint/dangling-pointers-from-locals.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,54 @@ const X: u8 = 5;
88
fn simple() -> *const u8 {
99
let x = 0;
1010
&x
11-
//~^ WARN a dangling pointer will be produced
11+
//~^ WARN dangling pointer
1212
}
1313

1414
fn bindings() -> *const u8 {
1515
let x = 0;
1616
let x = &x;
1717
x
18-
//~^ WARN a dangling pointer will be produced
18+
//~^ WARN dangling pointer
1919
}
2020

2121
fn bindings_with_return() -> *const u8 {
2222
let x = 42;
2323
let y = &x;
2424
return y;
25-
//~^ WARN a dangling pointer will be produced
25+
//~^ WARN dangling pointer
2626
}
2727

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

3434
fn bindings_and_casts() -> *const u8 {
3535
let x = 0u8;
3636
let x = &x as *const u8;
3737
x as *const u8
38-
//~^ WARN a dangling pointer will be produced
38+
//~^ WARN dangling pointer
3939
}
4040

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

4747
fn with_block() -> *const u8 {
4848
let x = 0;
4949
&{ x }
50-
//~^ WARN a dangling pointer will be produced
50+
//~^ WARN dangling pointer
5151
}
5252

5353
fn with_many_blocks() -> *const u8 {
5454
let x = 0;
5555
{
5656
{
5757
&{
58-
//~^ WARN a dangling pointer will be produced
58+
//~^ WARN dangling pointer
5959
{ x }
6060
}
6161
}
@@ -65,41 +65,41 @@ fn with_many_blocks() -> *const u8 {
6565
fn simple_return() -> *const u8 {
6666
let x = 0;
6767
return &x;
68-
//~^ WARN a dangling pointer will be produced
68+
//~^ WARN dangling pointer
6969
}
7070

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

7777
fn const_and_flow() -> *const u8 {
7878
if false {
7979
let x = 8;
8080
return &x;
81-
//~^ WARN a dangling pointer will be produced
81+
//~^ WARN dangling pointer
8282
}
8383
&X // not dangling
8484
}
8585

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

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

9898
fn closure() -> *const u8 {
9999
let _x = || -> *const u8 {
100100
let x = 8;
101101
return &x;
102-
//~^ WARN a dangling pointer will be produced
102+
//~^ WARN dangling pointer
103103
};
104104
&X // not dangling
105105
}
@@ -111,27 +111,27 @@ fn fn_ptr() -> *const fn() -> u8 {
111111

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

117117
fn as_arg(a: Adt) -> *const Adt {
118118
&a
119-
//~^ WARN a dangling pointer will be produced
119+
//~^ WARN dangling pointer
120120
}
121121

122122
fn fn_ptr_as_arg(a: fn() -> u8) -> *const fn() -> u8 {
123123
&a
124-
//~^ WARN a dangling pointer will be produced
124+
//~^ WARN dangling pointer
125125
}
126126

127127
fn ptr_as_arg(a: *const Adt) -> *const *const Adt {
128128
&a
129-
//~^ WARN a dangling pointer will be produced
129+
//~^ WARN dangling pointer
130130
}
131131

132132
fn adt_as_arg(a: &Adt) -> *const &Adt {
133133
&a
134-
//~^ WARN a dangling pointer will be produced
134+
//~^ WARN dangling pointer
135135
}
136136

137137
fn unit() -> *const () {

0 commit comments

Comments
 (0)