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)]
|
2023-08-11 12:05:13 +00:00
|
|
|
#![feature(never_type)]
|
|
|
|
|
|
|
|
use std::convert::Infallible;
|
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();
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: used `unwrap()` on an `Option` value
|
2022-08-11 17:42:16 +00:00
|
|
|
Some(3).expect("Hello world!");
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: used `expect()` on an `Option` value
|
2022-08-11 17:42:16 +00:00
|
|
|
|
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!");
|
|
|
|
|
2023-08-11 12:05:13 +00:00
|
|
|
// Issue #11245: The `Err` variant can never be constructed so do not lint this.
|
|
|
|
let x: Result<(), !> = Ok(());
|
|
|
|
x.unwrap();
|
|
|
|
x.expect("is `!` (never)");
|
|
|
|
let x: Result<(), Infallible> = Ok(());
|
|
|
|
x.unwrap();
|
|
|
|
x.expect("is never-like (0 variants)");
|
|
|
|
|
2022-08-11 17:42:16 +00:00
|
|
|
let a: Result<i32, i32> = Ok(3);
|
|
|
|
a.unwrap();
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: used `unwrap()` on a `Result` value
|
2022-08-11 17:42:16 +00:00
|
|
|
a.expect("Hello world!");
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: used `expect()` on a `Result` value
|
2022-08-31 13:24:45 +00:00
|
|
|
a.unwrap_err();
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: used `unwrap_err()` on a `Result` value
|
2022-08-31 13:24:45 +00:00
|
|
|
a.expect_err("Hello error!");
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: used `expect_err()` on a `Result` value
|
2022-08-11 17:42:16 +00:00
|
|
|
}
|