]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/expect_fun_call.rs
Rollup merge of #102764 - compiler-errors:issue-102762, r=jackh726
[rust.git] / src / tools / clippy / tests / ui / expect_fun_call.rs
1 // run-rustfix
2 #![warn(clippy::expect_fun_call)]
3 #![allow(clippy::to_string_in_format_args, clippy::uninlined_format_args)]
4
5 /// Checks implementation of the `EXPECT_FUN_CALL` lint
6
7 macro_rules! one {
8     () => {
9         1
10     };
11 }
12
13 fn main() {
14     struct Foo;
15
16     impl Foo {
17         fn new() -> Self {
18             Foo
19         }
20
21         fn expect(&self, msg: &str) {
22             panic!("{}", msg)
23         }
24     }
25
26     let with_some = Some("value");
27     with_some.expect("error");
28
29     let with_none: Option<i32> = None;
30     with_none.expect("error");
31
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));
35
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());
38
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());
41
42     let with_ok: Result<(), ()> = Ok(());
43     with_ok.expect("error");
44
45     let with_err: Result<(), ()> = Err(());
46     with_err.expect("error");
47
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));
51
52     let with_err_and_as_str: Result<(), ()> = Err(());
53     with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
54
55     let with_dummy_type = Foo::new();
56     with_dummy_type.expect("another test string");
57
58     let with_dummy_type_and_format = Foo::new();
59     with_dummy_type_and_format.expect(&format!("Error {}: fake error", error_code));
60
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());
63
64     //Issue #2937
65     Some("foo").expect(format!("{} {}", 1, 2).as_ref());
66
67     //Issue #2979 - this should not lint
68     {
69         let msg = "bar";
70         Some("foo").expect(msg);
71     }
72
73     {
74         fn get_string() -> String {
75             "foo".to_string()
76         }
77
78         fn get_static_str() -> &'static str {
79             "foo"
80         }
81
82         fn get_non_static_str(_: &u32) -> &str {
83             "foo"
84         }
85
86         Some("foo").expect(&get_string());
87         Some("foo").expect(get_string().as_ref());
88         Some("foo").expect(get_string().as_str());
89
90         Some("foo").expect(get_static_str());
91         Some("foo").expect(get_non_static_str(&0));
92     }
93
94     //Issue #3839
95     Some(true).expect(&format!("key {}, {}", 1, 2));
96
97     //Issue #4912 - the receiver is a &Option
98     {
99         let opt = Some(1);
100         let opt_ref = &opt;
101         opt_ref.expect(&format!("{:?}", opt_ref));
102     }
103
104     let format_capture: Option<i32> = None;
105     format_capture.expect(&format!("{error_code}"));
106
107     let format_capture_and_value: Option<i32> = None;
108     format_capture_and_value.expect(&format!("{error_code}, {}", 1));
109 }