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