rust-clippy/tests/ui/unnecessary_literal_unwrap_unfixable.rs

171 lines
6.5 KiB
Rust
Raw Normal View History

2023-02-16 15:30:09 +00:00
#![warn(clippy::unnecessary_literal_unwrap)]
2023-02-16 23:14:20 +00:00
#![allow(unreachable_code)]
2023-05-31 19:09:12 +00:00
#![allow(clippy::unnecessary_lazy_evaluations, clippy::let_unit_value)]
2023-07-27 11:40:22 +00:00
//@no-rustfix
2023-02-16 23:14:20 +00:00
fn unwrap_option_some() {
2023-02-16 15:30:09 +00:00
let val = Some(1);
let _val2 = val.unwrap();
//~^ ERROR: used `unwrap()` on `Some` value
2023-02-16 23:14:20 +00:00
let _val2 = val.expect("this never happens");
//~^ ERROR: used `expect()` on `Some` value
2023-02-16 23:14:20 +00:00
}
2023-05-31 19:09:12 +00:00
fn unwrap_option_some_context() {
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap();
//~^ ERROR: used `unwrap()` on `Some` value
2023-05-31 19:09:12 +00:00
let _val = Some::<usize>([1, 2, 3].iter().sum()).expect("this never happens");
//~^ ERROR: used `expect()` on `Some` value
2023-05-31 19:09:12 +00:00
let val = Some::<usize>([1, 2, 3].iter().sum());
let _val2 = val.unwrap();
//~^ ERROR: used `unwrap()` on `Some` value
2023-05-31 19:09:12 +00:00
let _val2 = val.expect("this never happens");
//~^ ERROR: used `expect()` on `Some` value
2023-05-31 19:09:12 +00:00
}
2023-02-16 23:14:20 +00:00
fn unwrap_option_none() {
2023-05-31 19:09:12 +00:00
let val = None::<()>;
let _val2 = val.unwrap();
//~^ ERROR: used `unwrap()` on `None` value
2023-05-31 19:09:12 +00:00
let _val2 = val.expect("this always happens");
//~^ ERROR: used `expect()` on `None` value
2023-07-06 02:13:37 +00:00
let _val3: u8 = None.unwrap_or_default();
//~^ ERROR: used `unwrap_or_default()` on `None` value
2023-07-06 02:13:37 +00:00
None::<()>.unwrap_or_default();
//~^ ERROR: used `unwrap_or_default()` on `None` value
2023-02-16 23:14:20 +00:00
}
fn unwrap_result_ok() {
2023-05-31 19:09:12 +00:00
let val = Ok::<_, ()>(1);
let _val2 = val.unwrap();
//~^ ERROR: used `unwrap()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val2 = val.expect("this never happens");
//~^ ERROR: used `expect()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val2 = val.unwrap_err();
//~^ ERROR: used `unwrap_err()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val2 = val.expect_err("this always happens");
//~^ ERROR: used `expect_err()` on `Ok` value
2023-05-31 19:09:12 +00:00
}
fn unwrap_result_ok_context() {
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap();
//~^ ERROR: used `unwrap()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).expect("this never happens");
//~^ ERROR: used `expect()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_err();
//~^ ERROR: used `unwrap_err()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).expect_err("this always happens");
//~^ ERROR: used `expect_err()` on `Ok` value
2023-05-31 19:09:12 +00:00
let val = Ok::<usize, ()>([1, 2, 3].iter().sum());
2023-02-16 23:14:20 +00:00
let _val2 = val.unwrap();
//~^ ERROR: used `unwrap()` on `Ok` value
2023-02-16 23:14:20 +00:00
let _val2 = val.expect("this never happens");
//~^ ERROR: used `expect()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val2 = val.unwrap_err();
//~^ ERROR: used `unwrap_err()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val2 = val.expect_err("this always happens");
//~^ ERROR: used `expect_err()` on `Ok` value
2023-02-16 23:14:20 +00:00
}
fn unwrap_result_err() {
2023-05-31 19:09:12 +00:00
let val = Err::<(), _>(1);
let _val2 = val.unwrap_err();
//~^ ERROR: used `unwrap_err()` on `Err` value
2023-05-31 19:09:12 +00:00
let _val2 = val.expect_err("this never happens");
//~^ ERROR: used `expect_err()` on `Err` value
2023-05-31 19:09:12 +00:00
let _val2 = val.unwrap();
//~^ ERROR: used `unwrap()` on `Err` value
2023-05-31 19:09:12 +00:00
let _val2 = val.expect("this always happens");
//~^ ERROR: used `expect()` on `Err` value
2023-05-31 19:09:12 +00:00
}
fn unwrap_result_err_context() {
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).unwrap_err();
//~^ ERROR: used `unwrap_err()` on `Err` value
2023-05-31 19:09:12 +00:00
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).expect_err("this never happens");
//~^ ERROR: used `expect_err()` on `Err` value
2023-05-31 19:09:12 +00:00
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).unwrap();
//~^ ERROR: used `unwrap()` on `Err` value
2023-05-31 19:09:12 +00:00
let _val = Err::<(), usize>([1, 2, 3].iter().sum()).expect("this always happens");
//~^ ERROR: used `expect()` on `Err` value
2023-05-31 19:09:12 +00:00
let val = Err::<(), usize>([1, 2, 3].iter().sum());
2023-02-16 23:14:20 +00:00
let _val2 = val.unwrap_err();
//~^ ERROR: used `unwrap_err()` on `Err` value
2023-02-16 23:14:20 +00:00
let _val2 = val.expect_err("this never happens");
//~^ ERROR: used `expect_err()` on `Err` value
2023-05-31 19:09:12 +00:00
let _val2 = val.unwrap();
//~^ ERROR: used `unwrap()` on `Err` value
2023-05-31 19:09:12 +00:00
let _val2 = val.expect("this always happens");
//~^ ERROR: used `expect()` on `Err` value
2023-02-16 23:14:20 +00:00
}
fn unwrap_methods_option() {
let val = Some(1);
let _val2 = val.unwrap_or(2);
//~^ ERROR: used `unwrap_or()` on `Some` value
2023-02-16 23:14:20 +00:00
let _val2 = val.unwrap_or_default();
//~^ ERROR: used `unwrap_or_default()` on `Some` value
2023-02-16 23:14:20 +00:00
let _val2 = val.unwrap_or_else(|| 2);
//~^ ERROR: used `unwrap_or_else()` on `Some` value
2023-02-16 23:14:20 +00:00
}
2023-05-31 19:09:12 +00:00
fn unwrap_methods_option_context() {
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or(2);
//~^ ERROR: used `unwrap_or()` on `Some` value
2023-05-31 19:09:12 +00:00
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or_default();
//~^ ERROR: used `unwrap_or_default()` on `Some` value
2023-05-31 19:09:12 +00:00
let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or_else(|| 2);
//~^ ERROR: used `unwrap_or_else()` on `Some` value
2023-05-31 19:09:12 +00:00
let val = Some::<usize>([1, 2, 3].iter().sum());
let _val2 = val.unwrap_or(2);
//~^ ERROR: used `unwrap_or()` on `Some` value
2023-05-31 19:09:12 +00:00
let _val2 = val.unwrap_or_default();
//~^ ERROR: used `unwrap_or_default()` on `Some` value
2023-05-31 19:09:12 +00:00
let _val2 = val.unwrap_or_else(|| 2);
//~^ ERROR: used `unwrap_or_else()` on `Some` value
2023-05-31 19:09:12 +00:00
}
2023-02-16 23:14:20 +00:00
fn unwrap_methods_result() {
2023-05-31 19:09:12 +00:00
let val = Ok::<_, ()>(1);
let _val2 = val.unwrap_or(2);
//~^ ERROR: used `unwrap_or()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val2 = val.unwrap_or_default();
//~^ ERROR: used `unwrap_or_default()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val2 = val.unwrap_or_else(|_| 2);
//~^ ERROR: used `unwrap_or_else()` on `Ok` value
2023-05-31 19:09:12 +00:00
}
fn unwrap_methods_result_context() {
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or(2);
//~^ ERROR: used `unwrap_or()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or_default();
//~^ ERROR: used `unwrap_or_default()` on `Ok` value
2023-05-31 19:09:12 +00:00
let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or_else(|_| 2);
//~^ ERROR: used `unwrap_or_else()` on `Ok` value
2023-05-31 19:09:12 +00:00
let val = Ok::<usize, ()>([1, 2, 3].iter().sum());
2023-02-16 23:14:20 +00:00
let _val2 = val.unwrap_or(2);
//~^ ERROR: used `unwrap_or()` on `Ok` value
2023-02-16 23:14:20 +00:00
let _val2 = val.unwrap_or_default();
//~^ ERROR: used `unwrap_or_default()` on `Ok` value
2023-02-16 23:14:20 +00:00
let _val2 = val.unwrap_or_else(|_| 2);
//~^ ERROR: used `unwrap_or_else()` on `Ok` value
2023-02-16 23:14:20 +00:00
}
fn main() {
unwrap_option_some();
2023-05-31 19:09:12 +00:00
unwrap_option_some_context();
2023-02-16 23:14:20 +00:00
unwrap_option_none();
unwrap_result_ok();
2023-05-31 19:09:12 +00:00
unwrap_result_ok_context();
2023-02-16 23:14:20 +00:00
unwrap_result_err();
2023-05-31 19:09:12 +00:00
unwrap_result_err_context();
2023-02-16 23:14:20 +00:00
unwrap_methods_option();
2023-05-31 19:09:12 +00:00
unwrap_methods_option_context();
2023-02-16 23:14:20 +00:00
unwrap_methods_result();
2023-05-31 19:09:12 +00:00
unwrap_methods_result_context();
2023-02-16 15:30:09 +00:00
}