rust/tests/ui/unnecessary_literal_unwrap.rs

79 lines
2.1 KiB
Rust
Raw Normal View History

2023-02-16 23:14:20 +00:00
//@run-rustfix
2023-02-16 11:32:12 +00:00
#![warn(clippy::unnecessary_literal_unwrap)]
2023-02-16 22:31:52 +00:00
#![allow(unreachable_code)]
2023-05-31 19:09:12 +00:00
#![allow(
clippy::unnecessary_lazy_evaluations,
clippy::diverging_sub_expression,
clippy::let_unit_value,
clippy::no_effect
)]
2023-02-16 11:32:12 +00:00
2023-02-16 22:31:52 +00:00
fn unwrap_option_some() {
2023-02-16 14:37:18 +00:00
let _val = Some(1).unwrap();
let _val = Some(1).expect("this never happens");
2023-05-31 19:09:12 +00:00
Some(1).unwrap();
Some(1).expect("this never happens");
2023-02-16 14:37:18 +00:00
}
2023-02-16 22:31:52 +00:00
fn unwrap_option_none() {
2023-05-31 19:09:12 +00:00
let _val = None::<()>.unwrap();
let _val = None::<()>.expect("this always happens");
None::<()>.unwrap();
None::<()>.expect("this always happens");
2023-02-16 22:31:52 +00:00
}
2023-02-16 17:15:19 +00:00
fn unwrap_result_ok() {
2023-05-31 19:09:12 +00:00
let _val = Ok::<_, ()>(1).unwrap();
let _val = Ok::<_, ()>(1).expect("this never happens");
let _val = Ok::<_, ()>(1).unwrap_err();
let _val = Ok::<_, ()>(1).expect_err("this always happens");
Ok::<_, ()>(1).unwrap();
Ok::<_, ()>(1).expect("this never happens");
Ok::<_, ()>(1).unwrap_err();
Ok::<_, ()>(1).expect_err("this always happens");
2023-02-16 17:15:19 +00:00
}
fn unwrap_result_err() {
2023-05-31 19:09:12 +00:00
let _val = Err::<(), _>(1).unwrap_err();
let _val = Err::<(), _>(1).expect_err("this never happens");
let _val = Err::<(), _>(1).unwrap();
let _val = Err::<(), _>(1).expect("this always happens");
Err::<(), _>(1).unwrap_err();
Err::<(), _>(1).expect_err("this never happens");
Err::<(), _>(1).unwrap();
Err::<(), _>(1).expect("this always happens");
2023-02-16 11:32:12 +00:00
}
2023-02-16 14:43:41 +00:00
fn unwrap_methods_option() {
let _val = Some(1).unwrap_or(2);
let _val = Some(1).unwrap_or_default();
2023-02-16 23:14:20 +00:00
let _val = Some(1).unwrap_or_else(|| 2);
2023-05-31 19:09:12 +00:00
Some(1).unwrap_or(2);
Some(1).unwrap_or_default();
Some(1).unwrap_or_else(|| 2);
2023-02-16 14:43:41 +00:00
}
fn unwrap_methods_result() {
2023-05-31 19:09:12 +00:00
let _val = Ok::<_, ()>(1).unwrap_or(2);
let _val = Ok::<_, ()>(1).unwrap_or_default();
let _val = Ok::<_, ()>(1).unwrap_or_else(|_| 2);
Ok::<_, ()>(1).unwrap_or(2);
Ok::<_, ()>(1).unwrap_or_default();
Ok::<_, ()>(1).unwrap_or_else(|_| 2);
2023-02-16 14:43:41 +00:00
}
2023-02-16 11:32:12 +00:00
fn main() {
2023-02-16 22:31:52 +00:00
unwrap_option_some();
unwrap_option_none();
2023-02-16 17:15:19 +00:00
unwrap_result_ok();
unwrap_result_err();
2023-02-16 14:43:41 +00:00
unwrap_methods_option();
unwrap_methods_result();
2023-02-16 11:32:12 +00:00
}