]> git.lizzy.rs Git - rust.git/blob - tests/ui/expect_fun_call.fixed
Merge commit '7ea7cd165ad6705603852771bf82cc2fd6560db5' into clippyup2
[rust.git] / tests / ui / expect_fun_call.fixed
1 // run-rustfix
2
3 #![warn(clippy::expect_fun_call)]
4
5 /// Checks implementation of the `EXPECT_FUN_CALL` lint
6
7 fn main() {
8     struct Foo;
9
10     impl Foo {
11         fn new() -> Self {
12             Foo
13         }
14
15         fn expect(&self, msg: &str) {
16             panic!("{}", msg)
17         }
18     }
19
20     let with_some = Some("value");
21     with_some.expect("error");
22
23     let with_none: Option<i32> = None;
24     with_none.expect("error");
25
26     let error_code = 123_i32;
27     let with_none_and_format: Option<i32> = None;
28     with_none_and_format.unwrap_or_else(|| panic!("Error {}: fake error", error_code));
29
30     let with_none_and_as_str: Option<i32> = None;
31     with_none_and_as_str.unwrap_or_else(|| panic!("Error {}: fake error", error_code));
32
33     let with_ok: Result<(), ()> = Ok(());
34     with_ok.expect("error");
35
36     let with_err: Result<(), ()> = Err(());
37     with_err.expect("error");
38
39     let error_code = 123_i32;
40     let with_err_and_format: Result<(), ()> = Err(());
41     with_err_and_format.unwrap_or_else(|_| panic!("Error {}: fake error", error_code));
42
43     let with_err_and_as_str: Result<(), ()> = Err(());
44     with_err_and_as_str.unwrap_or_else(|_| panic!("Error {}: fake error", error_code));
45
46     let with_dummy_type = Foo::new();
47     with_dummy_type.expect("another test string");
48
49     let with_dummy_type_and_format = Foo::new();
50     with_dummy_type_and_format.expect(&format!("Error {}: fake error", error_code));
51
52     let with_dummy_type_and_as_str = Foo::new();
53     with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
54
55     //Issue #2937
56     Some("foo").unwrap_or_else(|| panic!("{} {}", 1, 2));
57
58     //Issue #2979 - this should not lint
59     {
60         let msg = "bar";
61         Some("foo").expect(msg);
62     }
63
64     {
65         fn get_string() -> String {
66             "foo".to_string()
67         }
68
69         fn get_static_str() -> &'static str {
70             "foo"
71         }
72
73         fn get_non_static_str(_: &u32) -> &str {
74             "foo"
75         }
76
77         Some("foo").unwrap_or_else(|| { panic!(get_string()) });
78         Some("foo").unwrap_or_else(|| { panic!(get_string()) });
79         Some("foo").unwrap_or_else(|| { panic!(get_string()) });
80
81         Some("foo").unwrap_or_else(|| { panic!(get_static_str()) });
82         Some("foo").unwrap_or_else(|| { panic!(get_non_static_str(&0).to_string()) });
83     }
84
85     //Issue #3839
86     Some(true).unwrap_or_else(|| panic!("key {}, {}", 1, 2));
87
88     //Issue #4912 - the receiver is a &Option
89     {
90         let opt = Some(1);
91         let opt_ref = &opt;
92         opt_ref.unwrap_or_else(|| panic!("{:?}", opt_ref));
93     }
94 }