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