2022-08-11 12:42:16 -05:00
|
|
|
#![warn(clippy::unwrap_used, clippy::expect_used)]
|
2023-07-02 07:35:19 -05:00
|
|
|
#![allow(clippy::unnecessary_literal_unwrap)]
|
2023-08-11 07:05:13 -05:00
|
|
|
#![feature(never_type)]
|
|
|
|
|
|
|
|
use std::convert::Infallible;
|
2022-08-11 12:42:16 -05:00
|
|
|
|
2022-08-31 08:24:45 -05: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 12:42:16 -05:00
|
|
|
fn main() {
|
|
|
|
Some(3).unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on an `Option` value
|
2022-08-11 12:42:16 -05:00
|
|
|
Some(3).expect("Hello world!");
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: used `expect()` on an `Option` value
|
2022-08-11 12:42:16 -05:00
|
|
|
|
2022-08-31 08:24:45 -05:00
|
|
|
// Don't trigger on unwrap_err on an option
|
|
|
|
Some(3).unwrap_err();
|
2024-08-08 12:13:50 -05:00
|
|
|
Some(3).expect_err("Hello none!");
|
2022-08-31 08:24:45 -05:00
|
|
|
|
2023-08-11 07:05:13 -05: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 12:42:16 -05:00
|
|
|
let a: Result<i32, i32> = Ok(3);
|
|
|
|
a.unwrap();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on a `Result` value
|
2022-08-11 12:42:16 -05:00
|
|
|
a.expect("Hello world!");
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: used `expect()` on a `Result` value
|
2022-08-31 08:24:45 -05:00
|
|
|
a.unwrap_err();
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: used `unwrap_err()` on a `Result` value
|
2022-08-31 08:24:45 -05:00
|
|
|
a.expect_err("Hello error!");
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: used `expect_err()` on a `Result` value
|
2022-08-11 12:42:16 -05:00
|
|
|
}
|