-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #11845 - GuillaumeGomez:result_map_or_into_option-exten…
…sion, r=flip1995 Extend `result_map_or_into_option` lint to handle `Result::map_or_else(|_| None, Some)` Fixes #10365. As indicated in the title, it extends the `result_map_or_into_option` lint to handle `Result::map_or_else(|_| None, Some)`. changelog: extension of the `result_map_or_into_option` lint to handle `Result::map_or_else(|_| None, Some)` r? `@blyxyas`
- Loading branch information
Showing
5 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use clippy_utils::{is_res_lang_ctor, path_res, peel_blocks}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_hir::LangItem::{OptionNone, OptionSome}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym; | ||
|
||
use super::RESULT_MAP_OR_INTO_OPTION; | ||
|
||
/// lint use of `_.map_or_else(|_| None, Some)` for `Result`s | ||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'_>, | ||
recv: &'tcx hir::Expr<'_>, | ||
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 | ||
&& let hir::ExprKind::Closure(&hir::Closure { body, .. }) = def_arg.kind | ||
&& let body = cx.tcx.hir().body(body) | ||
&& 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"; | ||
let self_snippet = snippet(cx, recv.span, ".."); | ||
span_lint_and_sugg( | ||
cx, | ||
RESULT_MAP_OR_INTO_OPTION, | ||
expr.span, | ||
msg, | ||
"try using `ok` instead", | ||
format!("{self_snippet}.ok()"), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters