File: unnecessary-transmutation.rs

package info (click to toggle)
rustc 1.88.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 934,128 kB
  • sloc: xml: 158,127; python: 36,062; javascript: 19,855; sh: 19,700; cpp: 18,947; ansic: 12,993; asm: 4,792; makefile: 690; lisp: 29; perl: 29; ruby: 19; sql: 11
file content (94 lines) | stat: -rw-r--r-- 2,359 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//@ run-rustfix
#![deny(unnecessary_transmutes)]
#![allow(unused_unsafe, unused_imports, unused_variables, unused_parens)]
use std::mem::transmute;

pub fn bytes_at_home(x: u32) -> [u8; 4] {
    unsafe { transmute(x) }
    //~^ ERROR
}

fn main() {
    unsafe {
        let x: u16 = transmute(*b"01");
        //~^ ERROR
        let x: [u8; 2] = transmute(x);
        //~^ ERROR
        let x: u32 = transmute(*b"0123");
        //~^ ERROR
        let x: [u8; 4] = transmute(x);
        //~^ ERROR
        let x: u64 = transmute(*b"feriscat");
        //~^ ERROR
        let x: [u8; 8] = transmute(x);
        //~^ ERROR

        let y: i16 = transmute(*b"01");
        //~^ ERROR
        let y: [u8; 2] = transmute(y);
        //~^ ERROR
        let y: i32 = transmute(*b"0123");
        //~^ ERROR
        let y: [u8; 4] = transmute(y);
        //~^ ERROR
        let y: i64 = transmute(*b"feriscat");
        //~^ ERROR
        let y: [u8; 8] = transmute(y);
        //~^ ERROR

        let z: f32 = transmute(*b"0123");
        //~^ ERROR
        let z: [u8; 4] = transmute(z);
        //~^ ERROR
        let z: f64 = transmute(*b"feriscat");
        //~^ ERROR
        let z: [u8; 8] = transmute(z);
        //~^ ERROR

        let y: u32 = transmute('🦀');
        //~^ ERROR
        let y: char = transmute(y);
        //~^ ERROR
        let y: i32 = transmute('🐱');
        //~^ ERROR
        let y: char = transmute(y);
        //~^ ERROR

        let x: u16 = transmute(8i16);
        //~^ ERROR
        let x: i16 = transmute(x);
        //~^ ERROR
        let x: u32 = transmute(4i32);
        //~^ ERROR
        let x: i32 = transmute(x);
        //~^ ERROR
        let x: u64 = transmute(7i64);
        //~^ ERROR
        let x: i64 = transmute(x);
        //~^ ERROR

        let y: f32 = transmute(1u32);
        //~^ ERROR
        let y: u32 = transmute(y);
        //~^ ERROR
        let y: f64 = transmute(3u64);
        //~^ ERROR
        let y: u64 = transmute(2.0);
        //~^ ERROR

        let y: f64 = transmute(1i64);
        //~^ ERROR
        let y: i64 = transmute(1f64);
        //~^ ERROR

        let z: bool = transmute(1u8);
        //~^ ERROR
        let z: u8 = transmute(z);
        //~^ ERROR

        let z: bool = transmute(1i8);
        // no error!
        let z: i8 = transmute(z);
        //~^ ERROR
    }
}