From 24c283ea1271143ad32f6fefe90936808c53d5a2 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 25 Sep 2019 08:48:09 -0700 Subject: [PATCH] option_map_unit_fn: Split into fixable/unfixable --- tests/ui/option_map_unit_fn_fixable.rs | 25 +------ tests/ui/option_map_unit_fn_fixable.stderr | 74 +------------------- tests/ui/option_map_unit_fn_unfixable.rs | 39 +++++++++++ tests/ui/option_map_unit_fn_unfixable.stderr | 27 +++++++ tests/ui/result_map_unit_fn_fixable.rs | 23 ------ tests/ui/result_map_unit_fn_fixable.stderr | 56 +-------------- tests/ui/result_map_unit_fn_unfixable.rs | 40 +++++++++++ tests/ui/result_map_unit_fn_unfixable.stderr | 27 +++++++ 8 files changed, 137 insertions(+), 174 deletions(-) create mode 100644 tests/ui/option_map_unit_fn_unfixable.rs create mode 100644 tests/ui/option_map_unit_fn_unfixable.stderr create mode 100644 tests/ui/result_map_unit_fn_unfixable.rs create mode 100644 tests/ui/result_map_unit_fn_unfixable.stderr diff --git a/tests/ui/option_map_unit_fn_fixable.rs b/tests/ui/option_map_unit_fn_fixable.rs index 1d2a3a17ee0..01aa6259855 100644 --- a/tests/ui/option_map_unit_fn_fixable.rs +++ b/tests/ui/option_map_unit_fn_fixable.rs @@ -71,29 +71,6 @@ fn option_map_unit_fn() { x.field.map(|value| { { plus_one(value + captured); } }); - x.field.map(|ref value| { do_nothing(value + captured) }); - - - x.field.map(|value| { do_nothing(value); do_nothing(value) }); - - x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) }); - - // Suggestion for the let block should be `{ ... }` as it's too difficult to build a - // proper suggestion for these cases - x.field.map(|value| { - do_nothing(value); - do_nothing(value) - }); - x.field.map(|value| { do_nothing(value); do_nothing(value); }); - - // 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); - "12".parse::().ok().map(diverge); - Some(plus_one(1)).map(do_nothing); - - // Should suggest `if let Some(_y) ...` to not override the existing foo variable - let y = Some(42); - y.map(do_nothing); -} + x.field.map(|ref value| { do_nothing(value + captured) });} fn main() {} diff --git a/tests/ui/option_map_unit_fn_fixable.stderr b/tests/ui/option_map_unit_fn_fixable.stderr index 4b619495b7b..7c7ad39c363 100644 --- a/tests/ui/option_map_unit_fn_fixable.stderr +++ b/tests/ui/option_map_unit_fn_fixable.stderr @@ -131,80 +131,10 @@ LL | x.field.map(|value| { { plus_one(value + captured); } }); error: called `map(f)` on an Option value where `f` is a unit closure --> $DIR/option_map_unit_fn_fixable.rs:74:5 | -LL | x.field.map(|ref value| { do_nothing(value + captured) }); +LL | x.field.map(|ref value| { do_nothing(value + captured) });} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- | | | help: try this: `if let Some(ref value) = x.field { do_nothing(value + captured) }` -error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn_fixable.rs:77:5 - | -LL | x.field.map(|value| { do_nothing(value); do_nothing(value) }); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Some(value) = x.field { ... }` - -error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn_fixable.rs:79:5 - | -LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) }); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Some(value) = x.field { ... }` - -error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn_fixable.rs:83:5 - | -LL | x.field.map(|value| { - | _____^ - | |_____| - | || -LL | || do_nothing(value); -LL | || do_nothing(value) -LL | || }); - | ||______^- help: try this: `if let Some(value) = x.field { ... }` - | |_______| - | - -error: called `map(f)` on an Option value where `f` is a unit closure - --> $DIR/option_map_unit_fn_fixable.rs:87:5 - | -LL | x.field.map(|value| { do_nothing(value); do_nothing(value); }); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Some(value) = x.field { ... }` - -error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn_fixable.rs:90:5 - | -LL | Some(42).map(diverge); - | ^^^^^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Some(_) = Some(42) { diverge(...) }` - -error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn_fixable.rs:91:5 - | -LL | "12".parse::().ok().map(diverge); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Some(_) = "12".parse::().ok() { diverge(...) }` - -error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn_fixable.rs:92:5 - | -LL | Some(plus_one(1)).map(do_nothing); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Some(_) = Some(plus_one(1)) { do_nothing(...) }` - -error: called `map(f)` on an Option value where `f` is a unit function - --> $DIR/option_map_unit_fn_fixable.rs:96:5 - | -LL | y.map(do_nothing); - | ^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Some(_y) = y { do_nothing(...) }` - -error: aborting due to 25 previous errors +error: aborting due to 17 previous errors diff --git a/tests/ui/option_map_unit_fn_unfixable.rs b/tests/ui/option_map_unit_fn_unfixable.rs new file mode 100644 index 00000000000..20e6c15b18d --- /dev/null +++ b/tests/ui/option_map_unit_fn_unfixable.rs @@ -0,0 +1,39 @@ +#![warn(clippy::option_map_unit_fn)] +#![allow(unused)] + +fn do_nothing(_: T) {} + +fn diverge(_: T) -> ! { + panic!() +} + +fn plus_one(value: usize) -> usize { + value + 1 +} + +#[rustfmt::skip] +fn option_map_unit_fn() { + + x.field.map(|value| { do_nothing(value); do_nothing(value) }); + + x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) }); + + // Suggestion for the let block should be `{ ... }` as it's too difficult to build a + // proper suggestion for these cases + x.field.map(|value| { + do_nothing(value); + do_nothing(value) + }); + x.field.map(|value| { do_nothing(value); do_nothing(value); }); + + // 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); + "12".parse::().ok().map(diverge); + Some(plus_one(1)).map(do_nothing); + + // Should suggest `if let Some(_y) ...` to not override the existing foo variable + let y = Some(42); + y.map(do_nothing); +} + +fn main() {} diff --git a/tests/ui/option_map_unit_fn_unfixable.stderr b/tests/ui/option_map_unit_fn_unfixable.stderr new file mode 100644 index 00000000000..a53f5889c58 --- /dev/null +++ b/tests/ui/option_map_unit_fn_unfixable.stderr @@ -0,0 +1,27 @@ +error[E0425]: cannot find value `x` in this scope + --> $DIR/option_map_unit_fn_unfixable.rs:17:5 + | +LL | x.field.map(|value| { do_nothing(value); do_nothing(value) }); + | ^ not found in this scope + +error[E0425]: cannot find value `x` in this scope + --> $DIR/option_map_unit_fn_unfixable.rs:19:5 + | +LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) }); + | ^ not found in this scope + +error[E0425]: cannot find value `x` in this scope + --> $DIR/option_map_unit_fn_unfixable.rs:23:5 + | +LL | x.field.map(|value| { + | ^ not found in this scope + +error[E0425]: cannot find value `x` in this scope + --> $DIR/option_map_unit_fn_unfixable.rs:27:5 + | +LL | x.field.map(|value| { do_nothing(value); do_nothing(value); }); + | ^ not found in this scope + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0425`. diff --git a/tests/ui/result_map_unit_fn_fixable.rs b/tests/ui/result_map_unit_fn_fixable.rs index a8e891d8db0..b0377fbcd16 100644 --- a/tests/ui/result_map_unit_fn_fixable.rs +++ b/tests/ui/result_map_unit_fn_fixable.rs @@ -74,29 +74,6 @@ fn result_map_unit_fn() { x.field.map(|ref value| { do_nothing(value + captured) }); - - - x.field.map(|value| { do_nothing(value); do_nothing(value) }); - - x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) }); - - // Suggestion for the let block should be `{ ... }` as it's too difficult to build a - // proper suggestion for these cases - x.field.map(|value| { - do_nothing(value); - do_nothing(value) - }); - x.field.map(|value| { do_nothing(value); do_nothing(value); }); - - // The following should suggest `if let Ok(_X) ...` as it's difficult to generate a proper let variable name for them - let res: Result = Ok(42).map(diverge); - "12".parse::().map(diverge); - - 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 = Ok(42); - y.map(do_nothing); } fn main() {} diff --git a/tests/ui/result_map_unit_fn_fixable.stderr b/tests/ui/result_map_unit_fn_fixable.stderr index 29d1bd6dc18..e48fb7c1494 100644 --- a/tests/ui/result_map_unit_fn_fixable.stderr +++ b/tests/ui/result_map_unit_fn_fixable.stderr @@ -136,59 +136,5 @@ LL | x.field.map(|ref value| { do_nothing(value + captured) }); | | | help: try this: `if let Ok(ref value) = x.field { do_nothing(value + captured) }` -error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn_fixable.rs:79:5 - | -LL | x.field.map(|value| { do_nothing(value); do_nothing(value) }); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Ok(value) = x.field { ... }` - -error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn_fixable.rs:81:5 - | -LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) }); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Ok(value) = x.field { ... }` - -error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn_fixable.rs:85:5 - | -LL | x.field.map(|value| { - | _____^ - | |_____| - | || -LL | || do_nothing(value); -LL | || do_nothing(value) -LL | || }); - | ||______^- help: try this: `if let Ok(value) = x.field { ... }` - | |_______| - | - -error: called `map(f)` on an Result value where `f` is a unit closure - --> $DIR/result_map_unit_fn_fixable.rs:89:5 - | -LL | x.field.map(|value| { do_nothing(value); do_nothing(value); }); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Ok(value) = x.field { ... }` - -error: called `map(f)` on an Result value where `f` is a unit function - --> $DIR/result_map_unit_fn_fixable.rs:93:5 - | -LL | "12".parse::().map(diverge); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Ok(_) = "12".parse::() { diverge(...) }` - -error: called `map(f)` on an Result value where `f` is a unit function - --> $DIR/result_map_unit_fn_fixable.rs:99:5 - | -LL | y.map(do_nothing); - | ^^^^^^^^^^^^^^^^^- - | | - | help: try this: `if let Ok(_y) = y { do_nothing(...) }` - -error: aborting due to 23 previous errors +error: aborting due to 17 previous errors diff --git a/tests/ui/result_map_unit_fn_unfixable.rs b/tests/ui/result_map_unit_fn_unfixable.rs new file mode 100644 index 00000000000..7d597332eaf --- /dev/null +++ b/tests/ui/result_map_unit_fn_unfixable.rs @@ -0,0 +1,40 @@ +#![feature(never_type)] +#![warn(clippy::result_map_unit_fn)] +#![allow(unused)] + +fn do_nothing(_: T) {} + +fn diverge(_: T) -> ! { + panic!() +} + +fn plus_one(value: usize) -> usize { + value + 1 +} + +#[rustfmt::skip] +fn result_map_unit_fn() { + x.field.map(|value| { do_nothing(value); do_nothing(value) }); + + x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) }); + + // Suggestion for the let block should be `{ ... }` as it's too difficult to build a + // proper suggestion for these cases + x.field.map(|value| { + do_nothing(value); + do_nothing(value) + }); + x.field.map(|value| { do_nothing(value); do_nothing(value); }); + + // The following should suggest `if let Ok(_X) ...` as it's difficult to generate a proper let variable name for them + let res: Result = Ok(42).map(diverge); + "12".parse::().map(diverge); + + 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 = Ok(42); + y.map(do_nothing); +} + +fn main() {} diff --git a/tests/ui/result_map_unit_fn_unfixable.stderr b/tests/ui/result_map_unit_fn_unfixable.stderr new file mode 100644 index 00000000000..949c2294679 --- /dev/null +++ b/tests/ui/result_map_unit_fn_unfixable.stderr @@ -0,0 +1,27 @@ +error[E0425]: cannot find value `x` in this scope + --> $DIR/result_map_unit_fn_unfixable.rs:17:5 + | +LL | x.field.map(|value| { do_nothing(value); do_nothing(value) }); + | ^ not found in this scope + +error[E0425]: cannot find value `x` in this scope + --> $DIR/result_map_unit_fn_unfixable.rs:19:5 + | +LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) }); + | ^ not found in this scope + +error[E0425]: cannot find value `x` in this scope + --> $DIR/result_map_unit_fn_unfixable.rs:23:5 + | +LL | x.field.map(|value| { + | ^ not found in this scope + +error[E0425]: cannot find value `x` in this scope + --> $DIR/result_map_unit_fn_unfixable.rs:27:5 + | +LL | x.field.map(|value| { do_nothing(value); do_nothing(value); }); + | ^ not found in this scope + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0425`.