File: unnecessary-transmutation.fixed

package info (click to toggle)
rustc 1.89.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 906,624 kB
  • sloc: xml: 158,148; python: 34,888; javascript: 19,595; sh: 19,221; ansic: 13,046; cpp: 7,144; asm: 4,376; makefile: 692; lisp: 174; sql: 15
file content (114 lines) | stat: -rw-r--r-- 3,049 bytes parent folder | download | duplicates (5)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//@ 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 { u32::to_ne_bytes(x) }
    //~^ ERROR
}

pub const fn intinator_const(from: bool) -> u8 {
    unsafe { (from) as u8 }
    //~^ ERROR
}

pub static X: u8 = unsafe { (true) as u8 };
//~^ ERROR
pub const Y: u8 = unsafe { (true) as u8 };
//~^ ERROR

pub struct Z {}
impl Z {
    pub const fn intinator_assoc(x: bool) -> u8 {
        unsafe { (x) as u8 }
        //~^ ERROR
    }
}

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

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

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

        let y: u32 = u32::from('🦀');
        //~^ ERROR
        let y: char = char::from_u32_unchecked(y);
        //~^ ERROR
        let y: i32 = u32::from('🐱').cast_signed();
        //~^ ERROR
        let y: char = char::from_u32_unchecked(i32::cast_unsigned(y));
        //~^ ERROR

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

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

        let y: f64 = f64::from_bits(i64::cast_unsigned(1i64));
        //~^ ERROR
        let y: i64 = f64::to_bits(1f64).cast_signed();
        //~^ ERROR

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

        let z: bool = transmute(1i8);
        // clippy
        let z: i8 = i8::from(z);
        //~^ ERROR
    }
}