]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/fallible_impl_from.txt
Auto merge of #97870 - eggyal:inplace_fold_spec, r=wesleywiser
[rust.git] / src / tools / clippy / src / docs / fallible_impl_from.txt
1 ### What it does
2 Checks for impls of `From<..>` that contain `panic!()` or `unwrap()`
3
4 ### Why is this bad?
5 `TryFrom` should be used if there's a possibility of failure.
6
7 ### Example
8 ```
9 struct Foo(i32);
10
11 impl From<String> for Foo {
12     fn from(s: String) -> Self {
13         Foo(s.parse().unwrap())
14     }
15 }
16 ```
17
18 Use instead:
19 ```
20 struct Foo(i32);
21
22 impl TryFrom<String> for Foo {
23     type Error = ();
24     fn try_from(s: String) -> Result<Self, Self::Error> {
25         if let Ok(parsed) = s.parse() {
26             Ok(Foo(parsed))
27         } else {
28             Err(())
29         }
30     }
31 }
32 ```