]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/fallible_impl_from.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / 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 //~^ ERROR: consider implementing `TryFrom` instead
8 fn from(s: String) -> Self {
9 Foo(s.parse().unwrap())
10 }
11 }
12
13 struct Valid(Vec<u8>);
14
15 impl<'a> From<&'a str> for Valid {
16 fn from(s: &'a str) -> Valid {
17 Valid(s.to_owned().into_bytes())
18 }
19 }
20 impl From<usize> for Valid {
21 fn from(i: usize) -> Valid {
22 Valid(Vec::with_capacity(i))
23 }
24 }
25
26 struct Invalid;
27
28 impl From<usize> for Invalid {
29 //~^ ERROR: consider implementing `TryFrom` instead
30 fn from(i: usize) -> Invalid {
31 if i != 42 {
32 panic!();
33 }
34 Invalid
35 }
36 }
37
38 impl From<Option<String>> for Invalid {
39 //~^ ERROR: consider implementing `TryFrom` instead
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 //~^ ERROR: consider implementing `TryFrom` instead
59 fn from(s: &'a mut <Box<u32> as ProjStrTrait>::ProjString) -> Invalid {
60 if s.parse::<u32>().ok().unwrap() != 42 {
61 panic!("{:?}", s);
62 }
63 Invalid
64 }
65 }
66
67 struct Unreachable;
68
69 impl From<String> for Unreachable {
70 fn from(s: String) -> Unreachable {
71 if s.is_empty() {
72 return Unreachable;
73 }
74 match s.chars().next() {
75 Some(_) => Unreachable,
76 None => unreachable!(), // do not lint the unreachable macro
77 }
78 }
79 }
80
81 fn main() {}