Skip to content

Commit d0738bd

Browse files
committed
result_map_or_into_option: destructure lint tuple or return early
1 parent 3a29aed commit d0738bd

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,42 +2573,48 @@ fn lint_map_or_none<'a, 'tcx>(
25732573
false
25742574
};
25752575

2576-
let mess = if is_option && default_arg_is_none {
2577-
let self_snippet = snippet(cx, map_or_args[0].span, "..");
2578-
let func_snippet = snippet(cx, map_or_args[2].span, "..");
2579-
let msg = "called `map_or(None, f)` on an `Option` value. This can be done more directly by calling \
2580-
`and_then(f)` instead";
2581-
Some((
2582-
OPTION_MAP_OR_NONE,
2583-
msg,
2584-
"try using `and_then` instead",
2585-
format!("{0}.and_then({1})", self_snippet, func_snippet),
2586-
))
2587-
} else if is_result && f_arg_is_some {
2588-
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
2589-
`ok()` instead";
2590-
let self_snippet = snippet(cx, map_or_args[0].span, "..");
2591-
Some((
2592-
RESULT_MAP_OR_INTO_OPTION,
2593-
msg,
2594-
"try using `ok` instead",
2595-
format!("{0}.ok()", self_snippet),
2596-
))
2597-
} else {
2598-
None
2576+
let (lint, msg, instead, hint) = {
2577+
if !default_arg_is_none {
2578+
// nothing to lint!
2579+
return;
2580+
}
2581+
2582+
if is_option {
2583+
let self_snippet = snippet(cx, map_or_args[0].span, "..");
2584+
let func_snippet = snippet(cx, map_or_args[2].span, "..");
2585+
let msg = "called `map_or(None, f)` on an `Option` value. This can be done more directly by calling \
2586+
`and_then(f)` instead";
2587+
(
2588+
OPTION_MAP_OR_NONE,
2589+
msg,
2590+
"try using `and_then` instead",
2591+
format!("{0}.and_then({1})", self_snippet, func_snippet),
2592+
)
2593+
} else if f_arg_is_some {
2594+
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
2595+
`ok()` instead";
2596+
let self_snippet = snippet(cx, map_or_args[0].span, "..");
2597+
(
2598+
RESULT_MAP_OR_INTO_OPTION,
2599+
msg,
2600+
"try using `ok` instead",
2601+
format!("{0}.ok()", self_snippet),
2602+
)
2603+
} else {
2604+
// nothing to lint!
2605+
return;
2606+
}
25992607
};
26002608

2601-
if let Some((lint, msg, instead, hint)) = mess {
2602-
span_lint_and_sugg(
2603-
cx,
2604-
lint,
2605-
expr.span,
2606-
msg,
2607-
instead,
2608-
hint,
2609-
Applicability::MachineApplicable,
2610-
);
2611-
}
2609+
span_lint_and_sugg(
2610+
cx,
2611+
lint,
2612+
expr.span,
2613+
msg,
2614+
instead,
2615+
hint,
2616+
Applicability::MachineApplicable,
2617+
);
26122618
}
26132619

26142620
/// Lint use of `_.and_then(|x| Some(y))` for `Option`s

tests/ui/result_map_or_into_option.fixed

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ fn main() {
66
let opt: Result<u32, &str> = Ok(1);
77
let _ = opt.ok();
88

9-
let rewrap = |s: u32| -> Option<u32> {
10-
Some(s)
11-
};
9+
let rewrap = |s: u32| -> Option<u32> { Some(s) };
1210

1311
// A non-Some `f` arg should not emit the lint
1412
let opt: Result<u32, &str> = Ok(1);

0 commit comments

Comments
 (0)