Skip to content

Commit

Permalink
add testcase for rust-lang#8878
Browse files Browse the repository at this point in the history
  • Loading branch information
hellow554 authored and botahamec committed May 25, 2022
1 parent eb773eb commit de99477
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tests/ui/map_flatten_fixable.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32, u32>::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
});
}
17 changes: 17 additions & 0 deletions tests/ui/map_flatten_fixable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32, u32>::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();
}
28 changes: 27 additions & 1 deletion tests/ui/map_flatten_fixable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit de99477

Please sign in to comment.