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