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