-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
unnecessary_result_map_or_else
lint
- Loading branch information
1 parent
37947ff
commit 3b8f62f
Showing
4 changed files
with
125 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
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
95 changes: 95 additions & 0 deletions
95
clippy_lints/src/methods/unnecessary_result_map_or_else.rs
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,95 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::peel_blocks; | ||
use clippy_utils::source::snippet; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_errors::Applicability; | ||
use rustc_hir as hir; | ||
use rustc_hir::{Closure, Expr, ExprKind, HirId, QPath, Stmt, StmtKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::symbol::sym; | ||
|
||
use super::UNNECESSARY_RESULT_MAP_OR_ELSE; | ||
|
||
fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, def_arg: &Expr<'_>) { | ||
let msg = "unused \"map closure\" when calling `Result::map_or_else` value"; | ||
let self_snippet = snippet(cx, recv.span, ".."); | ||
let err_snippet = snippet(cx, def_arg.span, ".."); | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_RESULT_MAP_OR_ELSE, | ||
expr.span, | ||
msg, | ||
"consider using `unwrap_or_else`", | ||
format!("{self_snippet}.unwrap_or_else({err_snippet})"), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
|
||
fn get_last_chain_binding_hir_id(mut hir_id: HirId, statements: &[Stmt<'_>]) -> Option<HirId> { | ||
for stmt in statements { | ||
if let StmtKind::Local(local) = stmt.kind | ||
&& let Some(init) = local.init | ||
&& let ExprKind::Path(QPath::Resolved(_, path)) = init.kind | ||
&& let hir::def::Res::Local(local_hir_id) = path.res | ||
&& local_hir_id == hir_id | ||
{ | ||
hir_id = local.pat.hir_id; | ||
} else { | ||
return None; | ||
} | ||
} | ||
Some(hir_id) | ||
} | ||
|
||
fn handle_qpath( | ||
cx: &LateContext<'_>, | ||
expr: &Expr<'_>, | ||
recv: &Expr<'_>, | ||
def_arg: &Expr<'_>, | ||
expected_hir_id: HirId, | ||
qpath: QPath<'_>, | ||
) { | ||
if let QPath::Resolved(_, path) = qpath | ||
&& let hir::def::Res::Local(hir_id) = path.res | ||
&& expected_hir_id == hir_id | ||
{ | ||
emit_lint(cx, expr, recv, def_arg); | ||
} | ||
} | ||
|
||
/// lint use of `_.map_or_else(|err| err, |n| n)` for `Result`s. | ||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'_>, | ||
recv: &'tcx Expr<'_>, | ||
def_arg: &'tcx Expr<'_>, | ||
map_arg: &'tcx Expr<'_>, | ||
) { | ||
// 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) | ||
&& let ExprKind::Closure(&Closure { body, .. }) = map_arg.kind | ||
&& let body = cx.tcx.hir().body(body) | ||
&& let Some(first_param) = body.params.first() | ||
{ | ||
let body_expr = peel_blocks(body.value); | ||
|
||
match body_expr.kind { | ||
ExprKind::Path(qpath) => { | ||
handle_qpath(cx, expr, recv, def_arg, first_param.pat.hir_id, qpath); | ||
}, | ||
// If this is a block (that wasn't peeled off), then it means there are statements. | ||
ExprKind::Block(block, _) => { | ||
if let Some(block_expr) = block.expr | ||
// First we ensure that this is a "binding chain" (each statement is a binding | ||
// of the previous one) and that it is a binding of the closure argument. | ||
&& let Some(last_chain_binding_id) = | ||
get_last_chain_binding_hir_id(first_param.pat.hir_id, block.stmts) | ||
&& let ExprKind::Path(qpath) = block_expr.kind | ||
{ | ||
handle_qpath(cx, expr, recv, def_arg, last_chain_binding_id, qpath); | ||
} | ||
}, | ||
_ => {}, | ||
} | ||
} | ||
} |