File: cast-as-bool.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie, trixie-backports, trixie-proposed-updates
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (49 lines) | stat: -rw-r--r-- 1,748 bytes parent folder | download | duplicates (8)
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
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
}