Skip to content

Commit e91014c

Browse files
Add ui test for missing_transmute_annotations
1 parent 586bba4 commit e91014c

4 files changed

+187
-0
lines changed

tests/ui/auxiliary/macro_rules.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,10 @@ macro_rules! macro_with_panic {
5050
panic!()
5151
};
5252
}
53+
54+
#[macro_export]
55+
macro_rules! bad_transmute {
56+
($e:expr) => {
57+
std::mem::transmute($e)
58+
};
59+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//@aux-build:macro_rules.rs
2+
3+
#![warn(clippy::missing_transmute_annotations)]
4+
5+
#[macro_use]
6+
extern crate macro_rules;
7+
8+
macro_rules! local_bad_transmute {
9+
($e:expr) => {
10+
std::mem::transmute::<[u16; 2], i32>($e)
11+
};
12+
}
13+
14+
fn bar(x: i32) -> i32 {
15+
x
16+
}
17+
18+
unsafe fn foo1() -> i32 {
19+
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16])
20+
//~^ ERROR: transmute used without annotations
21+
}
22+
23+
unsafe fn foo2() -> i32 {
24+
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16])
25+
//~^ ERROR: transmute used without annotations
26+
}
27+
28+
unsafe fn foo3() -> i32 {
29+
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16])
30+
//~^ ERROR: transmute used without annotations
31+
}
32+
33+
unsafe fn foo4() -> i32 {
34+
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16])
35+
//~^ ERROR: transmute used without annotations
36+
}
37+
38+
unsafe fn foo5() -> i32 {
39+
let x: i32 = bar(std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]));
40+
//~^ ERROR: transmute used without annotations
41+
bar(std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]))
42+
//~^ ERROR: transmute used without annotations
43+
}
44+
45+
unsafe fn foo6() -> i32 {
46+
local_bad_transmute!([1u16, 2u16])
47+
//~^ ERROR: transmute used without annotations
48+
}
49+
50+
unsafe fn foo7() -> i32 {
51+
// Should not warn.
52+
bad_transmute!([1u16, 2u16])
53+
}
54+
55+
fn main() {
56+
unsafe {
57+
// Should not warn.
58+
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
59+
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
60+
let x: i32 = std::mem::transmute::<[u16; 2], _>([1u16, 2u16]);
61+
let x: i32 = std::mem::transmute::<_, i32>([1u16, 2u16]);
62+
let x: i32 = std::mem::transmute([1u16, 2u16]);
63+
}
64+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//@aux-build:macro_rules.rs
2+
3+
#![warn(clippy::missing_transmute_annotations)]
4+
5+
#[macro_use]
6+
extern crate macro_rules;
7+
8+
macro_rules! local_bad_transmute {
9+
($e:expr) => {
10+
std::mem::transmute($e)
11+
};
12+
}
13+
14+
fn bar(x: i32) -> i32 {
15+
x
16+
}
17+
18+
unsafe fn foo1() -> i32 {
19+
std::mem::transmute([1u16, 2u16])
20+
//~^ ERROR: transmute used without annotations
21+
}
22+
23+
unsafe fn foo2() -> i32 {
24+
std::mem::transmute::<_, _>([1u16, 2u16])
25+
//~^ ERROR: transmute used without annotations
26+
}
27+
28+
unsafe fn foo3() -> i32 {
29+
std::mem::transmute::<_, i32>([1u16, 2u16])
30+
//~^ ERROR: transmute used without annotations
31+
}
32+
33+
unsafe fn foo4() -> i32 {
34+
std::mem::transmute::<[u16; 2], _>([1u16, 2u16])
35+
//~^ ERROR: transmute used without annotations
36+
}
37+
38+
unsafe fn foo5() -> i32 {
39+
let x: i32 = bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]));
40+
//~^ ERROR: transmute used without annotations
41+
bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]))
42+
//~^ ERROR: transmute used without annotations
43+
}
44+
45+
unsafe fn foo6() -> i32 {
46+
local_bad_transmute!([1u16, 2u16])
47+
//~^ ERROR: transmute used without annotations
48+
}
49+
50+
unsafe fn foo7() -> i32 {
51+
// Should not warn.
52+
bad_transmute!([1u16, 2u16])
53+
}
54+
55+
fn main() {
56+
unsafe {
57+
// Should not warn.
58+
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
59+
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
60+
let x: i32 = std::mem::transmute::<[u16; 2], _>([1u16, 2u16]);
61+
let x: i32 = std::mem::transmute::<_, i32>([1u16, 2u16]);
62+
let x: i32 = std::mem::transmute([1u16, 2u16]);
63+
}
64+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error: transmute used without annotations
2+
--> tests/ui/missing_transmute_annotations.rs:19:15
3+
|
4+
LL | std::mem::transmute([1u16, 2u16])
5+
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
6+
|
7+
= note: `-D clippy::missing-transmute-annotations` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::missing_transmute_annotations)]`
9+
10+
error: transmute used without annotations
11+
--> tests/ui/missing_transmute_annotations.rs:24:15
12+
|
13+
LL | std::mem::transmute::<_, _>([1u16, 2u16])
14+
| ^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
15+
16+
error: transmute used without annotations
17+
--> tests/ui/missing_transmute_annotations.rs:29:15
18+
|
19+
LL | std::mem::transmute::<_, i32>([1u16, 2u16])
20+
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
21+
22+
error: transmute used without annotations
23+
--> tests/ui/missing_transmute_annotations.rs:34:15
24+
|
25+
LL | std::mem::transmute::<[u16; 2], _>([1u16, 2u16])
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
27+
28+
error: transmute used without annotations
29+
--> tests/ui/missing_transmute_annotations.rs:39:32
30+
|
31+
LL | let x: i32 = bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]));
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
33+
34+
error: transmute used without annotations
35+
--> tests/ui/missing_transmute_annotations.rs:41:19
36+
|
37+
LL | bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]))
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
39+
40+
error: transmute used without annotations
41+
--> tests/ui/missing_transmute_annotations.rs:10:19
42+
|
43+
LL | std::mem::transmute($e)
44+
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
45+
...
46+
LL | local_bad_transmute!([1u16, 2u16])
47+
| ---------------------------------- in this macro invocation
48+
|
49+
= note: this error originates in the macro `local_bad_transmute` (in Nightly builds, run with -Z macro-backtrace for more info)
50+
51+
error: aborting due to 7 previous errors
52+

0 commit comments

Comments
 (0)