1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#![warn(clippy::expect_fun_call)]
#![allow(
clippy::to_string_in_format_args,
clippy::uninlined_format_args,
clippy::unnecessary_literal_unwrap
)]
macro_rules! one {
() => {
1
};
}
fn main() {
struct Foo;
impl Foo {
fn new() -> Self {
Foo
}
fn expect(&self, msg: &str) {
panic!("{}", msg)
}
}
let with_some = Some("value");
with_some.expect("error");
let with_none: Option<i32> = None;
with_none.expect("error");
let error_code = 123_i32;
let with_none_and_format: Option<i32> = None;
with_none_and_format.expect(&format!("Error {}: fake error", error_code));
//~^ expect_fun_call
let with_none_and_as_str: Option<i32> = None;
with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
//~^ expect_fun_call
let with_none_and_format_with_macro: Option<i32> = None;
with_none_and_format_with_macro.expect(format!("Error {}: fake error", one!()).as_str());
//~^ expect_fun_call
let with_ok: Result<(), ()> = Ok(());
with_ok.expect("error");
let with_err: Result<(), ()> = Err(());
with_err.expect("error");
let error_code = 123_i32;
let with_err_and_format: Result<(), ()> = Err(());
with_err_and_format.expect(&format!("Error {}: fake error", error_code));
//~^ expect_fun_call
let with_err_and_as_str: Result<(), ()> = Err(());
with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
//~^ expect_fun_call
let with_dummy_type = Foo::new();
with_dummy_type.expect("another test string");
let with_dummy_type_and_format = Foo::new();
with_dummy_type_and_format.expect(&format!("Error {}: fake error", error_code));
let with_dummy_type_and_as_str = Foo::new();
with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
//Issue #2937
Some("foo").expect(format!("{} {}", 1, 2).as_ref());
//~^ expect_fun_call
//Issue #2979 - this should not lint
{
let msg = "bar";
Some("foo").expect(msg);
}
{
fn get_string() -> String {
"foo".to_string()
}
fn get_static_str() -> &'static str {
"foo"
}
fn get_non_static_str(_: &u32) -> &str {
"foo"
}
const fn const_evaluable() -> &'static str {
"foo"
}
Some("foo").expect(&get_string());
//~^ expect_fun_call
Some("foo").expect(get_string().as_ref());
//~^ expect_fun_call
Some("foo").expect(get_string().as_str());
//~^ expect_fun_call
Some("foo").expect(get_static_str());
//~^ expect_fun_call
Some("foo").expect(get_non_static_str(&0));
//~^ expect_fun_call
Some("foo").expect(const_evaluable());
//~^ expect_fun_call
const {
Some("foo").expect(const_evaluable());
}
Some("foo").expect(const { const_evaluable() });
}
//Issue #3839
Some(true).expect(&format!("key {}, {}", 1, 2));
//~^ expect_fun_call
//Issue #4912 - the receiver is a &Option
{
let opt = Some(1);
let opt_ref = &opt;
opt_ref.expect(&format!("{:?}", opt_ref));
//~^ expect_fun_call
}
let format_capture: Option<i32> = None;
format_capture.expect(&format!("{error_code}"));
//~^ expect_fun_call
let format_capture_and_value: Option<i32> = None;
format_capture_and_value.expect(&format!("{error_code}, {}", 1));
//~^ expect_fun_call
// Issue #15056
let a = false;
Some(5).expect(if a { "a" } else { "b" });
let return_in_expect: Option<i32> = None;
return_in_expect.expect(if true {
"Error"
} else {
return;
});
}
|