Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions clippy_lints/src/methods/map_unwrap_or.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::res::MaybeDef;
use clippy_utils::source::snippet;
Expand Down Expand Up @@ -51,11 +51,8 @@ pub(super) fn check<'tcx>(
// get snippets for args to map() and unwrap_or_else()
let map_snippet = snippet(cx, map_arg.span, "..");
let unwrap_snippet = snippet(cx, unwrap_arg.span, "..");
// lint, with note if neither arg is > 1 line and both map() and
// unwrap_or_else() have the same span
let multiline = map_snippet.lines().count() > 1 || unwrap_snippet.lines().count() > 1;
let same_span = map_arg.span.eq_ctxt(unwrap_arg.span);
if same_span && !multiline {
// lint, with note if both map() and unwrap_or_else() have the same span
if map_arg.span.eq_ctxt(unwrap_arg.span) {
let var_snippet = snippet(cx, recv.span, "..");
span_lint_and_sugg(
cx,
Expand All @@ -67,9 +64,6 @@ pub(super) fn check<'tcx>(
Applicability::MachineApplicable,
);
return true;
} else if same_span && multiline {
span_lint(cx, MAP_UNWRAP_OR, expr.span, msg);
return true;
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/ui/map_unwrap_or.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ LL | | x + 1
LL | | }
LL | | ).unwrap_or_else(|| 0);
| |__________________________^
|
help: try
|
LL ~ let _ = opt.map_or_else(|| 0, |x| {
LL +
LL + x + 1
LL ~ });
|

error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value
--> tests/ui/map_unwrap_or.rs:63:13
Expand All @@ -134,6 +142,12 @@ LL | | .unwrap_or_else(||
LL | | 0
LL | | );
| |_________^
|
help: try
|
LL ~ let _ = opt.map_or_else(||
LL ~ 0, |x| x + 1);
|

error: called `map(<f>).unwrap_or(false)` on an `Option` value
--> tests/ui/map_unwrap_or.rs:70:13
Expand All @@ -157,6 +171,14 @@ LL | | x + 1
LL | | }
LL | | ).unwrap_or_else(|_e| 0);
| |____________________________^
|
help: try
|
LL ~ let _ = res.map_or_else(|_e| 0, |x| {
LL +
LL + x + 1
LL ~ });
|

error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value
--> tests/ui/map_unwrap_or.rs:86:13
Expand All @@ -168,6 +190,13 @@ LL | | .unwrap_or_else(|_e| {
LL | | 0
LL | | });
| |__________^
|
help: try
|
LL ~ let _ = res.map_or_else(|_e| {
LL + 0
LL ~ }, |x| x + 1);
|

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