]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/map_err_ignore.rs
1fb6617145e718bb0a9f116f955a3c5a73a30f09
[rust.git] / clippy_lints / src / methods / map_err_ignore.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
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<'tcx>(cx: &LateContext<'_>, e: &Expr<'_>, arg: &'tcx 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), 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         span_lint_and_help(
26             cx,
27             MAP_ERR_IGNORE,
28             fn_decl_span,
29             "`map_err(|_|...` wildcard pattern discards the original error",
30             None,
31             "consider storing the original error as a source in the new error, or silence this warning using an ignored identifier (`.map_err(|_foo| ...`)",
32         );
33     }
34 }