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.expect(&format!("Error {}: fake error", error_code));
36 let with_none_and_as_str: Option<i32> = None;
37 with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
39 let with_none_and_format_with_macro: Option<i32> = None;
40 with_none_and_format_with_macro.expect(format!("Error {}: fake error", one!()).as_str());
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.expect(&format!("Error {}: fake error", error_code));
52 let with_err_and_as_str: Result<(), ()> = Err(());
53 with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
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").expect(format!("{} {}", 1, 2).as_ref());
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").expect(&get_string());
87 Some("foo").expect(get_string().as_ref());
88 Some("foo").expect(get_string().as_str());
90 Some("foo").expect(get_static_str());
91 Some("foo").expect(get_non_static_str(&0));
95 Some(true).expect(&format!("key {}, {}", 1, 2));
97 //Issue #4912 - the receiver is a &Option
101 opt_ref.expect(&format!("{:?}", opt_ref));
104 let format_capture: Option<i32> = None;
105 format_capture.expect(&format!("{error_code}"));
107 let format_capture_and_value: Option<i32> = None;
108 format_capture_and_value.expect(&format!("{error_code}, {}", 1));