From 22673bce4cd8bad876d047172680a9ced02ba4c1 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Tue, 24 May 2022 14:41:23 +0200 Subject: [PATCH 1/2] add testcase for #8878 --- tests/ui/map_flatten_fixable.fixed | 18 ++++++++++++++++++ tests/ui/map_flatten_fixable.rs | 17 +++++++++++++++++ tests/ui/map_flatten_fixable.stderr | 28 +++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/tests/ui/map_flatten_fixable.fixed b/tests/ui/map_flatten_fixable.fixed index fec3a95edd6..08aba35e14d 100644 --- a/tests/ui/map_flatten_fixable.fixed +++ b/tests/ui/map_flatten_fixable.fixed @@ -28,4 +28,22 @@ fn main() { // mapping to Result on Result let _: Result<_, &str> = (Ok(Ok(1))).and_then(|x| x); + + issue8878(); +} + +#[allow(clippy::bind_instead_of_map)] // map + flatten will be suggested to `and_then`, but afterwards `map` is suggested again +#[rustfmt::skip] // whitespace is important for this one +fn issue8878() { + std::collections::HashMap::::new() + .get(&0) + .and_then(|_| { +// we need some newlines +// so that the span is big enough +// we need some newlines +// so that the span is big enough +// for a splitted output of the diagnostic + Some("") + // whitespace beforehand is important as well + }); } diff --git a/tests/ui/map_flatten_fixable.rs b/tests/ui/map_flatten_fixable.rs index aa1f76e335a..fbe2bf8a37d 100644 --- a/tests/ui/map_flatten_fixable.rs +++ b/tests/ui/map_flatten_fixable.rs @@ -28,4 +28,21 @@ fn main() { // mapping to Result on Result let _: Result<_, &str> = (Ok(Ok(1))).map(|x| x).flatten(); + + issue8878(); +} + +#[allow(clippy::bind_instead_of_map)] // map + flatten will be suggested to `and_then`, but afterwards `map` is suggested again +#[rustfmt::skip] // whitespace is important for this one +fn issue8878() { + std::collections::HashMap::::new() + .get(&0) + .map(|_| { +// we need some newlines +// so that the span is big enough +// for a splitted output of the diagnostic + Some("") + // whitespace beforehand is important as well + }) + .flatten(); } diff --git a/tests/ui/map_flatten_fixable.stderr b/tests/ui/map_flatten_fixable.stderr index c91c73846b6..5ee2afca1d0 100644 --- a/tests/ui/map_flatten_fixable.stderr +++ b/tests/ui/map_flatten_fixable.stderr @@ -76,5 +76,31 @@ help: try replacing `map` with `and_then`, and remove the `.flatten()` LL | let _: Result<_, &str> = (Ok(Ok(1))).and_then(|x| x); | ~~~~~~~~~~~~~~~ -error: aborting due to 7 previous errors +error: called `map(..).flatten()` on `Option` + --> $DIR/map_flatten_fixable.rs:40:10 + | +LL | .map(|_| { + | __________^ +LL | | // we need some newlines +LL | | // so that the span is big enough +LL | | // for a splitted output of the diagnostic +... | +LL | | }) +LL | | .flatten(); + | |__________________^ + | +help: try replacing `map` with `and_then` + | +LL ~ .and_then(|_| { +LL + // we need some newlines +LL + // so that the span is big enough + | +help: and remove the `.flatten()` + | +LL + Some("") +LL + // whitespace beforehand is important as well +LL ~ }); + | + +error: aborting due to 8 previous errors From 04297de2ed5f9e822fc4bf79e24556a68df56dce Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Tue, 24 May 2022 14:42:08 +0200 Subject: [PATCH 2/2] prepare test for 8734 --- tests/ui/map_flatten_fixable.fixed | 19 +++++++++++++++++++ tests/ui/map_flatten_fixable.rs | 19 +++++++++++++++++++ tests/ui/map_flatten_fixable.stderr | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/ui/map_flatten_fixable.fixed b/tests/ui/map_flatten_fixable.fixed index 08aba35e14d..928e5bd509c 100644 --- a/tests/ui/map_flatten_fixable.fixed +++ b/tests/ui/map_flatten_fixable.fixed @@ -29,9 +29,28 @@ fn main() { // mapping to Result on Result let _: Result<_, &str> = (Ok(Ok(1))).and_then(|x| x); + issue8734(); issue8878(); } +fn issue8734() { + // let _ = [0u8, 1, 2, 3] + // .into_iter() + // .map(|n| match n { + // 1 => [n + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1)], + // n => [n], + // }) + // .flatten(); +} + #[allow(clippy::bind_instead_of_map)] // map + flatten will be suggested to `and_then`, but afterwards `map` is suggested again #[rustfmt::skip] // whitespace is important for this one fn issue8878() { diff --git a/tests/ui/map_flatten_fixable.rs b/tests/ui/map_flatten_fixable.rs index fbe2bf8a37d..4345c6eee74 100644 --- a/tests/ui/map_flatten_fixable.rs +++ b/tests/ui/map_flatten_fixable.rs @@ -29,9 +29,28 @@ fn main() { // mapping to Result on Result let _: Result<_, &str> = (Ok(Ok(1))).map(|x| x).flatten(); + issue8734(); issue8878(); } +fn issue8734() { + // let _ = [0u8, 1, 2, 3] + // .into_iter() + // .map(|n| match n { + // 1 => [n + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1) + // .saturating_add(1)], + // n => [n], + // }) + // .flatten(); +} + #[allow(clippy::bind_instead_of_map)] // map + flatten will be suggested to `and_then`, but afterwards `map` is suggested again #[rustfmt::skip] // whitespace is important for this one fn issue8878() { diff --git a/tests/ui/map_flatten_fixable.stderr b/tests/ui/map_flatten_fixable.stderr index 5ee2afca1d0..828e24acaad 100644 --- a/tests/ui/map_flatten_fixable.stderr +++ b/tests/ui/map_flatten_fixable.stderr @@ -77,7 +77,7 @@ LL | let _: Result<_, &str> = (Ok(Ok(1))).and_then(|x| x); | ~~~~~~~~~~~~~~~ error: called `map(..).flatten()` on `Option` - --> $DIR/map_flatten_fixable.rs:40:10 + --> $DIR/map_flatten_fixable.rs:59:10 | LL | .map(|_| { | __________^