about summary refs log tree commit diff
path: root/tests/ui/cast/cast-as-bool.rs
blob: fa9664e572c036e5dfc3aae2179982ae7e7b0c81 (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
//@ dont-require-annotations: SUGGESTION

fn main() {
    let u = 5 as bool; //~ ERROR cannot cast `i32` as `bool`
                       //~| HELP compare with zero instead
                       //~| SUGGESTION != 0

    let t = (1 + 2) as bool; //~ ERROR cannot cast `i32` as `bool`
                             //~| HELP compare with zero instead
                             //~| SUGGESTION != 0

    let _ = 5_u32 as bool; //~ ERROR cannot cast `u32` as `bool`
                           //~| HELP compare with zero instead

    let _ = 64.0_f64 as bool; //~ ERROR cannot cast `f64` as `bool`
                              //~| HELP compare with zero instead

    // Enums that can normally be cast to integers can't be cast to `bool`, just like integers.
    // Note that enums that cannot be cast to integers can't be cast to anything at *all*
    // so that's not tested here.
    enum IntEnum {
        Zero,
        One,
        Two
    }
    let _ = IntEnum::One as bool; //~ ERROR cannot cast `IntEnum` as `bool`

    fn uwu(_: u8) -> String {
        todo!()
    }

    unsafe fn owo() {}

    // fn item to bool
    let _ = uwu as bool; //~ ERROR cannot cast `fn(u8) -> String {uwu}` as `bool`
    // unsafe fn item
    let _ = owo as bool; //~ ERROR cannot cast `unsafe fn() {owo}` as `bool`

    // fn ptr to bool
    let _ = uwu as fn(u8) -> String as bool; //~ ERROR cannot cast `fn(u8) -> String` as `bool`

    let _ = 'x' as bool; //~ ERROR cannot cast `char` as `bool`

    let ptr = 1 as *const ();

    let _ = ptr as bool; //~ ERROR cannot cast `*const ()` as `bool`

    let v = "hello" as bool;
    //~^ ERROR casting `&'static str` as `bool` is invalid
    //~| HELP consider using the `is_empty` method on `&'static str` to determine if it contains anything
}