Skip to content

Commit 99f3475

Browse files
committed
Add test for unnecessary_refs lint
1 parent 6ebd41a commit 99f3475

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-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
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ check-pass
2+
//@ run-rustfix
3+
4+
#![allow(dead_code, unused_variables)]
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+
//~^ WARN 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+
//~^ WARN 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+
//~^ WARN 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+
//~^ WARN creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
29+
}
30+
31+
fn main() {
32+
let a = 0;
33+
let a = &raw const a;
34+
//~^ WARN creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
35+
36+
let mut b = 0;
37+
let b = &raw mut b;
38+
//~^ WARN creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
39+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ check-pass
2+
//@ run-rustfix
3+
4+
#![allow(dead_code, unused_variables)]
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+
//~^ WARN 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+
//~^ WARN 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+
//~^ WARN 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+
//~^ WARN creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
29+
}
30+
31+
fn main() {
32+
let a = 0;
33+
let a = &a as *const i32;
34+
//~^ WARN creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
35+
36+
let mut b = 0;
37+
let b = &mut b as *mut i32;
38+
//~^ WARN creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers [unnecessary_refs]
39+
}
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
warning: 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: `#[warn(unnecessary_refs)]` on by default
8+
help: consider using `&raw const` for a safer and more explicit raw pointer
9+
|
10+
LL - unsafe { &(*x).0 as *const i32 }
11+
LL + unsafe { &raw const (*x).0 }
12+
|
13+
14+
warning: creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers
15+
--> $DIR/lint-unnecessary-refs.rs:17:14
16+
|
17+
LL | unsafe { &(*x).b as *const u8 }
18+
| ^^^^^^^^^^^^^^^^^^^^
19+
|
20+
help: consider using `&raw const` for a safer and more explicit raw pointer
21+
|
22+
LL - unsafe { &(*x).b as *const u8 }
23+
LL + unsafe { &raw const (*x).b }
24+
|
25+
26+
warning: creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers
27+
--> $DIR/lint-unnecessary-refs.rs:22:14
28+
|
29+
LL | unsafe { &mut (*x).0 as *mut i32 }
30+
| ^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
help: consider using `&raw mut` for a safer and more explicit raw pointer
33+
|
34+
LL - unsafe { &mut (*x).0 as *mut i32 }
35+
LL + unsafe { &raw mut (*x).0 }
36+
|
37+
38+
warning: creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers
39+
--> $DIR/lint-unnecessary-refs.rs:27:14
40+
|
41+
LL | unsafe { &mut (*x).a as *mut i32 }
42+
| ^^^^^^^^^^^^^^^^^^^^^^^
43+
|
44+
help: consider using `&raw mut` for a safer and more explicit raw pointer
45+
|
46+
LL - unsafe { &mut (*x).a as *mut i32 }
47+
LL + unsafe { &raw mut (*x).a }
48+
|
49+
50+
warning: creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers
51+
--> $DIR/lint-unnecessary-refs.rs:33:13
52+
|
53+
LL | let a = &a as *const i32;
54+
| ^^^^^^^^^^^^^^^^
55+
|
56+
help: consider using `&raw const` for a safer and more explicit raw pointer
57+
|
58+
LL - let a = &a as *const i32;
59+
LL + let a = &raw const a;
60+
|
61+
62+
warning: creating a intermediate reference implies aliasing requirements even when immediately casting to raw pointers
63+
--> $DIR/lint-unnecessary-refs.rs:37:13
64+
|
65+
LL | let b = &mut b as *mut i32;
66+
| ^^^^^^^^^^^^^^^^^^
67+
|
68+
help: consider using `&raw mut` for a safer and more explicit raw pointer
69+
|
70+
LL - let b = &mut b as *mut i32;
71+
LL + let b = &raw mut b;
72+
|
73+
74+
warning: 6 warnings emitted
75+

0 commit comments

Comments
 (0)