Skip to content

Commit d707ec1

Browse files
committed
Allow multiline suggestions in map-unwrap-or
1 parent 24e16f9 commit d707ec1

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
@@ -123,6 +123,14 @@ LL | | x + 1
123123
LL | | }
124124
LL | | ).unwrap_or_else(|| 0);
125125
| |__________________________^
126+
|
127+
help: try
128+
|
129+
LL ~ let _ = opt.map_or_else(|| 0, |x| {
130+
LL +
131+
LL + x + 1
132+
LL ~ });
133+
|
126134

127135
error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value
128136
--> tests/ui/map_unwrap_or.rs:63:13
@@ -134,6 +142,12 @@ LL | | .unwrap_or_else(||
134142
LL | | 0
135143
LL | | );
136144
| |_________^
145+
|
146+
help: try
147+
|
148+
LL ~ let _ = opt.map_or_else(||
149+
LL ~ 0, |x| x + 1);
150+
|
137151

138152
error: called `map(<f>).unwrap_or(false)` on an `Option` value
139153
--> tests/ui/map_unwrap_or.rs:70:13
@@ -157,6 +171,14 @@ LL | | x + 1
157171
LL | | }
158172
LL | | ).unwrap_or_else(|_e| 0);
159173
| |____________________________^
174+
|
175+
help: try
176+
|
177+
LL ~ let _ = res.map_or_else(|_e| 0, |x| {
178+
LL +
179+
LL + x + 1
180+
LL ~ });
181+
|
160182

161183
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value
162184
--> tests/ui/map_unwrap_or.rs:86:13
@@ -168,6 +190,13 @@ LL | | .unwrap_or_else(|_e| {
168190
LL | | 0
169191
LL | | });
170192
| |__________^
193+
|
194+
help: try
195+
|
196+
LL ~ let _ = res.map_or_else(|_e| {
197+
LL + 0
198+
LL ~ }, |x| x + 1);
199+
|
171200

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

0 commit comments

Comments
 (0)