2 #![warn(clippy::expect_fun_call)]
3 #![allow(clippy::to_string_in_format_args, clippy::uninlined_format_args)]
5 /// Checks implementation of the `EXPECT_FUN_CALL` lint
21 fn expect(&self, msg: &str) {
26 let with_some = Some("value");
27 with_some.expect("error");
29 let with_none: Option<i32> = None;
30 with_none.expect("error");
32 let error_code = 123_i32;
33 let with_none_and_format: Option<i32> = None;
34 with_none_and_format.unwrap_or_else(|| panic!("Error {}: fake error", error_code));
36 let with_none_and_as_str: Option<i32> = None;
37 with_none_and_as_str.unwrap_or_else(|| panic!("Error {}: fake error", error_code));
39 let with_none_and_format_with_macro: Option<i32> = None;
40 with_none_and_format_with_macro.unwrap_or_else(|| panic!("Error {}: fake error", one!()));
42 let with_ok: Result<(), ()> = Ok(());
43 with_ok.expect("error");
45 let with_err: Result<(), ()> = Err(());
46 with_err.expect("error");
48 let error_code = 123_i32;
49 let with_err_and_format: Result<(), ()> = Err(());
50 with_err_and_format.unwrap_or_else(|_| panic!("Error {}: fake error", error_code));
52 let with_err_and_as_str: Result<(), ()> = Err(());
53 with_err_and_as_str.unwrap_or_else(|_| panic!("Error {}: fake error", error_code));
55 let with_dummy_type = Foo::new();
56 with_dummy_type.expect("another test string");
58 let with_dummy_type_and_format = Foo::new();
59 with_dummy_type_and_format.expect(&format!("Error {}: fake error", error_code));
61 let with_dummy_type_and_as_str = Foo::new();
62 with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
65 Some("foo").unwrap_or_else(|| panic!("{} {}", 1, 2));
67 //Issue #2979 - this should not lint
70 Some("foo").expect(msg);
74 fn get_string() -> String {
78 fn get_static_str() -> &'static str {
82 fn get_non_static_str(_: &u32) -> &str {
86 Some("foo").unwrap_or_else(|| { panic!("{}", get_string()) });
87 Some("foo").unwrap_or_else(|| { panic!("{}", get_string()) });
88 Some("foo").unwrap_or_else(|| { panic!("{}", get_string()) });
90 Some("foo").unwrap_or_else(|| { panic!("{}", get_static_str()) });
91 Some("foo").unwrap_or_else(|| { panic!("{}", get_non_static_str(&0).to_string()) });
95 Some(true).unwrap_or_else(|| panic!("key {}, {}", 1, 2));
97 //Issue #4912 - the receiver is a &Option
101 opt_ref.unwrap_or_else(|| panic!("{:?}", opt_ref));
104 let format_capture: Option<i32> = None;
105 format_capture.unwrap_or_else(|| panic!("{error_code}"));
107 let format_capture_and_value: Option<i32> = None;
108 format_capture_and_value.unwrap_or_else(|| panic!("{error_code}, {}", 1));