fix option_map_unit_fn_unfixable.rs

The errors were unrelated to the lint...
This commit is contained in:
Ada Alakbarova 2025-10-12 14:06:00 +02:00
parent 4463ba7ade
commit b0ecbdf5a2
No known key found for this signature in database
2 changed files with 75 additions and 19 deletions

View file

@ -1,4 +1,6 @@
//@no-rustfix
#![warn(clippy::option_map_unit_fn)]
#![allow(clippy::unnecessary_wraps, clippy::unnecessary_map_on_constructor)] // only fires before the fix
fn do_nothing<T>(_: T) {}
@ -10,14 +12,19 @@ fn plus_one(value: usize) -> usize {
value + 1
}
struct HasOption {
field: Option<usize>,
}
#[rustfmt::skip]
fn option_map_unit_fn() {
let x = HasOption { field: Some(10) };
x.field.map(|value| { do_nothing(value); do_nothing(value) });
//~^ ERROR: cannot find value
//~^ option_map_unit_fn
x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
//~^ ERROR: cannot find value
//~^ option_map_unit_fn
// Suggestion for the let block should be `{ ... }` as it's too difficult to build a
// proper suggestion for these cases
@ -25,18 +32,22 @@ fn option_map_unit_fn() {
do_nothing(value);
do_nothing(value)
});
//~^^^^ ERROR: cannot find value
//~^^^^ option_map_unit_fn
x.field.map(|value| { do_nothing(value); do_nothing(value); });
//~^ ERROR: cannot find value
//~^ option_map_unit_fn
// The following should suggest `if let Some(_X) ...` as it's difficult to generate a proper let variable name for them
Some(42).map(diverge);
//~^ option_map_unit_fn
"12".parse::<i32>().ok().map(diverge);
//~^ option_map_unit_fn
Some(plus_one(1)).map(do_nothing);
//~^ option_map_unit_fn
// Should suggest `if let Some(_y) ...` to not override the existing foo variable
let y = Some(42);
y.map(do_nothing);
//~^ option_map_unit_fn
}
fn main() {}

View file

@ -1,27 +1,72 @@
error[E0425]: cannot find value `x` in this scope
--> tests/ui/option_map_unit_fn_unfixable.rs:16:5
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
--> tests/ui/option_map_unit_fn_unfixable.rs:23:5
|
LL | x.field.map(|value| { do_nothing(value); do_nothing(value) });
| ^ not found in this scope
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try: `if let Some(value) = x.field { ... }`
|
= note: `-D clippy::option-map-unit-fn` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::option_map_unit_fn)]`
error[E0425]: cannot find value `x` in this scope
--> tests/ui/option_map_unit_fn_unfixable.rs:19:5
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
--> tests/ui/option_map_unit_fn_unfixable.rs:26:5
|
LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
| ^ not found in this scope
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try: `if let Some(value) = x.field { ... }`
error[E0425]: cannot find value `x` in this scope
--> tests/ui/option_map_unit_fn_unfixable.rs:24:5
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
--> tests/ui/option_map_unit_fn_unfixable.rs:31:5
|
LL | // x.field.map(|value| {
LL | || do_nothing(value);
LL | || do_nothing(value)
LL | || });
| ||______^- help: try: `if let Some(value) = x.field { ... }`
| |______|
|
LL | x.field.map(|value| {
| ^ not found in this scope
error[E0425]: cannot find value `x` in this scope
--> tests/ui/option_map_unit_fn_unfixable.rs:29:5
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
--> tests/ui/option_map_unit_fn_unfixable.rs:36:5
|
LL | x.field.map(|value| { do_nothing(value); do_nothing(value); });
| ^ not found in this scope
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try: `if let Some(value) = x.field { ... }`
error: aborting due to 4 previous errors
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
--> tests/ui/option_map_unit_fn_unfixable.rs:40:5
|
LL | Some(42).map(diverge);
| ^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try: `if let Some(a) = Some(42) { diverge(a) }`
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
--> tests/ui/option_map_unit_fn_unfixable.rs:42:5
|
LL | "12".parse::<i32>().ok().map(diverge);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try: `if let Some(a) = "12".parse::<i32>().ok() { diverge(a) }`
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
--> tests/ui/option_map_unit_fn_unfixable.rs:44:5
|
LL | Some(plus_one(1)).map(do_nothing);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| |
| help: try: `if let Some(a) = Some(plus_one(1)) { do_nothing(a) }`
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
--> tests/ui/option_map_unit_fn_unfixable.rs:49:5
|
LL | y.map(do_nothing);
| ^^^^^^^^^^^^^^^^^-
| |
| help: try: `if let Some(_y) = y { do_nothing(_y) }`
error: aborting due to 8 previous errors
For more information about this error, try `rustc --explain E0425`.