summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/unnecessary_cast.fixed
blob: 18dd53bf2b42c050c4a123beec10d732bc45274b (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//@aux-build:extern_fake_libc.rs
#![warn(clippy::unnecessary_cast)]
#![allow(
    clippy::borrow_as_ptr,
    clippy::no_effect,
    clippy::nonstandard_macro_braces,
    clippy::unnecessary_operation,
    nonstandard_style,
    unused
)]

extern crate extern_fake_libc;

type PtrConstU8 = *const u8;
type PtrMutU8 = *mut u8;

fn owo<T>(ptr: *const T) -> *const T {
    ptr
}

fn uwu<T, U>(ptr: *const T) -> *const U {
    ptr as *const U
}

mod fake_libc {
    type pid_t = i32;
    pub unsafe fn getpid() -> pid_t {
        pid_t::from(0)
    }
    // Make sure a where clause does not break it
    pub fn getpid_SAFE_TRUTH<T: Clone>(t: &T) -> pid_t
    where
        T: Clone,
    {
        t;
        unsafe { getpid() }
    }
}

fn aaa() -> ::std::primitive::u32 {
    0
}

use std::primitive::u32 as UnsignedThirtyTwoBitInteger;

fn bbb() -> UnsignedThirtyTwoBitInteger {
    0
}

#[rustfmt::skip]
fn main() {
    // Test cast_unnecessary
    1_i32;
    1_f32;
    false;
    &1i32 as &i32;

    -1_i32;
    - 1_i32;
    -1_f32;
    1_i32;
    1_f32;

    let _: *mut u8 = [1u8, 2].as_ptr() as *mut u8;

    [1u8, 2].as_ptr();
    [1u8, 2].as_ptr() as *mut u8;
    [1u8, 2].as_mut_ptr();
    [1u8, 2].as_mut_ptr() as *const u8;
    [1u8, 2].as_ptr() as PtrConstU8;
    [1u8, 2].as_ptr() as PtrMutU8;
    [1u8, 2].as_mut_ptr() as PtrMutU8;
    [1u8, 2].as_mut_ptr() as PtrConstU8;
    let _: *const u8 = [1u8, 2].as_ptr() as _;
    let _: *mut u8 = [1u8, 2].as_mut_ptr() as _;
    let _: *const u8 = [1u8, 2].as_ptr() as *const _;
    let _: *mut u8 = [1u8, 2].as_mut_ptr() as *mut _;

    owo::<u32>([1u32].as_ptr());
    uwu::<u32, u8>([1u32].as_ptr());
    // this will not lint in the function body even though they have the same type, instead here
    uwu::<u32, u32>([1u32].as_ptr());

    // macro version
    macro_rules! foo {
        ($a:ident, $b:ident) => {
            #[allow(unused)]
            pub fn $a() -> $b {
                1 as $b
            }
        };
    }
    foo!(a, i32);
    foo!(b, f32);
    foo!(c, f64);

    // do not lint cast from cfg-dependant type
    let x = 0 as std::ffi::c_ulong;
    let y = x as u64;
    let x: std::ffi::c_ulong = 0;
    let y = x as u64;

    // do not lint cast to cfg-dependant type
    let x = 1 as std::os::raw::c_char;
    let y = x as u64;

    // do not lint cast to alias type
    1 as I32Alias;
    &1 as &I32Alias;
    // or from
    let x: I32Alias = 1;
    let y = x as u64;
    fake_libc::getpid_SAFE_TRUTH(&0u32) as i32;
    extern_fake_libc::getpid_SAFE_TRUTH() as i32;
    let pid = unsafe { fake_libc::getpid() };
    pid as i32;
    aaa();
    let x = aaa();
    aaa();
    // Will not lint currently.
    bbb() as u32;
    let x = bbb();
    bbb() as u32;

    let i8_ptr: *const i8 = &1;
    let u8_ptr: *const u8 = &1;

    // cfg dependant pointees
    i8_ptr as *const std::os::raw::c_char;
    u8_ptr as *const std::os::raw::c_char;

    // type aliased pointees
    i8_ptr as *const std::ffi::c_char;
    u8_ptr as *const std::ffi::c_char;

    // issue #9960
    macro_rules! bind_var {
        ($id:ident, $e:expr) => {{
            let $id = 0usize;
            let _ = $e != 0usize;
            let $id = 0isize;
            let _ = $e != 0usize;
        }}
    }
    bind_var!(x, (x as usize) + 1);
}

type I32Alias = i32;

mod fixable {
    #![allow(dead_code)]

    fn main() {
        // casting integer literal to float is unnecessary
        100_f32;
        100_f64;
        100_f64;
        let _ = -100_f32;
        let _ = -100_f64;
        let _ = -100_f64;
        100_f32;
        100_f64;
        // Should not trigger
        #[rustfmt::skip]
        let v = vec!(1);
        &v as &[i32];
        0x10 as f32;
        0o10 as f32;
        0b10 as f32;
        0x11 as f64;
        0o11 as f64;
        0b11 as f64;

        1_u32;
        0x10_i32;
        0b10_usize;
        0o73_u16;
        1_000_000_000_u32;

        1.0_f64;
        0.5_f32;

        1.0 as u16;

        let _ = -1_i32;
        let _ = -1.0_f32;

        let _ = 1 as I32Alias;
        let _ = &1 as &I32Alias;

        let x = 1i32;
        let _ = &{ x };
    }

    type I32Alias = i32;

    fn issue_9380() {
        let _: i32 = -1_i32;
        let _: f32 = -(1) as f32;
        let _: i64 = -1_i64;
        let _: i64 = -(1.0) as i64;

        let _ = -(1 + 1) as i64;
    }

    fn issue_9563() {
        let _: f64 = (-8.0_f64).exp();
        #[allow(clippy::precedence)]
        let _: f64 = -8.0_f64.exp(); // should suggest `-8.0_f64.exp()` here not to change code behavior
    }

    fn issue_9562_non_literal() {
        fn foo() -> f32 {
            0.
        }

        let _num = foo();
    }

    fn issue_9603() {
        let _: f32 = -0x400 as f32;
    }
}