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