Skip to content

Commit

Permalink
fix a bug that the closure arguments are not displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 committed Nov 15, 2021
1 parent bbffe82 commit 02e0726
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
19 changes: 8 additions & 11 deletions clippy_lints/src/methods/option_map_or_none.rs
Expand Up @@ -21,7 +21,7 @@ fn reduce_unit_expression<'a>(
match block.expr {
Some(inner_expr) => {
// If block only contains an expression,
// reduce `{ X }` to `X`
// reduce `|x| { x + 1 }` to `|x| x + 1`
reduce_unit_expression(cx, inner_expr)
},
_ => None,
Expand Down Expand Up @@ -51,7 +51,6 @@ pub(super) fn check<'tcx>(
return;
}

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

if is_option {
let self_snippet = snippet(cx, recv.span, "..");
if let hir::ExprKind::Closure(_, _, id, _, _) = map_arg.kind {
if let hir::ExprKind::Closure(_, _, id, span, _) = map_arg.kind {
let arg_snippet = snippet(cx, span, "..");
if_chain! {
let body = cx.tcx.hir().body(id);
if let Some((func, arg_char)) = reduce_unit_expression(cx, &body.value);
Expand All @@ -89,7 +89,7 @@ pub(super) fn check<'tcx>(
expr.span,
msg,
"try using `map` instead",
format!("{0}.map({1})", self_snippet, func_snippet),
format!("{0}.map({1} {2})", self_snippet, arg_snippet,func_snippet),
Applicability::MachineApplicable,
);
}
Expand All @@ -99,30 +99,27 @@ pub(super) fn check<'tcx>(
let func_snippet = snippet(cx, map_arg.span, "..");
let msg = "called `map_or(None, ..)` on an `Option` value. This can be done more directly by calling \
`and_then(..)` instead";
span_lint_and_sugg(
return span_lint_and_sugg(
cx,
OPTION_MAP_OR_NONE,
expr.span,
msg,
"try using `and_then` instead",
format!("{0}.and_then({1})", self_snippet, func_snippet),
Applicability::MachineApplicable,
)
);
} else if f_arg_is_some {
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
`ok()` instead";
let self_snippet = snippet(cx, recv.span, "..");
span_lint_and_sugg(
return span_lint_and_sugg(
cx,
RESULT_MAP_OR_INTO_OPTION,
expr.span,
msg,
"try using `ok` instead",
format!("{0}.ok()", self_snippet),
Applicability::MachineApplicable,
)
} else {
// nothing to lint!
return;
);
}
}
9 changes: 1 addition & 8 deletions tests/ui/option_map_or_none.stderr
Expand Up @@ -13,14 +13,7 @@ LL | let _ :Option<i32> = opt.map_or(None, |x| {
| __________________________^
LL | | Some(x + 1)
LL | | });
| |_________________________^
|
help: try using `map` instead
|
LL ~ let _ :Option<i32> = opt.map(|x| {
LL + x + 1
LL ~ });
|
| |_________________________^ help: try using `map` instead: `opt.map(|x| x + 1)`

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

0 comments on commit 02e0726

Please sign in to comment.