fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

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