2023-04-20 09:37:15 -05:00
|
|
|
//@run-rustfix
|
2018-10-30 15:21:23 -05:00
|
|
|
#![warn(clippy::expect_fun_call)]
|
2022-10-02 14:13:22 -05:00
|
|
|
#![allow(clippy::to_string_in_format_args, clippy::uninlined_format_args)]
|
2018-10-30 15:21:23 -05:00
|
|
|
|
|
|
|
/// Checks implementation of the `EXPECT_FUN_CALL` lint
|
|
|
|
|
2022-01-28 08:42:19 -06:00
|
|
|
macro_rules! one {
|
|
|
|
() => {
|
|
|
|
1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-23 22:58:53 -06:00
|
|
|
fn main() {
|
2018-10-30 15:21:23 -05:00
|
|
|
struct Foo;
|
|
|
|
|
|
|
|
impl Foo {
|
2018-12-09 16:26:16 -06:00
|
|
|
fn new() -> Self {
|
|
|
|
Foo
|
|
|
|
}
|
2018-10-30 15:21:23 -05:00
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
let with_none_and_as_str: Option<i32> = None;
|
|
|
|
with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
|
|
|
|
|
2022-01-28 08:42:19 -06:00
|
|
|
let with_none_and_format_with_macro: Option<i32> = None;
|
|
|
|
with_none_and_format_with_macro.expect(format!("Error {}: fake error", one!()).as_str());
|
|
|
|
|
2018-10-30 15:21:23 -05:00
|
|
|
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));
|
|
|
|
|
|
|
|
let with_err_and_as_str: Result<(), ()> = Err(());
|
|
|
|
with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2019-01-23 22:58:53 -06:00
|
|
|
//Issue #2937
|
|
|
|
Some("foo").expect(format!("{} {}", 1, 2).as_ref());
|
|
|
|
|
2018-10-30 15:21:23 -05:00
|
|
|
//Issue #2979 - this should not lint
|
2019-01-23 22:58:53 -06:00
|
|
|
{
|
|
|
|
let msg = "bar";
|
|
|
|
Some("foo").expect(msg);
|
|
|
|
}
|
2018-10-30 15:21:23 -05:00
|
|
|
|
2019-01-23 22:58:53 -06:00
|
|
|
{
|
|
|
|
fn get_string() -> String {
|
|
|
|
"foo".to_string()
|
|
|
|
}
|
2019-01-19 06:13:06 -06:00
|
|
|
|
2019-01-23 22:58:53 -06:00
|
|
|
fn get_static_str() -> &'static str {
|
|
|
|
"foo"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_non_static_str(_: &u32) -> &str {
|
|
|
|
"foo"
|
|
|
|
}
|
2018-10-30 15:21:23 -05:00
|
|
|
|
2019-01-23 22:58:53 -06:00
|
|
|
Some("foo").expect(&get_string());
|
|
|
|
Some("foo").expect(get_string().as_ref());
|
|
|
|
Some("foo").expect(get_string().as_str());
|
|
|
|
|
|
|
|
Some("foo").expect(get_static_str());
|
|
|
|
Some("foo").expect(get_non_static_str(&0));
|
|
|
|
}
|
2019-03-05 23:00:05 -06:00
|
|
|
|
|
|
|
//Issue #3839
|
|
|
|
Some(true).expect(&format!("key {}, {}", 1, 2));
|
2019-12-18 13:54:37 -06:00
|
|
|
|
|
|
|
//Issue #4912 - the receiver is a &Option
|
|
|
|
{
|
|
|
|
let opt = Some(1);
|
|
|
|
let opt_ref = &opt;
|
|
|
|
opt_ref.expect(&format!("{:?}", opt_ref));
|
|
|
|
}
|
2022-10-05 11:10:52 -05:00
|
|
|
|
|
|
|
let format_capture: Option<i32> = None;
|
|
|
|
format_capture.expect(&format!("{error_code}"));
|
|
|
|
|
|
|
|
let format_capture_and_value: Option<i32> = None;
|
|
|
|
format_capture_and_value.expect(&format!("{error_code}, {}", 1));
|
2019-01-23 22:58:53 -06:00
|
|
|
}
|