forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
expect_fun_call
lint suggestions
This commit corrects some bad suggestions produced by the `expect_fun_call` lint and enables `rust-fix` checking on the tests. Addresses rust-lang#3630
- Loading branch information
Michael Wright
authored and
Grzegorz
committed
Feb 5, 2019
1 parent
1c091b3
commit 3c96e89
Showing
4 changed files
with
238 additions
and
116 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
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,84 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::expect_fun_call)] | ||
|
||
/// Checks implementation of the `EXPECT_FUN_CALL` lint | ||
|
||
fn main() { | ||
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.unwrap_or_else(|| panic!("Error {}: fake error", error_code)); | ||
|
||
let with_none_and_as_str: Option<i32> = None; | ||
with_none_and_as_str.unwrap_or_else(|| panic!("Error {}: fake error", error_code)); | ||
|
||
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.unwrap_or_else(|_| panic!("Error {}: fake error", error_code)); | ||
|
||
let with_err_and_as_str: Result<(), ()> = Err(()); | ||
with_err_and_as_str.unwrap_or_else(|_| panic!("Error {}: fake error", error_code)); | ||
|
||
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 #2937 | ||
Some("foo").unwrap_or_else(|| panic!("{} {}", 1, 2)); | ||
|
||
//Issue #2979 - this should not lint | ||
{ | ||
let msg = "bar"; | ||
Some("foo").expect(msg); | ||
} | ||
|
||
{ | ||
fn get_string() -> String { | ||
"foo".to_string() | ||
} | ||
|
||
fn get_static_str() -> &'static str { | ||
"foo" | ||
} | ||
|
||
fn get_non_static_str(_: &u32) -> &str { | ||
"foo" | ||
} | ||
|
||
Some("foo").unwrap_or_else(|| { panic!(get_string()) }); | ||
Some("foo").unwrap_or_else(|| { panic!(get_string()) }); | ||
Some("foo").unwrap_or_else(|| { panic!(get_string()) }); | ||
|
||
Some("foo").unwrap_or_else(|| { panic!(get_static_str()) }); | ||
Some("foo").unwrap_or_else(|| { panic!(get_non_static_str(&0).to_string()) }); | ||
} | ||
} |
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
Oops, something went wrong.