mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 13:13:34 +00:00
result_map_or_into_option: destructure lint tuple or return early
This commit is contained in:
parent
3a29aedf8d
commit
d0738bd673
2 changed files with 41 additions and 37 deletions
|
@ -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,
|
||||
|
@ -2608,7 +2615,6 @@ fn lint_map_or_none<'a, 'tcx>(
|
|||
hint,
|
||||
Applicability::MachineApplicable,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Lint use of `_.and_then(|x| Some(y))` for `Option`s
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue