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-07-27 06:40:22 -05:00
|
|
|
//@no-rustfix
|
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-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on `Some` value
|
2023-02-16 17:14:20 -06:00
|
|
|
let _val2 = val.expect("this never happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect()` on `Some` value
|
2023-02-16 17:14:20 -06:00
|
|
|
}
|
|
|
|
|
2023-05-31 14:09:12 -05:00
|
|
|
fn unwrap_option_some_context() {
|
|
|
|
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on `Some` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Some::<usize>([1, 2, 3].iter().sum()).expect("this never happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect()` on `Some` value
|
2023-05-31 14:09:12 -05:00
|
|
|
|
|
|
|
let val = Some::<usize>([1, 2, 3].iter().sum());
|
|
|
|
let _val2 = val.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on `Some` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.expect("this never happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect()` on `Some` value
|
2023-05-31 14:09:12 -05:00
|
|
|
}
|
|
|
|
|
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();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on `None` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.expect("this always happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect()` on `None` value
|
2023-07-05 21:13:37 -05:00
|
|
|
let _val3: u8 = None.unwrap_or_default();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_default()` on `None` value
|
2023-07-05 21:13:37 -05:00
|
|
|
None::<()>.unwrap_or_default();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_default()` on `None` value
|
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();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.expect("this never happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.unwrap_err();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_err()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.expect_err("this always happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect_err()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unwrap_result_ok_context() {
|
|
|
|
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).expect("this never happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_err();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_err()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).expect_err("this always happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect_err()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
|
|
|
|
let val = Ok::<usize, ()>([1, 2, 3].iter().sum());
|
2023-02-16 17:14:20 -06:00
|
|
|
let _val2 = val.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on `Ok` value
|
2023-02-16 17:14:20 -06:00
|
|
|
let _val2 = val.expect("this never happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.unwrap_err();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_err()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.expect_err("this always happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect_err()` on `Ok` value
|
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();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_err()` on `Err` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.expect_err("this never happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect_err()` on `Err` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on `Err` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.expect("this always happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect()` on `Err` value
|
2023-05-31 14:09:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unwrap_result_err_context() {
|
|
|
|
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).unwrap_err();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_err()` on `Err` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).expect_err("this never happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect_err()` on `Err` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on `Err` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).expect("this always happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect()` on `Err` value
|
2023-05-31 14:09:12 -05:00
|
|
|
|
|
|
|
let val = Err::<(), usize>([1, 2, 3].iter().sum());
|
2023-02-16 17:14:20 -06:00
|
|
|
let _val2 = val.unwrap_err();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_err()` on `Err` value
|
2023-02-16 17:14:20 -06:00
|
|
|
let _val2 = val.expect_err("this never happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect_err()` on `Err` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.unwrap();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap()` on `Err` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.expect("this always happens");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `expect()` on `Err` value
|
2023-02-16 17:14:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unwrap_methods_option() {
|
|
|
|
let val = Some(1);
|
|
|
|
let _val2 = val.unwrap_or(2);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or()` on `Some` value
|
2023-02-16 17:14:20 -06:00
|
|
|
let _val2 = val.unwrap_or_default();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_default()` on `Some` value
|
2023-02-16 17:14:20 -06:00
|
|
|
let _val2 = val.unwrap_or_else(|| 2);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_else()` on `Some` value
|
2023-02-16 17:14:20 -06:00
|
|
|
}
|
|
|
|
|
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);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or()` on `Some` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or_default();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_default()` on `Some` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or_else(|| 2);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_else()` on `Some` value
|
2023-05-31 14:09:12 -05:00
|
|
|
|
|
|
|
let val = Some::<usize>([1, 2, 3].iter().sum());
|
|
|
|
let _val2 = val.unwrap_or(2);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or()` on `Some` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.unwrap_or_default();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_default()` on `Some` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.unwrap_or_else(|| 2);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_else()` on `Some` value
|
2023-05-31 14:09:12 -05:00
|
|
|
}
|
|
|
|
|
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);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.unwrap_or_default();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_default()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val2 = val.unwrap_or_else(|_| 2);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_else()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn unwrap_methods_result_context() {
|
|
|
|
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or(2);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or_default();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_default()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or_else(|_| 2);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_else()` on `Ok` value
|
2023-05-31 14:09:12 -05:00
|
|
|
|
|
|
|
let val = Ok::<usize, ()>([1, 2, 3].iter().sum());
|
2023-02-16 17:14:20 -06:00
|
|
|
let _val2 = val.unwrap_or(2);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or()` on `Ok` value
|
2023-02-16 17:14:20 -06:00
|
|
|
let _val2 = val.unwrap_or_default();
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_default()` on `Ok` value
|
2023-02-16 17:14:20 -06:00
|
|
|
let _val2 = val.unwrap_or_else(|_| 2);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: used `unwrap_or_else()` on `Ok` value
|
2023-02-16 17:14:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|