about summary refs log tree commit diff
path: root/tests/ui/cast/cast-enum-to-primitive-error.rs
blob: fdbe63b9bc73facb75e8d72aad0d13a3471ccbb2 (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
//! This test verifies that a direct non-primitive cast from an enum to an integer type
//! is correctly disallowed, even when a `From` implementation exists for that enum.

//@ run-rustfix

#![allow(dead_code, unused_variables)]

enum NonNullary {
    Nullary,
    Other(isize),
}

impl From<NonNullary> for isize {
    fn from(val: NonNullary) -> isize {
        match val {
            NonNullary::Nullary => 0,
            NonNullary::Other(i) => i,
        }
    }
}

fn main() {
    let v = NonNullary::Nullary;
    let val = v as isize;
    //~^ ERROR non-primitive cast: `NonNullary` as `isize` [E0605]
    //~| HELP consider using the `From` trait instead
}