Skip to content

Commit 34fab5c

Browse files
authored
Allow multiline suggestions in map-unwrap-or (#16114)
I recently got a `map-unwrap-or` warning with no suggestion, and was very confused because I couldn't tell what the lint was flagging. It turned out the "else" part had multiple lines, so the lint decided not to include a suggestion. Please look at this PR's stderr file, and decide whether you like it. Then look at [its predecessor](https://github.com/rust-lang/rust-clippy/blob/24e16f992c2dda1904fe8d4a94b391513832ce60/tests/ui/map_unwrap_or.stderr), and decide whether you like it. IMHO, the one with the suggestions is much more informative, even if not as pretty. changelog: Allow multiline suggestions in `map-unwrap-or`
2 parents f245d85 + d707ec1 commit 34fab5c

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

clippy_lints/src/methods/map_unwrap_or.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
1+
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::msrvs::{self, Msrv};
33
use clippy_utils::res::MaybeDef;
44
use clippy_utils::source::snippet;
@@ -51,11 +51,8 @@ pub(super) fn check<'tcx>(
5151
// get snippets for args to map() and unwrap_or_else()
5252
let map_snippet = snippet(cx, map_arg.span, "..");
5353
let unwrap_snippet = snippet(cx, unwrap_arg.span, "..");
54-
// lint, with note if neither arg is > 1 line and both map() and
55-
// unwrap_or_else() have the same span
56-
let multiline = map_snippet.lines().count() > 1 || unwrap_snippet.lines().count() > 1;
57-
let same_span = map_arg.span.eq_ctxt(unwrap_arg.span);
58-
if same_span && !multiline {
54+
// lint, with note if both map() and unwrap_or_else() have the same span
55+
if map_arg.span.eq_ctxt(unwrap_arg.span) {
5956
let var_snippet = snippet(cx, recv.span, "..");
6057
span_lint_and_sugg(
6158
cx,
@@ -67,9 +64,6 @@ pub(super) fn check<'tcx>(
6764
Applicability::MachineApplicable,
6865
);
6966
return true;
70-
} else if same_span && multiline {
71-
span_lint(cx, MAP_UNWRAP_OR, expr.span, msg);
72-
return true;
7367
}
7468
}
7569

tests/ui/map_unwrap_or.stderr

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ LL | | x + 1
127127
LL | | }
128128
LL | | ).unwrap_or_else(|| 0);
129129
| |__________________________^
130+
|
131+
help: try
132+
|
133+
LL ~ let _ = opt.map_or_else(|| 0, |x| {
134+
LL +
135+
LL + x + 1
136+
LL ~ });
137+
|
130138

131139
error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value
132140
--> tests/ui/map_unwrap_or.rs:63:13
@@ -138,6 +146,12 @@ LL | | .unwrap_or_else(||
138146
LL | | 0
139147
LL | | );
140148
| |_________^
149+
|
150+
help: try
151+
|
152+
LL ~ let _ = opt.map_or_else(||
153+
LL ~ 0, |x| x + 1);
154+
|
141155

142156
error: called `map(<f>).unwrap_or(false)` on an `Option` value
143157
--> tests/ui/map_unwrap_or.rs:70:13
@@ -161,6 +175,14 @@ LL | | x + 1
161175
LL | | }
162176
LL | | ).unwrap_or_else(|_e| 0);
163177
| |____________________________^
178+
|
179+
help: try
180+
|
181+
LL ~ let _ = res.map_or_else(|_e| 0, |x| {
182+
LL +
183+
LL + x + 1
184+
LL ~ });
185+
|
164186

165187
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value
166188
--> tests/ui/map_unwrap_or.rs:86:13
@@ -172,6 +194,13 @@ LL | | .unwrap_or_else(|_e| {
172194
LL | | 0
173195
LL | | });
174196
| |__________^
197+
|
198+
help: try
199+
|
200+
LL ~ let _ = res.map_or_else(|_e| {
201+
LL + 0
202+
LL ~ }, |x| x + 1);
203+
|
175204

176205
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value
177206
--> tests/ui/map_unwrap_or.rs:111:13

0 commit comments

Comments
 (0)