about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/cast_size.rs
blob: ecc586694191851592a3da517d8f07e2de50bf71 (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
//@revisions: 32bit 64bit
//@[32bit]ignore-bitwidth: 64
//@[64bit]ignore-bitwidth: 32
//@no-rustfix: only some diagnostics have suggestions

#![warn(
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_possible_wrap,
    clippy::cast_lossless
)]
#![allow(clippy::no_effect, clippy::unnecessary_operation)]

fn main() {
    // Casting from *size
    1isize as i8;
    //~^ cast_possible_truncation
    let x0 = 1isize;
    let x1 = 1usize;
    // FIXME(f16_f128): enable f16 and f128 conversions once const eval supports them
    // x0 as f16;
    // x1 as f16;
    x0 as f32;
    //~^ cast_precision_loss
    x1 as f32;
    //~^ cast_precision_loss
    x0 as f64;
    //~^ cast_precision_loss
    x1 as f64;
    //~^ cast_precision_loss
    // x0 as f128;
    // x1 as f128;

    1isize as i32;
    //~^ cast_possible_truncation
    1isize as u32;
    //~^ cast_possible_truncation
    1usize as u32;
    //~^ cast_possible_truncation
    1usize as i32;
    //~^ cast_possible_truncation
    //~| cast_possible_wrap
    1i64 as isize;
    //~^ cast_possible_truncation
    1i64 as usize;
    //~^ cast_possible_truncation
    1u64 as isize;
    //~^ cast_possible_truncation
    //~| cast_possible_wrap
    1u64 as usize;
    //~^ cast_possible_truncation
    1u32 as isize;
    //~^ cast_possible_wrap
    1u32 as usize; // Should not trigger any lint
    1i32 as isize; // Neither should this
    1i32 as usize;

    // Big integer literal to float
    // 999_999 as f16;
    999_999_999 as f32;
    //~^ cast_precision_loss
    9_999_999_999_999_999usize as f64;
    //~^ cast_precision_loss
    //~[32bit]^^ ERROR: literal out of range for `usize`
    // 999_999_999_999_999_999_999_999_999_999u128 as f128;
}

fn issue15163() {
    const M: usize = 100;
    const N: u16 = M as u16;
    //~^ cast_possible_truncation
}