2022-08-11 17:42:16 +00:00
|
|
|
#![warn(clippy::unwrap_used, clippy::expect_used)]
|
2023-07-02 12:35:19 +00:00
|
|
|
#![allow(clippy::unnecessary_literal_unwrap)]
|
2022-08-11 17:42:16 +00:00
|
|
|
|
2022-08-31 13:24:45 +00:00
|
|
|
trait OptionExt {
|
|
|
|
type Item;
|
|
|
|
|
|
|
|
fn unwrap_err(self) -> Self::Item;
|
|
|
|
|
|
|
|
fn expect_err(self, msg: &str) -> Self::Item;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> OptionExt for Option<T> {
|
|
|
|
type Item = T;
|
|
|
|
fn unwrap_err(self) -> T {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expect_err(self, msg: &str) -> T {
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 17:42:16 +00:00
|
|
|
fn main() {
|
|
|
|
Some(3).unwrap();
|
|
|
|
Some(3).expect("Hello world!");
|
|
|
|
|
2022-08-31 13:24:45 +00:00
|
|
|
// Don't trigger on unwrap_err on an option
|
|
|
|
Some(3).unwrap_err();
|
|
|
|
Some(3).expect_err("Hellow none!");
|
|
|
|
|
2022-08-11 17:42:16 +00:00
|
|
|
let a: Result<i32, i32> = Ok(3);
|
|
|
|
a.unwrap();
|
|
|
|
a.expect("Hello world!");
|
2022-08-31 13:24:45 +00:00
|
|
|
a.unwrap_err();
|
|
|
|
a.expect_err("Hello error!");
|
2022-08-11 17:42:16 +00:00
|
|
|
}
|