about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/infallible_try_from.rs
blob: 6a1f12f824f5598595db0c5bac2610be7e955fdb (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
#![feature(never_type)]
#![warn(clippy::infallible_try_from)]

use std::convert::Infallible;

struct MyStruct(i32);

impl TryFrom<i8> for MyStruct {
    //~^ infallible_try_from
    type Error = !;
    fn try_from(other: i8) -> Result<Self, !> {
        Ok(Self(other.into()))
    }
}

impl TryFrom<i16> for MyStruct {
    //~^ infallible_try_from
    type Error = Infallible;
    fn try_from(other: i16) -> Result<Self, Infallible> {
        Ok(Self(other.into()))
    }
}

impl TryFrom<i64> for MyStruct {
    type Error = i64;
    fn try_from(other: i64) -> Result<Self, i64> {
        Ok(Self(i32::try_from(other).map_err(|_| other)?))
    }
}

fn main() {
    // test code goes here
}