#![deny(clippy::fallible_impl_from)] #![allow(clippy::uninlined_format_args)] // docs example struct Foo(i32); impl From for Foo { //~^ ERROR: consider implementing `TryFrom` instead fn from(s: String) -> Self { Foo(s.parse().unwrap()) } } struct Valid(Vec); impl<'a> From<&'a str> for Valid { fn from(s: &'a str) -> Valid { Valid(s.to_owned().into_bytes()) } } impl From for Valid { fn from(i: usize) -> Valid { Valid(Vec::with_capacity(i)) } } struct Invalid; impl From for Invalid { //~^ ERROR: consider implementing `TryFrom` instead fn from(i: usize) -> Invalid { if i != 42 { panic!(); } Invalid } } impl From> for Invalid { //~^ ERROR: consider implementing `TryFrom` instead fn from(s: Option) -> Invalid { let s = s.unwrap(); if !s.is_empty() { panic!("42"); } else if s.parse::().unwrap() != 42 { panic!("{:?}", s); } Invalid } } trait ProjStrTrait { type ProjString; } impl ProjStrTrait for Box { type ProjString = String; } impl<'a> From<&'a mut as ProjStrTrait>::ProjString> for Invalid { //~^ ERROR: consider implementing `TryFrom` instead fn from(s: &'a mut as ProjStrTrait>::ProjString) -> Invalid { if s.parse::().ok().unwrap() != 42 { panic!("{:?}", s); } Invalid } } struct Unreachable; impl From for Unreachable { fn from(s: String) -> Unreachable { if s.is_empty() { return Unreachable; } match s.chars().next() { Some(_) => Unreachable, None => unreachable!(), // do not lint the unreachable macro } } } fn main() {}