Skip to content

Commit faf0620

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

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-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
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@ run-rustfix
2+
3+
#![deny(unnecessary_refs)]
4+
#![allow(dead_code)]
5+
6+
struct A {
7+
a: i32,
8+
b: u8,
9+
}
10+
11+
fn via_ref(x: *const (i32, i32)) -> *const i32 {
12+
unsafe { &raw const (*x).0 }
13+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
14+
}
15+
16+
fn via_ref_struct(x: *const A) -> *const u8 {
17+
unsafe { &raw const (*x).b }
18+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
19+
}
20+
21+
fn via_ref_mut(x: *mut (i32, i32)) -> *mut i32 {
22+
unsafe { &raw mut (*x).0 }
23+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
24+
}
25+
26+
fn via_ref_struct_mut(x: *mut A) -> *mut i32 {
27+
unsafe { &raw mut (*x).a }
28+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
29+
}
30+
31+
fn main() {}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@ run-rustfix
2+
3+
#![deny(unnecessary_refs)]
4+
#![allow(dead_code)]
5+
6+
struct A {
7+
a: i32,
8+
b: u8,
9+
}
10+
11+
fn via_ref(x: *const (i32, i32)) -> *const i32 {
12+
unsafe { &(*x).0 as *const i32 }
13+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
14+
}
15+
16+
fn via_ref_struct(x: *const A) -> *const u8 {
17+
unsafe { &(*x).b as *const u8 }
18+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
19+
}
20+
21+
fn via_ref_mut(x: *mut (i32, i32)) -> *mut i32 {
22+
unsafe { &mut (*x).0 as *mut i32 }
23+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
24+
}
25+
26+
fn via_ref_struct_mut(x: *mut A) -> *mut i32 {
27+
unsafe { &mut (*x).a as *mut i32 }
28+
//~^ ERROR creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
29+
}
30+
31+
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:12: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:3: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:17: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:22: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:27: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)