Skip to content

Simplify code for result_map_or_else_none #11866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions clippy_lints/src/methods/result_map_or_else_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ pub(super) fn check<'tcx>(
def_arg: &'tcx hir::Expr<'_>,
map_arg: &'tcx hir::Expr<'_>,
) {
let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);

if !is_result {
return;
}

let f_arg_is_some = is_res_lang_ctor(cx, path_res(cx, map_arg), OptionSome);

if f_arg_is_some
// lint if the caller of `map_or_else()` is a `Result`
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result)
// We check that it is mapped as `Some`.
&& is_res_lang_ctor(cx, path_res(cx, map_arg), OptionSome)
&& let hir::ExprKind::Closure(&hir::Closure { body, .. }) = def_arg.kind
&& let body = cx.tcx.hir().body(body)
// And finally we check that we return a `None` in the "else case".
&& is_res_lang_ctor(cx, path_res(cx, peel_blocks(body.value)), OptionNone)
{
let msg = "called `map_or_else(|_| None, Some)` on a `Result` value";
Expand Down