rust/tests/ui/unnecessary_literal_unwrap.rs

50 lines
1.3 KiB
Rust
Raw Normal View History

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