-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI test cleanup: Extract expect_fun_call tests
Note that the new stderr file does not include a `shadow-unrelated` error, because the new UI test file does not use `#![warn(clippy::all)]`
- Loading branch information
Showing
4 changed files
with
134 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![warn(clippy::expect_fun_call)] | ||
#![allow(clippy::useless_format)] | ||
|
||
/// Checks implementation of the `EXPECT_FUN_CALL` lint | ||
fn expect_fun_call() { | ||
struct Foo; | ||
|
||
impl Foo { | ||
fn new() -> Self { Foo } | ||
|
||
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()); | ||
|
||
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()); | ||
|
||
//Issue #2979 - this should not lint | ||
let msg = "bar"; | ||
Some("foo").expect(msg); | ||
|
||
Some("foo").expect({ &format!("error") }); | ||
Some("foo").expect(format!("error").as_ref()); | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
error: use of `expect` followed by a function call | ||
--> $DIR/expect_fun_call.rs:34:26 | ||
| | ||
34 | with_none_and_format.expect(&format!("Error {}: fake error", error_code)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))` | ||
| | ||
= note: `-D clippy::expect-fun-call` implied by `-D warnings` | ||
|
||
error: use of `expect` followed by a function call | ||
--> $DIR/expect_fun_call.rs:37:26 | ||
| | ||
37 | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))` | ||
|
||
error: use of `expect` followed by a function call | ||
--> $DIR/expect_fun_call.rs:47:25 | ||
| | ||
47 | with_err_and_format.expect(&format!("Error {}: fake error", error_code)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))` | ||
|
||
error: use of `expect` followed by a function call | ||
--> $DIR/expect_fun_call.rs:50:25 | ||
| | ||
50 | with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))` | ||
|
||
error: use of `expect` followed by a function call | ||
--> $DIR/expect_fun_call.rs:65:17 | ||
| | ||
65 | Some("foo").expect({ &format!("error") }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { let msg = { &format!("error") }; panic!(msg) }))` | ||
|
||
error: use of `expect` followed by a function call | ||
--> $DIR/expect_fun_call.rs:66:17 | ||
| | ||
66 | Some("foo").expect(format!("error").as_ref()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("error"))` | ||
|
||
error: aborting due to 6 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters