Skip to content

Commit e113827

Browse files
committed
Add test for unnecessary_refs lint
1 parent ca499a4 commit e113827

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

tests/ui/consts/offset_from.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//@ run-pass
22

3+
#![allow(unnecessary_refs)]
4+
35
struct Struct {
46
field: (),
57
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![deny(unnecessary_refs)]
2+
3+
struct A {
4+
a: i32,
5+
b: u8,
6+
}
7+
8+
fn via_ref(x: *const (i32, i32)) -> *const i32 {
9+
unsafe { &(*x).0 as *const i32 }
10+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
11+
}
12+
13+
fn via_ref_struct(x: *const A) -> *const u8 {
14+
unsafe { &(*x).b as *const u8 }
15+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
16+
}
17+
18+
fn via_ref_mut(x: *mut (i32, i32)) -> *mut i32 {
19+
unsafe { &mut (*x).0 as *mut i32 }
20+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
21+
}
22+
23+
fn via_ref_struct_mut(x: *mut A) -> *mut i32 {
24+
unsafe { &mut (*x).a as *mut i32 }
25+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
26+
}
27+
28+
fn main() {}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
error: creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers
2+
--> $DIR/lint-unnecessary-refs.rs:9:14
3+
|
4+
LL | unsafe { &(*x).0 as *const i32 }
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-unnecessary-refs.rs:1:9
9+
|
10+
LL | #![deny(unnecessary_refs)]
11+
| ^^^^^^^^^^^^^^^^
12+
help: consider using `&raw const` for a safer and more explicit raw pointer
13+
|
14+
LL - unsafe { &(*x).0 as *const i32 }
15+
LL + unsafe { &raw const (*x).0 }
16+
|
17+
18+
error: creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers
19+
--> $DIR/lint-unnecessary-refs.rs:14:14
20+
|
21+
LL | unsafe { &(*x).b as *const u8 }
22+
| ^^^^^^^^^^^^^^^^^^^^
23+
|
24+
help: consider using `&raw const` for a safer and more explicit raw pointer
25+
|
26+
LL - unsafe { &(*x).b as *const u8 }
27+
LL + unsafe { &raw const (*x).b }
28+
|
29+
30+
error: creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers
31+
--> $DIR/lint-unnecessary-refs.rs:19:14
32+
|
33+
LL | unsafe { &mut (*x).0 as *mut i32 }
34+
| ^^^^^^^^^^^^^^^^^^^^^^^
35+
|
36+
help: consider using `&raw const` for a safer and more explicit raw pointer
37+
|
38+
LL - unsafe { &mut (*x).0 as *mut i32 }
39+
LL + unsafe { &raw mut (*x).0 }
40+
|
41+
42+
error: creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers
43+
--> $DIR/lint-unnecessary-refs.rs:24:14
44+
|
45+
LL | unsafe { &mut (*x).a as *mut i32 }
46+
| ^^^^^^^^^^^^^^^^^^^^^^^
47+
|
48+
help: consider using `&raw const` for a safer and more explicit raw pointer
49+
|
50+
LL - unsafe { &mut (*x).a as *mut i32 }
51+
LL + unsafe { &raw mut (*x).a }
52+
|
53+
54+
error: aborting due to 4 previous errors
55+

0 commit comments

Comments
 (0)