rust/tests/ui/unnecessary_literal_unwrap_unfixable.rs

117 lines
3.8 KiB
Rust
Raw Normal View History

2023-02-16 09:30:09 -06:00
#![warn(clippy::unnecessary_literal_unwrap)]
2023-02-16 17:14:20 -06:00
#![allow(unreachable_code)]
2023-05-31 14:09:12 -05:00
#![allow(clippy::unnecessary_lazy_evaluations, clippy::let_unit_value)]
2023-02-16 09:30:09 -06:00
2023-02-16 17:14:20 -06:00
fn unwrap_option_some() {
2023-02-16 09:30:09 -06:00
let val = Some(1);
let _val2 = val.unwrap();
2023-02-16 17:14:20 -06:00
let _val2 = val.expect("this never happens");
}
2023-05-31 14:09:12 -05:00
fn unwrap_option_some_context() {
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap();
let _val = Some::<usize>([1, 2, 3].iter().sum()).expect("this never happens");
let val = Some::<usize>([1, 2, 3].iter().sum());
let _val2 = val.unwrap();
let _val2 = val.expect("this never happens");
}
2023-02-16 17:14:20 -06:00
fn unwrap_option_none() {
2023-05-31 14:09:12 -05:00
let val = None::<()>;
let _val2 = val.unwrap();
let _val2 = val.expect("this always happens");
2023-02-16 17:14:20 -06:00
}
fn unwrap_result_ok() {
2023-05-31 14:09:12 -05:00
let val = Ok::<_, ()>(1);
let _val2 = val.unwrap();
let _val2 = val.expect("this never happens");
let _val2 = val.unwrap_err();
let _val2 = val.expect_err("this always happens");
}
fn unwrap_result_ok_context() {
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap();
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).expect("this never happens");
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_err();
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).expect_err("this always happens");
let val = Ok::<usize, ()>([1, 2, 3].iter().sum());
2023-02-16 17:14:20 -06:00
let _val2 = val.unwrap();
let _val2 = val.expect("this never happens");
2023-05-31 14:09:12 -05:00
let _val2 = val.unwrap_err();
let _val2 = val.expect_err("this always happens");
2023-02-16 17:14:20 -06:00
}
fn unwrap_result_err() {
2023-05-31 14:09:12 -05:00
let val = Err::<(), _>(1);
let _val2 = val.unwrap_err();
let _val2 = val.expect_err("this never happens");
let _val2 = val.unwrap();
let _val2 = val.expect("this always happens");
}
fn unwrap_result_err_context() {
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).unwrap_err();
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).expect_err("this never happens");
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).unwrap();
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).expect("this always happens");
let val = Err::<(), usize>([1, 2, 3].iter().sum());
2023-02-16 17:14:20 -06:00
let _val2 = val.unwrap_err();
let _val2 = val.expect_err("this never happens");
2023-05-31 14:09:12 -05:00
let _val2 = val.unwrap();
let _val2 = val.expect("this always happens");
2023-02-16 17:14:20 -06:00
}
fn unwrap_methods_option() {
let val = Some(1);
let _val2 = val.unwrap_or(2);
let _val2 = val.unwrap_or_default();
let _val2 = val.unwrap_or_else(|| 2);
}
2023-05-31 14:09:12 -05:00
fn unwrap_methods_option_context() {
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or(2);
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or_default();
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or_else(|| 2);
let val = Some::<usize>([1, 2, 3].iter().sum());
let _val2 = val.unwrap_or(2);
let _val2 = val.unwrap_or_default();
let _val2 = val.unwrap_or_else(|| 2);
}
2023-02-16 17:14:20 -06:00
fn unwrap_methods_result() {
2023-05-31 14:09:12 -05:00
let val = Ok::<_, ()>(1);
let _val2 = val.unwrap_or(2);
let _val2 = val.unwrap_or_default();
let _val2 = val.unwrap_or_else(|_| 2);
}
fn unwrap_methods_result_context() {
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or(2);
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or_default();
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or_else(|_| 2);
let val = Ok::<usize, ()>([1, 2, 3].iter().sum());
2023-02-16 17:14:20 -06:00
let _val2 = val.unwrap_or(2);
let _val2 = val.unwrap_or_default();
let _val2 = val.unwrap_or_else(|_| 2);
}
fn main() {
unwrap_option_some();
2023-05-31 14:09:12 -05:00
unwrap_option_some_context();
2023-02-16 17:14:20 -06:00
unwrap_option_none();
unwrap_result_ok();
2023-05-31 14:09:12 -05:00
unwrap_result_ok_context();
2023-02-16 17:14:20 -06:00
unwrap_result_err();
2023-05-31 14:09:12 -05:00
unwrap_result_err_context();
2023-02-16 17:14:20 -06:00
unwrap_methods_option();
2023-05-31 14:09:12 -05:00
unwrap_methods_option_context();
2023-02-16 17:14:20 -06:00
unwrap_methods_result();
2023-05-31 14:09:12 -05:00
unwrap_methods_result_context();
2023-02-16 09:30:09 -06:00
}