diff options
Diffstat (limited to '')
-rw-r--r-- | src/tools/clippy/src/docs/fallible_impl_from.txt | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/tools/clippy/src/docs/fallible_impl_from.txt b/src/tools/clippy/src/docs/fallible_impl_from.txt new file mode 100644 index 000000000..588a5bb10 --- /dev/null +++ b/src/tools/clippy/src/docs/fallible_impl_from.txt @@ -0,0 +1,32 @@ +### What it does +Checks for impls of `From<..>` that contain `panic!()` or `unwrap()` + +### Why is this bad? +`TryFrom` should be used if there's a possibility of failure. + +### Example +``` +struct Foo(i32); + +impl From<String> for Foo { + fn from(s: String) -> Self { + Foo(s.parse().unwrap()) + } +} +``` + +Use instead: +``` +struct Foo(i32); + +impl TryFrom<String> for Foo { + type Error = (); + fn try_from(s: String) -> Result<Self, Self::Error> { + if let Ok(parsed) = s.parse() { + Ok(Foo(parsed)) + } else { + Err(()) + } + } +} +```
\ No newline at end of file |