]> git.lizzy.rs Git - rust.git/blob - tests/ui/fallible_impl_from.rs
Auto merge of #4478 - tsurai:master, r=flip1995
[rust.git] / tests / ui / fallible_impl_from.rs
1 #![deny(clippy::fallible_impl_from)]
2
3 // docs example
4 struct Foo(i32);
5 impl From<String> for Foo {
6     fn from(s: String) -> Self {
7         Foo(s.parse().unwrap())
8     }
9 }
10
11 struct Valid(Vec<u8>);
12
13 impl<'a> From<&'a str> for Valid {
14     fn from(s: &'a str) -> Valid {
15         Valid(s.to_owned().into_bytes())
16     }
17 }
18 impl From<usize> for Valid {
19     fn from(i: usize) -> Valid {
20         Valid(Vec::with_capacity(i))
21     }
22 }
23
24 struct Invalid;
25
26 impl From<usize> for Invalid {
27     fn from(i: usize) -> Invalid {
28         if i != 42 {
29             panic!();
30         }
31         Invalid
32     }
33 }
34
35 impl From<Option<String>> for Invalid {
36     fn from(s: Option<String>) -> Invalid {
37         let s = s.unwrap();
38         if !s.is_empty() {
39             panic!(42);
40         } else if s.parse::<u32>().unwrap() != 42 {
41             panic!("{:?}", s);
42         }
43         Invalid
44     }
45 }
46
47 trait ProjStrTrait {
48     type ProjString;
49 }
50 impl<T> ProjStrTrait for Box<T> {
51     type ProjString = String;
52 }
53 impl<'a> From<&'a mut <Box<u32> as ProjStrTrait>::ProjString> for Invalid {
54     fn from(s: &'a mut <Box<u32> as ProjStrTrait>::ProjString) -> Invalid {
55         if s.parse::<u32>().ok().unwrap() != 42 {
56             panic!("{:?}", s);
57         }
58         Invalid
59     }
60 }
61
62 struct Unreachable;
63
64 impl From<String> for Unreachable {
65     fn from(s: String) -> Unreachable {
66         if s.is_empty() {
67             return Unreachable;
68         }
69         match s.chars().next() {
70             Some(_) => Unreachable,
71             None => unreachable!(), // do not lint the unreachable macro
72         }
73     }
74 }
75
76 fn main() {}