Skip to content

Commit 02e0726

Browse files
committed
fix a bug that the closure arguments are not displayed
1 parent bbffe82 commit 02e0726

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

clippy_lints/src/methods/option_map_or_none.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn reduce_unit_expression<'a>(
2121
match block.expr {
2222
Some(inner_expr) => {
2323
// If block only contains an expression,
24-
// reduce `{ X }` to `X`
24+
// reduce `|x| { x + 1 }` to `|x| x + 1`
2525
reduce_unit_expression(cx, inner_expr)
2626
},
2727
_ => None,
@@ -51,7 +51,6 @@ pub(super) fn check<'tcx>(
5151
return;
5252
}
5353

54-
// let (lint_name, msg, instead, hint) = {
5554
let default_arg_is_none = if let hir::ExprKind::Path(ref qpath) = def_arg.kind {
5655
is_lang_ctor(cx, qpath, OptionNone)
5756
} else {
@@ -71,7 +70,8 @@ pub(super) fn check<'tcx>(
7170

7271
if is_option {
7372
let self_snippet = snippet(cx, recv.span, "..");
74-
if let hir::ExprKind::Closure(_, _, id, _, _) = map_arg.kind {
73+
if let hir::ExprKind::Closure(_, _, id, span, _) = map_arg.kind {
74+
let arg_snippet = snippet(cx, span, "..");
7575
if_chain! {
7676
let body = cx.tcx.hir().body(id);
7777
if let Some((func, arg_char)) = reduce_unit_expression(cx, &body.value);
@@ -89,7 +89,7 @@ pub(super) fn check<'tcx>(
8989
expr.span,
9090
msg,
9191
"try using `map` instead",
92-
format!("{0}.map({1})", self_snippet, func_snippet),
92+
format!("{0}.map({1} {2})", self_snippet, arg_snippet,func_snippet),
9393
Applicability::MachineApplicable,
9494
);
9595
}
@@ -99,30 +99,27 @@ pub(super) fn check<'tcx>(
9999
let func_snippet = snippet(cx, map_arg.span, "..");
100100
let msg = "called `map_or(None, ..)` on an `Option` value. This can be done more directly by calling \
101101
`and_then(..)` instead";
102-
span_lint_and_sugg(
102+
return span_lint_and_sugg(
103103
cx,
104104
OPTION_MAP_OR_NONE,
105105
expr.span,
106106
msg,
107107
"try using `and_then` instead",
108108
format!("{0}.and_then({1})", self_snippet, func_snippet),
109109
Applicability::MachineApplicable,
110-
)
110+
);
111111
} else if f_arg_is_some {
112112
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
113113
`ok()` instead";
114114
let self_snippet = snippet(cx, recv.span, "..");
115-
span_lint_and_sugg(
115+
return span_lint_and_sugg(
116116
cx,
117117
RESULT_MAP_OR_INTO_OPTION,
118118
expr.span,
119119
msg,
120120
"try using `ok` instead",
121121
format!("{0}.ok()", self_snippet),
122122
Applicability::MachineApplicable,
123-
)
124-
} else {
125-
// nothing to lint!
126-
return;
123+
);
127124
}
128125
}

tests/ui/option_map_or_none.stderr

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@ LL | let _ :Option<i32> = opt.map_or(None, |x| {
1313
| __________________________^
1414
LL | | Some(x + 1)
1515
LL | | });
16-
| |_________________________^
17-
|
18-
help: try using `map` instead
19-
|
20-
LL ~ let _ :Option<i32> = opt.map(|x| {
21-
LL + x + 1
22-
LL ~ });
23-
|
16+
| |_________________________^ help: try using `map` instead: `opt.map(|x| x + 1)`
2417

2518
error: called `map_or(None, ..)` on an `Option` value. This can be done more directly by calling `and_then(..)` instead
2619
--> $DIR/option_map_or_none.rs:20:26

0 commit comments

Comments
 (0)