1 // run-rustfix 2 3 #![warn(clippy::unwrap_or_else_default)] 4 #![allow(dead_code)] 5 #![allow(clippy::unnecessary_wraps)] 6 7 /// Checks implementation of the `UNWRAP_OR_ELSE_DEFAULT` lint. unwrap_or_else_default()8fn unwrap_or_else_default() { 9 struct Foo; 10 11 impl Foo { 12 fn new() -> Foo { 13 Foo 14 } 15 16 // fake default, we should not trigger on this 17 fn default() -> Foo { 18 Foo 19 } 20 } 21 22 struct HasDefaultAndDuplicate; 23 24 impl HasDefaultAndDuplicate { 25 fn default() -> Self { 26 HasDefaultAndDuplicate 27 } 28 } 29 30 impl Default for HasDefaultAndDuplicate { 31 fn default() -> Self { 32 HasDefaultAndDuplicate 33 } 34 } 35 36 enum Enum { 37 A(), 38 } 39 40 fn make<T, V>(_: V) -> T { 41 unimplemented!(); 42 } 43 44 let with_enum = Some(Enum::A()); 45 with_enum.unwrap_or_else(Enum::A); 46 47 let with_new = Some(vec![1]); 48 with_new.unwrap_or_else(Vec::new); 49 50 let with_err: Result<_, ()> = Ok(vec![1]); 51 with_err.unwrap_or_else(make); 52 53 // should not be changed 54 let with_fake_default = None::<Foo>; 55 with_fake_default.unwrap_or_else(Foo::default); 56 57 // should not be changed 58 let with_fake_default2 = None::<HasDefaultAndDuplicate>; 59 with_fake_default2.unwrap_or_else(<HasDefaultAndDuplicate>::default); 60 61 let with_real_default = None::<HasDefaultAndDuplicate>; 62 with_real_default.unwrap_or_else(<HasDefaultAndDuplicate as Default>::default); 63 64 let with_default_trait = Some(1); 65 with_default_trait.unwrap_or_else(Default::default); 66 67 let with_default_type = Some(1); 68 with_default_type.unwrap_or_else(u64::default); 69 } 70 main()71fn main() {} 72