mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 07:00:55 +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,42 +2573,48 @@ fn lint_map_or_none<'a, 'tcx>(
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
let mess = if is_option && default_arg_is_none {
|
let (lint, msg, instead, hint) = {
|
||||||
let self_snippet = snippet(cx, map_or_args[0].span, "..");
|
if !default_arg_is_none {
|
||||||
let func_snippet = snippet(cx, map_or_args[2].span, "..");
|
// nothing to lint!
|
||||||
let msg = "called `map_or(None, f)` on an `Option` value. This can be done more directly by calling \
|
return;
|
||||||
`and_then(f)` instead";
|
}
|
||||||
Some((
|
|
||||||
OPTION_MAP_OR_NONE,
|
if is_option {
|
||||||
msg,
|
let self_snippet = snippet(cx, map_or_args[0].span, "..");
|
||||||
"try using `and_then` instead",
|
let func_snippet = snippet(cx, map_or_args[2].span, "..");
|
||||||
format!("{0}.and_then({1})", self_snippet, func_snippet),
|
let msg = "called `map_or(None, f)` on an `Option` value. This can be done more directly by calling \
|
||||||
))
|
`and_then(f)` instead";
|
||||||
} else if is_result && f_arg_is_some {
|
(
|
||||||
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
|
OPTION_MAP_OR_NONE,
|
||||||
`ok()` instead";
|
msg,
|
||||||
let self_snippet = snippet(cx, map_or_args[0].span, "..");
|
"try using `and_then` instead",
|
||||||
Some((
|
format!("{0}.and_then({1})", self_snippet, func_snippet),
|
||||||
RESULT_MAP_OR_INTO_OPTION,
|
)
|
||||||
msg,
|
} else if f_arg_is_some {
|
||||||
"try using `ok` instead",
|
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
|
||||||
format!("{0}.ok()", self_snippet),
|
`ok()` instead";
|
||||||
))
|
let self_snippet = snippet(cx, map_or_args[0].span, "..");
|
||||||
} else {
|
(
|
||||||
None
|
RESULT_MAP_OR_INTO_OPTION,
|
||||||
|
msg,
|
||||||
|
"try using `ok` instead",
|
||||||
|
format!("{0}.ok()", self_snippet),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
// nothing to lint!
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some((lint, msg, instead, hint)) = mess {
|
span_lint_and_sugg(
|
||||||
span_lint_and_sugg(
|
cx,
|
||||||
cx,
|
lint,
|
||||||
lint,
|
expr.span,
|
||||||
expr.span,
|
msg,
|
||||||
msg,
|
instead,
|
||||||
instead,
|
hint,
|
||||||
hint,
|
Applicability::MachineApplicable,
|
||||||
Applicability::MachineApplicable,
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lint use of `_.and_then(|x| Some(y))` for `Option`s
|
/// 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: Result<u32, &str> = Ok(1);
|
||||||
let _ = opt.ok();
|
let _ = opt.ok();
|
||||||
|
|
||||||
let rewrap = |s: u32| -> Option<u32> {
|
let rewrap = |s: u32| -> Option<u32> { Some(s) };
|
||||||
Some(s)
|
|
||||||
};
|
|
||||||
|
|
||||||
// A non-Some `f` arg should not emit the lint
|
// A non-Some `f` arg should not emit the lint
|
||||||
let opt: Result<u32, &str> = Ok(1);
|
let opt: Result<u32, &str> = Ok(1);
|
||||||
|
|
Loading…
Reference in a new issue