2020-06-15 09:21:56 +00:00
|
|
|
// run-rustfix
|
2020-08-16 20:16:39 +00:00
|
|
|
#![warn(clippy::unnecessary_lazy_evaluations)]
|
2020-06-15 09:21:56 +00:00
|
|
|
#![allow(clippy::redundant_closure)]
|
|
|
|
#![allow(clippy::bind_instead_of_map)]
|
2020-08-21 22:43:04 +00:00
|
|
|
#![allow(clippy::map_identity)]
|
2020-06-15 09:21:56 +00:00
|
|
|
|
2020-06-15 13:26:02 +00:00
|
|
|
struct Deep(Option<usize>);
|
2020-06-15 09:21:56 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct SomeStruct {
|
2020-06-15 13:26:02 +00:00
|
|
|
some_field: usize,
|
2020-06-15 09:21:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SomeStruct {
|
2020-06-15 13:26:02 +00:00
|
|
|
fn return_some_field(&self) -> usize {
|
2020-06-15 09:21:56 +00:00
|
|
|
self.some_field
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn some_call<T: Default>() -> T {
|
|
|
|
T::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let astronomers_pi = 10;
|
2020-06-15 13:26:02 +00:00
|
|
|
let ext_arr: [usize; 1] = [2];
|
2020-06-15 09:21:56 +00:00
|
|
|
let ext_str = SomeStruct { some_field: 10 };
|
|
|
|
|
|
|
|
let mut opt = Some(42);
|
|
|
|
let ext_opt = Some(42);
|
2020-06-15 18:01:18 +00:00
|
|
|
let nested_opt = Some(Some(42));
|
|
|
|
let nested_tuple_opt = Some(Some((42, 43)));
|
2022-07-02 22:00:24 +00:00
|
|
|
let cond = true;
|
2020-06-15 18:01:18 +00:00
|
|
|
|
|
|
|
// Should lint - Option
|
2020-06-15 09:21:56 +00:00
|
|
|
let _ = opt.unwrap_or_else(|| 2);
|
|
|
|
let _ = opt.unwrap_or_else(|| astronomers_pi);
|
|
|
|
let _ = opt.unwrap_or_else(|| ext_str.some_field);
|
2020-06-15 13:26:02 +00:00
|
|
|
let _ = opt.unwrap_or_else(|| ext_arr[0]);
|
2020-06-15 09:21:56 +00:00
|
|
|
let _ = opt.and_then(|_| ext_opt);
|
|
|
|
let _ = opt.or_else(|| ext_opt);
|
|
|
|
let _ = opt.or_else(|| None);
|
|
|
|
let _ = opt.get_or_insert_with(|| 2);
|
|
|
|
let _ = opt.ok_or_else(|| 2);
|
2020-08-21 22:43:04 +00:00
|
|
|
let _ = nested_tuple_opt.unwrap_or_else(|| Some((1, 2)));
|
2022-07-02 22:00:24 +00:00
|
|
|
let _ = cond.then(|| astronomers_pi);
|
2020-06-15 09:21:56 +00:00
|
|
|
|
|
|
|
// Cases when unwrap is not called on a simple variable
|
|
|
|
let _ = Some(10).unwrap_or_else(|| 2);
|
|
|
|
let _ = Some(10).and_then(|_| ext_opt);
|
2020-06-15 13:26:02 +00:00
|
|
|
let _: Option<usize> = None.or_else(|| ext_opt);
|
2020-06-15 09:21:56 +00:00
|
|
|
let _ = None.get_or_insert_with(|| 2);
|
2020-06-15 13:26:02 +00:00
|
|
|
let _: Result<usize, usize> = None.ok_or_else(|| 2);
|
|
|
|
let _: Option<usize> = None.or_else(|| None);
|
2020-06-15 09:21:56 +00:00
|
|
|
|
|
|
|
let mut deep = Deep(Some(42));
|
|
|
|
let _ = deep.0.unwrap_or_else(|| 2);
|
|
|
|
let _ = deep.0.and_then(|_| ext_opt);
|
|
|
|
let _ = deep.0.or_else(|| None);
|
|
|
|
let _ = deep.0.get_or_insert_with(|| 2);
|
|
|
|
let _ = deep.0.ok_or_else(|| 2);
|
|
|
|
|
|
|
|
// Should not lint - Option
|
|
|
|
let _ = opt.unwrap_or_else(|| ext_str.return_some_field());
|
2020-06-15 18:01:18 +00:00
|
|
|
let _ = nested_opt.unwrap_or_else(|| Some(some_call()));
|
|
|
|
let _ = nested_tuple_opt.unwrap_or_else(|| Some((some_call(), some_call())));
|
2020-06-15 09:21:56 +00:00
|
|
|
let _ = opt.or_else(some_call);
|
|
|
|
let _ = opt.or_else(|| some_call());
|
2020-06-15 13:26:02 +00:00
|
|
|
let _: Result<usize, usize> = opt.ok_or_else(|| some_call());
|
|
|
|
let _: Result<usize, usize> = opt.ok_or_else(some_call);
|
2020-06-15 09:21:56 +00:00
|
|
|
let _ = deep.0.get_or_insert_with(|| some_call());
|
|
|
|
let _ = deep.0.or_else(some_call);
|
|
|
|
let _ = deep.0.or_else(|| some_call());
|
2020-08-21 22:43:04 +00:00
|
|
|
let _ = opt.ok_or_else(|| ext_arr[0]);
|
2020-06-15 09:21:56 +00:00
|
|
|
|
2020-08-21 22:43:04 +00:00
|
|
|
// should not lint, bind_instead_of_map takes priority
|
2020-06-15 13:26:02 +00:00
|
|
|
let _ = Some(10).and_then(|idx| Some(ext_arr[idx]));
|
|
|
|
let _ = Some(10).and_then(|idx| Some(idx));
|
2020-08-21 22:43:04 +00:00
|
|
|
|
|
|
|
// should lint, bind_instead_of_map doesn't apply
|
2020-06-15 13:26:02 +00:00
|
|
|
let _: Option<usize> = None.or_else(|| Some(3));
|
2020-06-15 09:21:56 +00:00
|
|
|
let _ = deep.0.or_else(|| Some(3));
|
|
|
|
let _ = opt.or_else(|| Some(3));
|
|
|
|
|
|
|
|
// Should lint - Result
|
2020-06-15 13:26:02 +00:00
|
|
|
let res: Result<usize, usize> = Err(5);
|
|
|
|
let res2: Result<usize, SomeStruct> = Err(SomeStruct { some_field: 5 });
|
2020-06-15 09:21:56 +00:00
|
|
|
|
|
|
|
let _ = res2.unwrap_or_else(|_| 2);
|
|
|
|
let _ = res2.unwrap_or_else(|_| astronomers_pi);
|
|
|
|
let _ = res2.unwrap_or_else(|_| ext_str.some_field);
|
|
|
|
|
|
|
|
// Should not lint - Result
|
|
|
|
let _ = res.unwrap_or_else(|err| err);
|
2020-06-15 13:26:02 +00:00
|
|
|
let _ = res.unwrap_or_else(|err| ext_arr[err]);
|
2020-06-15 09:21:56 +00:00
|
|
|
let _ = res2.unwrap_or_else(|err| err.some_field);
|
|
|
|
let _ = res2.unwrap_or_else(|err| err.return_some_field());
|
|
|
|
let _ = res2.unwrap_or_else(|_| ext_str.return_some_field());
|
|
|
|
|
2020-08-21 22:43:04 +00:00
|
|
|
// should not lint, bind_instead_of_map takes priority
|
2020-06-15 13:26:02 +00:00
|
|
|
let _: Result<usize, usize> = res.and_then(|x| Ok(x));
|
|
|
|
let _: Result<usize, usize> = res.or_else(|err| Err(err));
|
2020-06-15 09:21:56 +00:00
|
|
|
|
2020-06-15 13:26:02 +00:00
|
|
|
let _: Result<usize, usize> = res.and_then(|_| Ok(2));
|
|
|
|
let _: Result<usize, usize> = res.and_then(|_| Ok(astronomers_pi));
|
|
|
|
let _: Result<usize, usize> = res.and_then(|_| Ok(ext_str.some_field));
|
2020-06-15 09:21:56 +00:00
|
|
|
|
2020-08-21 22:43:04 +00:00
|
|
|
let _: Result<usize, usize> = res.or_else(|_| Err(2));
|
|
|
|
let _: Result<usize, usize> = res.or_else(|_| Err(astronomers_pi));
|
|
|
|
let _: Result<usize, usize> = res.or_else(|_| Err(ext_str.some_field));
|
|
|
|
|
|
|
|
// should lint, bind_instead_of_map doesn't apply
|
2020-06-15 13:26:02 +00:00
|
|
|
let _: Result<usize, usize> = res.and_then(|_| Err(2));
|
|
|
|
let _: Result<usize, usize> = res.and_then(|_| Err(astronomers_pi));
|
|
|
|
let _: Result<usize, usize> = res.and_then(|_| Err(ext_str.some_field));
|
2020-06-15 09:21:56 +00:00
|
|
|
|
2020-06-15 13:26:02 +00:00
|
|
|
let _: Result<usize, usize> = res.or_else(|_| Ok(2));
|
|
|
|
let _: Result<usize, usize> = res.or_else(|_| Ok(astronomers_pi));
|
|
|
|
let _: Result<usize, usize> = res.or_else(|_| Ok(ext_str.some_field));
|
2022-03-15 13:29:17 +00:00
|
|
|
let _: Result<usize, usize> = res.
|
|
|
|
// some lines
|
|
|
|
// some lines
|
|
|
|
// some lines
|
|
|
|
// some lines
|
|
|
|
// some lines
|
|
|
|
// some lines
|
|
|
|
or_else(|_| Ok(ext_str.some_field));
|
2020-09-16 17:59:51 +00:00
|
|
|
|
|
|
|
// neither bind_instead_of_map nor unnecessary_lazy_eval applies here
|
|
|
|
let _: Result<usize, usize> = res.and_then(|x| Err(x));
|
|
|
|
let _: Result<usize, usize> = res.or_else(|err| Ok(err));
|
2020-06-15 09:21:56 +00:00
|
|
|
}
|