2019-09-25 15:48:09 +00:00
|
|
|
#![warn(clippy::result_map_unit_fn)]
|
2019-12-14 13:40:43 +00:00
|
|
|
#![feature(never_type)]
|
2023-08-26 14:26:09 +00:00
|
|
|
#![allow(unused, clippy::unnecessary_map_on_constructor)]
|
2023-07-27 11:40:22 +00:00
|
|
|
//@no-rustfix
|
2020-04-13 13:26:53 +00:00
|
|
|
struct HasResult {
|
|
|
|
field: Result<usize, usize>,
|
|
|
|
}
|
|
|
|
|
2019-09-25 15:48:09 +00:00
|
|
|
fn do_nothing<T>(_: T) {}
|
|
|
|
|
|
|
|
fn diverge<T>(_: T) -> ! {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn plus_one(value: usize) -> usize {
|
|
|
|
value + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn result_map_unit_fn() {
|
2020-04-13 13:26:53 +00:00
|
|
|
let x = HasResult { field: Ok(10) };
|
|
|
|
|
2019-09-25 15:48:09 +00:00
|
|
|
x.field.map(|value| { do_nothing(value); do_nothing(value) });
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: called `map(f)` on an `Result` value where `f` is a closure that returns t
|
|
|
|
//~| NOTE: `-D clippy::result-map-unit-fn` implied by `-D warnings`
|
2019-09-25 15:48:09 +00:00
|
|
|
|
|
|
|
x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: called `map(f)` on an `Result` value where `f` is a closure that returns t
|
2019-09-25 15:48:09 +00:00
|
|
|
|
|
|
|
// Suggestion for the let block should be `{ ... }` as it's too difficult to build a
|
|
|
|
// proper suggestion for these cases
|
|
|
|
x.field.map(|value| {
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: called `map(f)` on an `Result` value where `f` is a closure that returns t
|
2019-09-25 15:48:09 +00:00
|
|
|
do_nothing(value);
|
|
|
|
do_nothing(value)
|
|
|
|
});
|
|
|
|
x.field.map(|value| { do_nothing(value); do_nothing(value); });
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: called `map(f)` on an `Result` value where `f` is a closure that returns t
|
2019-09-25 15:48:09 +00:00
|
|
|
|
|
|
|
// The following should suggest `if let Ok(_X) ...` as it's difficult to generate a proper let variable name for them
|
|
|
|
let res: Result<!, usize> = Ok(42).map(diverge);
|
|
|
|
"12".parse::<i32>().map(diverge);
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: called `map(f)` on an `Result` value where `f` is a function that returns
|
2019-09-25 15:48:09 +00:00
|
|
|
|
|
|
|
let res: Result<(), usize> = Ok(plus_one(1)).map(do_nothing);
|
|
|
|
|
|
|
|
// Should suggest `if let Ok(_y) ...` to not override the existing foo variable
|
|
|
|
let y: Result<usize, usize> = Ok(42);
|
|
|
|
y.map(do_nothing);
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: called `map(f)` on an `Result` value where `f` is a function that returns
|
2019-09-25 15:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|