|
| 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 { Foo } |
| 20 | + |
| 21 | + fn expect(&self, msg: &str) { |
| 22 | + panic!("{}", msg) |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + let with_some = Some("value"); |
| 27 | + with_some.expect("error"); |
| 28 | + |
| 29 | + let with_none: Option<i32> = None; |
| 30 | + with_none.expect("error"); |
| 31 | + |
| 32 | + let error_code = 123_i32; |
| 33 | + let with_none_and_format: Option<i32> = None; |
| 34 | + with_none_and_format.expect(&format!("Error {}: fake error", error_code)); |
| 35 | + |
| 36 | + let with_none_and_as_str: Option<i32> = None; |
| 37 | + with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str()); |
| 38 | + |
| 39 | + let with_ok: Result<(), ()> = Ok(()); |
| 40 | + with_ok.expect("error"); |
| 41 | + |
| 42 | + let with_err: Result<(), ()> = Err(()); |
| 43 | + with_err.expect("error"); |
| 44 | + |
| 45 | + let error_code = 123_i32; |
| 46 | + let with_err_and_format: Result<(), ()> = Err(()); |
| 47 | + with_err_and_format.expect(&format!("Error {}: fake error", error_code)); |
| 48 | + |
| 49 | + let with_err_and_as_str: Result<(), ()> = Err(()); |
| 50 | + with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str()); |
| 51 | + |
| 52 | + let with_dummy_type = Foo::new(); |
| 53 | + with_dummy_type.expect("another test string"); |
| 54 | + |
| 55 | + let with_dummy_type_and_format = Foo::new(); |
| 56 | + with_dummy_type_and_format.expect(&format!("Error {}: fake error", error_code)); |
| 57 | + |
| 58 | + let with_dummy_type_and_as_str = Foo::new(); |
| 59 | + with_dummy_type_and_as_str.expect(format!("Error {}: fake error", error_code).as_str()); |
| 60 | + |
| 61 | + //Issue #2979 - this should not lint |
| 62 | + let msg = "bar"; |
| 63 | + Some("foo").expect(msg); |
| 64 | + |
| 65 | + Some("foo").expect({ &format!("error") }); |
| 66 | + Some("foo").expect(format!("error").as_ref()); |
| 67 | +} |
| 68 | + |
| 69 | +fn main() {} |
0 commit comments