Skip to content

Commit f2e4ff1

Browse files
committed
add tests
1 parent 8bb8d04 commit f2e4ff1

File tree

6 files changed

+92
-2
lines changed

6 files changed

+92
-2
lines changed

tests/ui/consts/const-eval/raw-bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//@ ignore-endian-big
33
// ignore-tidy-linelength
44
//@ normalize-stderr: "╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼" -> "╾ALLOC_ID$1╼"
5-
#![allow(invalid_value)]
5+
#![allow(invalid_value, redundant_transmutation)]
66
#![feature(never_type, rustc_attrs, ptr_metadata, slice_from_ptr_range, const_slice_from_ptr_range)]
77

88
use std::mem;

tests/ui/consts/const-eval/ub-enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//@ normalize-stderr: "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
44
//@ normalize-stderr: "0x0+" -> "0x0"
55
#![feature(never_type)]
6-
#![allow(invalid_value)]
6+
#![allow(invalid_value, redundant_transmutation)]
77

88
use std::mem;
99

tests/ui/issues/issue-25746-bool-transmute.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ run-pass
2+
#![allow(redundant_transmutation)]
23
use std::mem::transmute;
34

45
fn main() {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ run-rustfix
2+
#![deny(redundant_transmutation)]
3+
#![feature(integer_sign_cast)]
4+
#![allow(unused_unsafe, unused_imports)]
5+
use std::mem::transmute;
6+
7+
pub fn bytes_at_home(x: u32) -> [u8; 4] {
8+
unsafe { u32::to_ne_bytes(x) }
9+
//~^ ERROR
10+
}
11+
12+
pub fn from_ne_bytes(x: [u8; 2]) -> u16 {
13+
unsafe { u16::from_ne_bytes(x) }
14+
//~^ ERROR
15+
}
16+
17+
pub fn cast_unsigned(x: i16) -> u16 {
18+
unsafe { i16::cast_unsigned(x) }
19+
//~^ ERROR
20+
}
21+
22+
pub fn to_u32(x: char) -> u32 {
23+
unsafe { (x) as u32 }
24+
//~^ ERROR
25+
}
26+
27+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ run-rustfix
2+
#![deny(redundant_transmutation)]
3+
#![feature(integer_sign_cast)]
4+
#![allow(unused_unsafe, unused_imports)]
5+
use std::mem::transmute;
6+
7+
pub fn bytes_at_home(x: u32) -> [u8; 4] {
8+
unsafe { transmute(x) }
9+
//~^ ERROR
10+
}
11+
12+
pub fn from_ne_bytes(x: [u8; 2]) -> u16 {
13+
unsafe { transmute(x) }
14+
//~^ ERROR
15+
}
16+
17+
pub fn cast_unsigned(x: i16) -> u16 {
18+
unsafe { transmute(x) }
19+
//~^ ERROR
20+
}
21+
22+
pub fn to_u32(x: char) -> u32 {
23+
unsafe { transmute(x) }
24+
//~^ ERROR
25+
}
26+
27+
fn main() {}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error: this transmute could be performed safely
2+
--> $DIR/redundant-transmutation.rs:8:14
3+
|
4+
LL | unsafe { transmute(x) }
5+
| ^^^^^^^^^^^^ help: replace `transmute`: `u32::to_ne_bytes(x)`
6+
|
7+
= help: there's also `to_le_bytes` and `to_ne_bytes` if you expect a particular byte order
8+
note: the lint level is defined here
9+
--> $DIR/redundant-transmutation.rs:2:9
10+
|
11+
LL | #![deny(redundant_transmutation)]
12+
| ^^^^^^^^^^^^^^^^^^^^^^^
13+
14+
error: this transmute could be performed safely
15+
--> $DIR/redundant-transmutation.rs:13:14
16+
|
17+
LL | unsafe { transmute(x) }
18+
| ^^^^^^^^^^^^ help: replace `transmute`: `u16::from_ne_bytes(x)`
19+
|
20+
= help: there's also `from_le_bytes` and `from_ne_bytes` if you expect a particular byte order
21+
22+
error: this transmute could be performed safely
23+
--> $DIR/redundant-transmutation.rs:18:14
24+
|
25+
LL | unsafe { transmute(x) }
26+
| ^^^^^^^^^^^^ help: replace `transmute`: `i16::cast_unsigned(x)`
27+
28+
error: this transmute could be performed safely
29+
--> $DIR/redundant-transmutation.rs:23:14
30+
|
31+
LL | unsafe { transmute(x) }
32+
| ^^^^^^^^^^^^ help: replace `transmute`: `(x) as u32`
33+
34+
error: aborting due to 4 previous errors
35+

0 commit comments

Comments
 (0)