]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/map_err_ignore.rs
Auto merge of #82680 - jturner314:div_euclid-docs, r=JohnTitor
[rust.git] / src / tools / clippy / clippy_lints / src / map_err_ignore.rs
1 use crate::utils::span_lint_and_help;
2
3 use rustc_hir::{CaptureBy, Expr, ExprKind, PatKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6
7 declare_clippy_lint! {
8     /// **What it does:** Checks for instances of `map_err(|_| Some::Enum)`
9     ///
10     /// **Why is this bad?** This `map_err` throws away the original error rather than allowing the enum to contain and report the cause of the error
11     ///
12     /// **Known problems:** None.
13     ///
14     /// **Example:**
15     /// Before:
16     /// ```rust
17     /// use std::fmt;
18     ///
19     /// #[derive(Debug)]
20     /// enum Error {
21     ///     Indivisible,
22     ///     Remainder(u8),
23     /// }
24     ///
25     /// impl fmt::Display for Error {
26     ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27     ///         match self {
28     ///             Error::Indivisible => write!(f, "could not divide input by three"),
29     ///             Error::Remainder(remainder) => write!(
30     ///                 f,
31     ///                 "input is not divisible by three, remainder = {}",
32     ///                 remainder
33     ///             ),
34     ///         }
35     ///     }
36     /// }
37     ///
38     /// impl std::error::Error for Error {}
39     ///
40     /// fn divisible_by_3(input: &str) -> Result<(), Error> {
41     ///     input
42     ///         .parse::<i32>()
43     ///         .map_err(|_| Error::Indivisible)
44     ///         .map(|v| v % 3)
45     ///         .and_then(|remainder| {
46     ///             if remainder == 0 {
47     ///                 Ok(())
48     ///             } else {
49     ///                 Err(Error::Remainder(remainder as u8))
50     ///             }
51     ///         })
52     /// }
53     ///  ```
54     ///
55     ///  After:
56     ///  ```rust
57     /// use std::{fmt, num::ParseIntError};
58     ///
59     /// #[derive(Debug)]
60     /// enum Error {
61     ///     Indivisible(ParseIntError),
62     ///     Remainder(u8),
63     /// }
64     ///
65     /// impl fmt::Display for Error {
66     ///     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67     ///         match self {
68     ///             Error::Indivisible(_) => write!(f, "could not divide input by three"),
69     ///             Error::Remainder(remainder) => write!(
70     ///                 f,
71     ///                 "input is not divisible by three, remainder = {}",
72     ///                 remainder
73     ///             ),
74     ///         }
75     ///     }
76     /// }
77     ///
78     /// impl std::error::Error for Error {
79     ///     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
80     ///         match self {
81     ///             Error::Indivisible(source) => Some(source),
82     ///             _ => None,
83     ///         }
84     ///     }
85     /// }
86     ///
87     /// fn divisible_by_3(input: &str) -> Result<(), Error> {
88     ///     input
89     ///         .parse::<i32>()
90     ///         .map_err(Error::Indivisible)
91     ///         .map(|v| v % 3)
92     ///         .and_then(|remainder| {
93     ///             if remainder == 0 {
94     ///                 Ok(())
95     ///             } else {
96     ///                 Err(Error::Remainder(remainder as u8))
97     ///             }
98     ///         })
99     /// }
100     /// ```
101     pub MAP_ERR_IGNORE,
102     restriction,
103     "`map_err` should not ignore the original error"
104 }
105
106 declare_lint_pass!(MapErrIgnore => [MAP_ERR_IGNORE]);
107
108 impl<'tcx> LateLintPass<'tcx> for MapErrIgnore {
109     // do not try to lint if this is from a macro or desugaring
110     fn check_expr(&mut self, cx: &LateContext<'_>, e: &Expr<'_>) {
111         if e.span.from_expansion() {
112             return;
113         }
114
115         // check if this is a method call (e.g. x.foo())
116         if let ExprKind::MethodCall(ref method, _t_span, ref args, _) = e.kind {
117             // only work if the method name is `map_err` and there are only 2 arguments (e.g. x.map_err(|_|[1]
118             // Enum::Variant[2]))
119             if method.ident.as_str() == "map_err" && args.len() == 2 {
120                 // make sure the first argument is a closure, and grab the CaptureRef, body_id, and body_span fields
121                 if let ExprKind::Closure(capture, _, body_id, body_span, _) = args[1].kind {
122                     // check if this is by Reference (meaning there's no move statement)
123                     if capture == CaptureBy::Ref {
124                         // Get the closure body to check the parameters and values
125                         let closure_body = cx.tcx.hir().body(body_id);
126                         // make sure there's only one parameter (`|_|`)
127                         if closure_body.params.len() == 1 {
128                             // make sure that parameter is the wild token (`_`)
129                             if let PatKind::Wild = closure_body.params[0].pat.kind {
130                                 // span the area of the closure capture and warn that the
131                                 // original error will be thrown away
132                                 span_lint_and_help(
133                                     cx,
134                                     MAP_ERR_IGNORE,
135                                     body_span,
136                                     "`map_err(|_|...` wildcard pattern discards the original error",
137                                     None,
138                                     "consider storing the original error as a source in the new error, or silence this warning using an ignored identifier (`.map_err(|_foo| ...`)",
139                                 );
140                             }
141                         }
142                     }
143                 }
144             }
145         }
146     }
147 }