result_map_or_into_option: destructure lint tuple or return early

This commit is contained in:
Nick Torres 2020-04-04 13:53:08 -07:00
parent 3a29aedf8d
commit d0738bd673
2 changed files with 41 additions and 37 deletions

View file

@ -2573,32 +2573,39 @@ fn lint_map_or_none<'a, 'tcx>(
false
};
let mess = if is_option && default_arg_is_none {
let (lint, msg, instead, hint) = {
if !default_arg_is_none {
// nothing to lint!
return;
}
if is_option {
let self_snippet = snippet(cx, map_or_args[0].span, "..");
let func_snippet = snippet(cx, map_or_args[2].span, "..");
let msg = "called `map_or(None, f)` on an `Option` value. This can be done more directly by calling \
`and_then(f)` instead";
Some((
(
OPTION_MAP_OR_NONE,
msg,
"try using `and_then` instead",
format!("{0}.and_then({1})", self_snippet, func_snippet),
))
} else if is_result && f_arg_is_some {
)
} 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, map_or_args[0].span, "..");
Some((
(
RESULT_MAP_OR_INTO_OPTION,
msg,
"try using `ok` instead",
format!("{0}.ok()", self_snippet),
))
)
} else {
None
// nothing to lint!
return;
}
};
if let Some((lint, msg, instead, hint)) = mess {
span_lint_and_sugg(
cx,
lint,
@ -2609,7 +2616,6 @@ fn lint_map_or_none<'a, 'tcx>(
Applicability::MachineApplicable,
);
}
}
/// Lint use of `_.and_then(|x| Some(y))` for `Option`s
fn lint_option_and_then_some(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {

View file

@ -6,9 +6,7 @@ fn main() {
let opt: Result<u32, &str> = Ok(1);
let _ = opt.ok();
let rewrap = |s: u32| -> Option<u32> {
Some(s)
};
let rewrap = |s: u32| -> Option<u32> { Some(s) };
// A non-Some `f` arg should not emit the lint
let opt: Result<u32, &str> = Ok(1);