Skip to content

Commit 22673bc

Browse files
committed
add testcase for #8878
1 parent 050cdd6 commit 22673bc

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

tests/ui/map_flatten_fixable.fixed

+18
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,22 @@ fn main() {
2828

2929
// mapping to Result on Result
3030
let _: Result<_, &str> = (Ok(Ok(1))).and_then(|x| x);
31+
32+
issue8878();
33+
}
34+
35+
#[allow(clippy::bind_instead_of_map)] // map + flatten will be suggested to `and_then`, but afterwards `map` is suggested again
36+
#[rustfmt::skip] // whitespace is important for this one
37+
fn issue8878() {
38+
std::collections::HashMap::<u32, u32>::new()
39+
.get(&0)
40+
.and_then(|_| {
41+
// we need some newlines
42+
// so that the span is big enough
43+
// we need some newlines
44+
// so that the span is big enough
45+
// for a splitted output of the diagnostic
46+
Some("")
47+
// whitespace beforehand is important as well
48+
});
3149
}

tests/ui/map_flatten_fixable.rs

+17
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,21 @@ fn main() {
2828

2929
// mapping to Result on Result
3030
let _: Result<_, &str> = (Ok(Ok(1))).map(|x| x).flatten();
31+
32+
issue8878();
33+
}
34+
35+
#[allow(clippy::bind_instead_of_map)] // map + flatten will be suggested to `and_then`, but afterwards `map` is suggested again
36+
#[rustfmt::skip] // whitespace is important for this one
37+
fn issue8878() {
38+
std::collections::HashMap::<u32, u32>::new()
39+
.get(&0)
40+
.map(|_| {
41+
// we need some newlines
42+
// so that the span is big enough
43+
// for a splitted output of the diagnostic
44+
Some("")
45+
// whitespace beforehand is important as well
46+
})
47+
.flatten();
3148
}

tests/ui/map_flatten_fixable.stderr

+27-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,31 @@ help: try replacing `map` with `and_then`, and remove the `.flatten()`
7676
LL | let _: Result<_, &str> = (Ok(Ok(1))).and_then(|x| x);
7777
| ~~~~~~~~~~~~~~~
7878

79-
error: aborting due to 7 previous errors
79+
error: called `map(..).flatten()` on `Option`
80+
--> $DIR/map_flatten_fixable.rs:40:10
81+
|
82+
LL | .map(|_| {
83+
| __________^
84+
LL | | // we need some newlines
85+
LL | | // so that the span is big enough
86+
LL | | // for a splitted output of the diagnostic
87+
... |
88+
LL | | })
89+
LL | | .flatten();
90+
| |__________________^
91+
|
92+
help: try replacing `map` with `and_then`
93+
|
94+
LL ~ .and_then(|_| {
95+
LL + // we need some newlines
96+
LL + // so that the span is big enough
97+
|
98+
help: and remove the `.flatten()`
99+
|
100+
LL + Some("")
101+
LL + // whitespace beforehand is important as well
102+
LL ~ });
103+
|
104+
105+
error: aborting due to 8 previous errors
80106

0 commit comments

Comments
 (0)