rust-clippy/tests/ui/unnecessary_literal_unwrap.fixed

110 lines
2 KiB
Rust
Raw Normal View History

2023-02-16 14:21:43 +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 14:21:43 +00:00
2023-02-16 22:31:52 +00:00
fn unwrap_option_some() {
2023-02-16 14:37:18 +00:00
let _val = 1;
let _val = 1;
2023-05-31 19:09:12 +00:00
1;
1;
2023-02-16 14:37:18 +00:00
}
2023-07-06 03:24:20 +00:00
#[rustfmt::skip] // force rustfmt not to remove braces in `|| { 234 }`
2023-02-16 22:31:52 +00:00
fn unwrap_option_none() {
2023-05-31 19:09:12 +00:00
let _val = panic!();
let _val = panic!("this always happens");
2023-07-06 02:13:37 +00:00
let _val: String = String::default();
2023-07-06 03:24:20 +00:00
let _val: u16 = 234;
let _val: u16 = 234;
let _val: u16 = { 234 };
let _val: u16 = { 234 };
2023-05-31 19:09:12 +00:00
2023-02-16 22:31:52 +00:00
panic!();
panic!("this always happens");
2023-07-06 02:13:37 +00:00
String::default();
2023-07-06 03:24:20 +00:00
234;
234;
{ 234 };
{ 234 };
2023-02-16 22:31:52 +00:00
}
2023-02-16 17:15:19 +00:00
fn unwrap_result_ok() {
let _val = 1;
let _val = 1;
2023-05-31 19:09:12 +00:00
let _val = panic!("{:?}", 1);
let _val = panic!("{1}: {:?}", 1, "this always happens");
1;
1;
2023-02-16 22:31:52 +00:00
panic!("{:?}", 1);
panic!("{1}: {:?}", 1, "this always happens");
2023-02-16 17:15:19 +00:00
}
fn unwrap_result_err() {
2023-02-16 14:37:18 +00:00
let _val = 1;
let _val = 1;
2023-05-31 19:09:12 +00:00
let _val = panic!("{:?}", 1);
let _val = panic!("{1}: {:?}", 1, "this always happens");
1;
1;
2023-02-16 22:31:52 +00:00
panic!("{:?}", 1);
panic!("{1}: {:?}", 1, "this always happens");
2023-02-16 14:21:43 +00:00
}
2023-02-16 14:43:41 +00:00
fn unwrap_methods_option() {
let _val = 1;
let _val = 1;
2023-02-16 15:05:56 +00:00
let _val = 1;
2023-05-31 19:09:12 +00:00
1;
1;
1;
2023-02-16 14:43:41 +00:00
}
fn unwrap_methods_result() {
let _val = 1;
let _val = 1;
2023-02-16 15:05:56 +00:00
let _val = 1;
2023-05-31 19:09:12 +00:00
1;
1;
1;
2023-02-16 14:43:41 +00:00
}
fn unwrap_from_binding() {
macro_rules! from_macro {
() => {
Some("")
};
}
let val = from_macro!();
let _ = val.unwrap_or("");
}
fn unwrap_unchecked() {
let _ = 1;
let _ = unsafe { 1 + *(&1 as *const i32) }; // needs to keep the unsafe block
let _ = 1 + 1;
let _ = 1;
let _ = unsafe { 1 + *(&1 as *const i32) };
let _ = 1 + 1;
let _ = 123;
}
2023-02-16 14:21:43 +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();
unwrap_unchecked();
2023-02-16 14:21:43 +00:00
}