Skip to content

Commit eaf90b1

Browse files
committed
Add test for raw_borrows_via_references lint
Signed-off-by: Obei Sideg <obei.sideg@gmail.com>
1 parent 23ff64c commit eaf90b1

3 files changed

Lines changed: 301 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//@ check-pass
2+
//@ run-rustfix
3+
//@ rustfix-only-machine-applicable
4+
5+
#![allow(dead_code, unused_variables)]
6+
#![warn(raw_borrows_via_references)]
7+
8+
struct A {
9+
a: i32,
10+
b: u8,
11+
}
12+
13+
fn via_ref(x: *const (i32, i32)) -> *const i32 {
14+
unsafe { &raw const (*x).0 }
15+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
16+
}
17+
18+
fn via_ref_struct(x: *const A) -> *const u8 {
19+
unsafe { &raw const (*x).b }
20+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
21+
}
22+
23+
fn via_ref_mut(x: *mut (i32, i32)) -> *mut i32 {
24+
unsafe { &raw mut (*x).0 }
25+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
26+
}
27+
28+
fn via_ref_struct_mut(x: *mut A) -> *mut i32 {
29+
unsafe { &raw mut (*x).a }
30+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
31+
}
32+
33+
fn multiple_casts(x: *const (i32, i32)) -> *const u8 {
34+
unsafe { &raw const (*x).0 as *const u8 }
35+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
36+
}
37+
38+
fn ret_i32() -> i32 {
39+
0
40+
}
41+
42+
fn temporaries() {
43+
let _ = &1 as *const i32;
44+
let _ = &(1 + 2) as *const i32;
45+
let _ = &ret_i32() as *const i32;
46+
let _ = &(1, 2) as *const (i32, i32);
47+
let _ = &[1, 2, 3] as *const [i32; 3];
48+
let _ = &A { a: 0, b: 0 } as *const A;
49+
let _ = &mut 4 as *mut i32;
50+
}
51+
52+
fn inner_blocks() {
53+
let x = 0;
54+
let _ = { &raw const x };
55+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
56+
57+
let _ = {
58+
let y = 0;
59+
{ &raw const y }
60+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
61+
};
62+
63+
let _ = { &1 as *const i32 };
64+
}
65+
66+
macro_rules! ref_cast {
67+
($e:expr) => {
68+
&$e as *const i32
69+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
70+
};
71+
}
72+
73+
fn from_macro(x: *const i32) -> *const i32 {
74+
unsafe { ref_cast!(*x) }
75+
}
76+
77+
fn main() {
78+
let a = 0;
79+
let a = &raw const a;
80+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
81+
82+
let mut b = 0;
83+
let b = &raw mut b;
84+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
85+
86+
let i = &1 as *const i32;
87+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//@ check-pass
2+
//@ run-rustfix
3+
//@ rustfix-only-machine-applicable
4+
5+
#![allow(dead_code, unused_variables)]
6+
#![warn(raw_borrows_via_references)]
7+
8+
struct A {
9+
a: i32,
10+
b: u8,
11+
}
12+
13+
fn via_ref(x: *const (i32, i32)) -> *const i32 {
14+
unsafe { &(*x).0 as *const i32 }
15+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
16+
}
17+
18+
fn via_ref_struct(x: *const A) -> *const u8 {
19+
unsafe { &(*x).b as *const u8 }
20+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
21+
}
22+
23+
fn via_ref_mut(x: *mut (i32, i32)) -> *mut i32 {
24+
unsafe { &mut (*x).0 as *mut i32 }
25+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
26+
}
27+
28+
fn via_ref_struct_mut(x: *mut A) -> *mut i32 {
29+
unsafe { &mut (*x).a as *mut i32 }
30+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
31+
}
32+
33+
fn multiple_casts(x: *const (i32, i32)) -> *const u8 {
34+
unsafe { &(*x).0 as *const i32 as *const u8 }
35+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
36+
}
37+
38+
fn ret_i32() -> i32 {
39+
0
40+
}
41+
42+
fn temporaries() {
43+
let _ = &1 as *const i32;
44+
let _ = &(1 + 2) as *const i32;
45+
let _ = &ret_i32() as *const i32;
46+
let _ = &(1, 2) as *const (i32, i32);
47+
let _ = &[1, 2, 3] as *const [i32; 3];
48+
let _ = &A { a: 0, b: 0 } as *const A;
49+
let _ = &mut 4 as *mut i32;
50+
}
51+
52+
fn inner_blocks() {
53+
let x = 0;
54+
let _ = { &x as *const i32 };
55+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
56+
57+
let _ = {
58+
let y = 0;
59+
{ &y as *const i32 }
60+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
61+
};
62+
63+
let _ = { &1 as *const i32 };
64+
}
65+
66+
macro_rules! ref_cast {
67+
($e:expr) => {
68+
&$e as *const i32
69+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
70+
};
71+
}
72+
73+
fn from_macro(x: *const i32) -> *const i32 {
74+
unsafe { ref_cast!(*x) }
75+
}
76+
77+
fn main() {
78+
let a = 0;
79+
let a = &a as *const i32;
80+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
81+
82+
let mut b = 0;
83+
let b = &mut b as *mut i32;
84+
//~^ WARN creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers [raw_borrows_via_references]
85+
86+
let i = &1 as *const i32;
87+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
2+
--> $DIR/lint-raw-borrows-via-references.rs:14:14
3+
|
4+
LL | unsafe { &(*x).0 as *const i32 }
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-raw-borrows-via-references.rs:6:9
9+
|
10+
LL | #![warn(raw_borrows_via_references)]
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+
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
19+
--> $DIR/lint-raw-borrows-via-references.rs:19: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+
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
31+
--> $DIR/lint-raw-borrows-via-references.rs:24:14
32+
|
33+
LL | unsafe { &mut (*x).0 as *mut i32 }
34+
| ^^^^^^^^^^^^^^^^^^^^^^^
35+
|
36+
help: consider using `&raw mut` 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+
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
43+
--> $DIR/lint-raw-borrows-via-references.rs:29:14
44+
|
45+
LL | unsafe { &mut (*x).a as *mut i32 }
46+
| ^^^^^^^^^^^^^^^^^^^^^^^
47+
|
48+
help: consider using `&raw mut` 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+
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
55+
--> $DIR/lint-raw-borrows-via-references.rs:34:14
56+
|
57+
LL | unsafe { &(*x).0 as *const i32 as *const u8 }
58+
| ^^^^^^^^^^^^^^^^^^^^^
59+
|
60+
help: consider using `&raw const` for a safer and more explicit raw pointer
61+
|
62+
LL - unsafe { &(*x).0 as *const i32 as *const u8 }
63+
LL + unsafe { &raw const (*x).0 as *const u8 }
64+
|
65+
66+
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
67+
--> $DIR/lint-raw-borrows-via-references.rs:54:15
68+
|
69+
LL | let _ = { &x as *const i32 };
70+
| ^^^^^^^^^^^^^^^^
71+
|
72+
help: consider using `&raw const` for a safer and more explicit raw pointer
73+
|
74+
LL - let _ = { &x as *const i32 };
75+
LL + let _ = { &raw const x };
76+
|
77+
78+
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
79+
--> $DIR/lint-raw-borrows-via-references.rs:59:11
80+
|
81+
LL | { &y as *const i32 }
82+
| ^^^^^^^^^^^^^^^^
83+
|
84+
help: consider using `&raw const` for a safer and more explicit raw pointer
85+
|
86+
LL - { &y as *const i32 }
87+
LL + { &raw const y }
88+
|
89+
90+
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
91+
--> $DIR/lint-raw-borrows-via-references.rs:68:10
92+
|
93+
LL | &$e as *const i32
94+
| ^^^^^^^^^^^^^^^^
95+
...
96+
LL | unsafe { ref_cast!(*x) }
97+
| ------------- in this macro invocation
98+
|
99+
= help: consider using `&raw const` for a safer and more explicit raw pointer
100+
= note: this warning originates in the macro `ref_cast` (in Nightly builds, run with -Z macro-backtrace for more info)
101+
102+
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
103+
--> $DIR/lint-raw-borrows-via-references.rs:79:13
104+
|
105+
LL | let a = &a as *const i32;
106+
| ^^^^^^^^^^^^^^^^
107+
|
108+
help: consider using `&raw const` for a safer and more explicit raw pointer
109+
|
110+
LL - let a = &a as *const i32;
111+
LL + let a = &raw const a;
112+
|
113+
114+
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
115+
--> $DIR/lint-raw-borrows-via-references.rs:83:13
116+
|
117+
LL | let b = &mut b as *mut i32;
118+
| ^^^^^^^^^^^^^^^^^^
119+
|
120+
help: consider using `&raw mut` for a safer and more explicit raw pointer
121+
|
122+
LL - let b = &mut b as *mut i32;
123+
LL + let b = &raw mut b;
124+
|
125+
126+
warning: 10 warnings emitted
127+

0 commit comments

Comments
 (0)