1 | use clippy_utils::diagnostics::span_lint_and_then; |
---|---|
2 | use clippy_utils::ty::is_type_diagnostic_item; |
3 | use rustc_hir::{CaptureBy, Closure, Expr, ExprKind, PatKind}; |
4 | use rustc_lint::LateContext; |
5 | use rustc_span::sym; |
6 | |
7 | use super::MAP_ERR_IGNORE; |
8 | |
9 | pub(super) fn check(cx: &LateContext<'_>, e: &Expr<'_>, arg: &Expr<'_>) { |
10 | if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id) |
11 | && let Some(impl_id) = cx.tcx.impl_of_method(method_id) |
12 | && is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id).instantiate_identity(), sym::Result) |
13 | && let ExprKind::Closure(&Closure { |
14 | capture_clause: CaptureBy::Ref, |
15 | body, |
16 | fn_decl_span, |
17 | .. |
18 | }) = arg.kind |
19 | && let closure_body = cx.tcx.hir_body(body) |
20 | && let [param] = closure_body.params |
21 | && let PatKind::Wild = param.pat.kind |
22 | { |
23 | // span the area of the closure capture and warn that the |
24 | // original error will be thrown away |
25 | #[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")] |
26 | span_lint_and_then( |
27 | cx, |
28 | MAP_ERR_IGNORE, |
29 | fn_decl_span, |
30 | "`map_err(|_|...` wildcard pattern discards the original error", |
31 | |diag| { |
32 | diag.help( |
33 | "consider storing the original error as a source in the new error, or silence this warning using an ignored identifier (`.map_err(|_foo| ...`)", |
34 | ); |
35 | }, |
36 | ); |
37 | } |
38 | } |
39 |
Definitions
Learn Rust with the experts
Find out more