]> git.lizzy.rs Git - rust.git/blob - tests/ui/expect_fun_call.rs
Merge branch 'master' into rustfmt_tests
[rust.git] / tests / ui / expect_fun_call.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::expect_fun_call)]
11 #![allow(clippy::useless_format)]
12
13 /// Checks implementation of the `EXPECT_FUN_CALL` lint
14
15 fn expect_fun_call() {
16     struct Foo;
17
18     impl Foo {
19         fn new() -> Self {
20             Foo
21         }
22
23         fn expect(&self, msg: &str) {
24             panic!("{}", msg)
25         }
26     }
27
28     let with_some = Some("value");
29     with_some.expect("error");
30
31     let with_none: Option<i32> = None;
32     with_none.expect("error");
33
34     let error_code = 123_i32;
35     let with_none_and_format: Option<i32> = None;
36     with_none_and_format.expect(&format!("Error {}: fake error", error_code));
37
38     let with_none_and_as_str: Option<i32> = None;
39     with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
40
41     let with_ok: Result<(), ()> = Ok(());
42     with_ok.expect("error");
43
44     let with_err: Result<(), ()> = Err(());
45     with_err.expect("error");
46
47     let error_code = 123_i32;
48     let with_err_and_format: Result<(), ()> = Err(());
49     with_err_and_format.expect(&format!("Error {}: fake error", error_code));
50
51     let with_err_and_as_str: Result<(), ()> = Err(());
52     with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
53
54     let with_dummy_type = Foo::new();
55     with_dummy_type.expect("another test string");
56
57     let with_dummy_type_and_format = Foo::new();
58     with_dummy_type_and_format.expect(&format!("Error {}: fake error", error_code));
59
60     let with_dummy_type_and_as_str = Foo::new();
61     with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
62
63     //Issue #2979 - this should not lint
64     let msg = "bar";
65     Some("foo").expect(msg);
66
67     Some("foo").expect({ &format!("error") });
68     Some("foo").expect(format!("error").as_ref());
69 }
70
71 fn main() {}