1use clippy_utils::diagnostics::span_lint_and_then;
2use clippy_utils::ty::is_type_diagnostic_item;
3use rustc_hir::{CaptureBy, Closure, Expr, ExprKind, PatKind};
4use rustc_lint::LateContext;
5use rustc_span::sym;
6
7use super::MAP_ERR_IGNORE;
8
9pub(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

Provided by KDAB

Privacy Policy
Learn Rust with the experts
Find out more