]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/question_mark.rs
Merge remote-tracking branch 'upstream/master' into rustup
[rust.git] / clippy_lints / src / question_mark.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::higher;
3 use clippy_utils::source::snippet_with_applicability;
4 use clippy_utils::ty::is_type_diagnostic_item;
5 use clippy_utils::{
6     eq_expr_value, get_parent_node, in_constant, is_else_clause, is_lang_ctor, path_to_local, path_to_local_id,
7     peel_blocks, peel_blocks_with_stmt,
8 };
9 use if_chain::if_chain;
10 use rustc_errors::Applicability;
11 use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr, ResultOk};
12 use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, Node, PatKind, PathSegment, QPath};
13 use rustc_lint::{LateContext, LateLintPass};
14 use rustc_middle::ty::Ty;
15 use rustc_session::{declare_lint_pass, declare_tool_lint};
16 use rustc_span::{sym, symbol::Symbol};
17
18 declare_clippy_lint! {
19     /// ### What it does
20     /// Checks for expressions that could be replaced by the question mark operator.
21     ///
22     /// ### Why is this bad?
23     /// Question mark usage is more idiomatic.
24     ///
25     /// ### Example
26     /// ```ignore
27     /// if option.is_none() {
28     ///     return None;
29     /// }
30     /// ```
31     ///
32     /// Could be written:
33     ///
34     /// ```ignore
35     /// option?;
36     /// ```
37     #[clippy::version = "pre 1.29.0"]
38     pub QUESTION_MARK,
39     style,
40     "checks for expressions that could be replaced by the question mark operator"
41 }
42
43 declare_lint_pass!(QuestionMark => [QUESTION_MARK]);
44
45 enum IfBlockType<'hir> {
46     /// An `if x.is_xxx() { a } else { b } ` expression.
47     ///
48     /// Contains: caller (x), caller_type, call_sym (is_xxx), if_then (a), if_else (b)
49     IfIs(
50         &'hir Expr<'hir>,
51         Ty<'hir>,
52         Symbol,
53         &'hir Expr<'hir>,
54         Option<&'hir Expr<'hir>>,
55     ),
56     /// An `if let Xxx(a) = b { c } else { d }` expression.
57     ///
58     /// Contains: let_pat_qpath (Xxx), let_pat_type, let_pat_sym (a), let_expr (b), if_then (c),
59     /// if_else (d)
60     IfLet(
61         &'hir QPath<'hir>,
62         Ty<'hir>,
63         Symbol,
64         &'hir Expr<'hir>,
65         &'hir Expr<'hir>,
66         Option<&'hir Expr<'hir>>,
67     ),
68 }
69
70 /// Checks if the given expression on the given context matches the following structure:
71 ///
72 /// ```ignore
73 /// if option.is_none() {
74 ///    return None;
75 /// }
76 /// ```
77 ///
78 /// ```ignore
79 /// if result.is_err() {
80 ///     return result;
81 /// }
82 /// ```
83 ///
84 /// If it matches, it will suggest to use the question mark operator instead
85 fn check_is_none_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
86     if_chain! {
87         if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr);
88         if !is_else_clause(cx.tcx, expr);
89         if let ExprKind::MethodCall(segment, caller, ..) = &cond.kind;
90         let caller_ty = cx.typeck_results().expr_ty(caller);
91         let if_block = IfBlockType::IfIs(caller, caller_ty, segment.ident.name, then, r#else);
92         if is_early_return(sym::Option, cx, &if_block) || is_early_return(sym::Result, cx, &if_block);
93         then {
94             let mut applicability = Applicability::MachineApplicable;
95             let receiver_str = snippet_with_applicability(cx, caller.span, "..", &mut applicability);
96             let by_ref = !caller_ty.is_copy_modulo_regions(cx.tcx.at(caller.span), cx.param_env) &&
97                 !matches!(caller.kind, ExprKind::Call(..) | ExprKind::MethodCall(..));
98             let sugg = if let Some(else_inner) = r#else {
99                 if eq_expr_value(cx, caller, peel_blocks(else_inner)) {
100                     format!("Some({receiver_str}?)")
101                 } else {
102                     return;
103                 }
104             } else {
105                 format!("{receiver_str}{}?;", if by_ref { ".as_ref()" } else { "" })
106             };
107
108             span_lint_and_sugg(
109                 cx,
110                 QUESTION_MARK,
111                 expr.span,
112                 "this block may be rewritten with the `?` operator",
113                 "replace it with",
114                 sugg,
115                 applicability,
116             );
117         }
118     }
119 }
120
121 fn check_if_let_some_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
122     if_chain! {
123         if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else }) = higher::IfLet::hir(cx, expr);
124         if !is_else_clause(cx.tcx, expr);
125         if let PatKind::TupleStruct(ref path1, [field], ddpos) = let_pat.kind;
126         if ddpos.as_opt_usize().is_none();
127         if let PatKind::Binding(BindingAnnotation(by_ref, _), bind_id, ident, None) = field.kind;
128         let caller_ty = cx.typeck_results().expr_ty(let_expr);
129         let if_block = IfBlockType::IfLet(path1, caller_ty, ident.name, let_expr, if_then, if_else);
130         if (is_early_return(sym::Option, cx, &if_block) && path_to_local_id(peel_blocks(if_then), bind_id))
131             || is_early_return(sym::Result, cx, &if_block);
132         if if_else.map(|e| eq_expr_value(cx, let_expr, peel_blocks(e))).filter(|e| *e).is_none();
133         then {
134             let mut applicability = Applicability::MachineApplicable;
135             let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
136             let requires_semi = matches!(get_parent_node(cx.tcx, expr.hir_id), Some(Node::Stmt(_)));
137             let sugg = format!(
138                 "{receiver_str}{}?{}",
139                 if by_ref == ByRef::Yes { ".as_ref()" } else { "" },
140                 if requires_semi { ";" } else { "" }
141             );
142             span_lint_and_sugg(
143                 cx,
144                 QUESTION_MARK,
145                 expr.span,
146                 "this block may be rewritten with the `?` operator",
147                 "replace it with",
148                 sugg,
149                 applicability,
150             );
151         }
152     }
153 }
154
155 fn is_early_return(smbl: Symbol, cx: &LateContext<'_>, if_block: &IfBlockType<'_>) -> bool {
156     match *if_block {
157         IfBlockType::IfIs(caller, caller_ty, call_sym, if_then, _) => {
158             // If the block could be identified as `if x.is_none()/is_err()`,
159             // we then only need to check the if_then return to see if it is none/err.
160             is_type_diagnostic_item(cx, caller_ty, smbl)
161                 && expr_return_none_or_err(smbl, cx, if_then, caller, None)
162                 && match smbl {
163                     sym::Option => call_sym == sym!(is_none),
164                     sym::Result => call_sym == sym!(is_err),
165                     _ => false,
166                 }
167         },
168         IfBlockType::IfLet(qpath, let_expr_ty, let_pat_sym, let_expr, if_then, if_else) => {
169             is_type_diagnostic_item(cx, let_expr_ty, smbl)
170                 && match smbl {
171                     sym::Option => {
172                         // We only need to check `if let Some(x) = option` not `if let None = option`,
173                         // because the later one will be suggested as `if option.is_none()` thus causing conflict.
174                         is_lang_ctor(cx, qpath, OptionSome)
175                             && if_else.is_some()
176                             && expr_return_none_or_err(smbl, cx, if_else.unwrap(), let_expr, None)
177                     },
178                     sym::Result => {
179                         (is_lang_ctor(cx, qpath, ResultOk)
180                             && if_else.is_some()
181                             && expr_return_none_or_err(smbl, cx, if_else.unwrap(), let_expr, Some(let_pat_sym)))
182                             || is_lang_ctor(cx, qpath, ResultErr)
183                                 && expr_return_none_or_err(smbl, cx, if_then, let_expr, Some(let_pat_sym))
184                     },
185                     _ => false,
186                 }
187         },
188     }
189 }
190
191 fn expr_return_none_or_err(
192     smbl: Symbol,
193     cx: &LateContext<'_>,
194     expr: &Expr<'_>,
195     cond_expr: &Expr<'_>,
196     err_sym: Option<Symbol>,
197 ) -> bool {
198     match peel_blocks_with_stmt(expr).kind {
199         ExprKind::Ret(Some(ret_expr)) => expr_return_none_or_err(smbl, cx, ret_expr, cond_expr, err_sym),
200         ExprKind::Path(ref qpath) => match smbl {
201             sym::Option => is_lang_ctor(cx, qpath, OptionNone),
202             sym::Result => path_to_local(expr).is_some() && path_to_local(expr) == path_to_local(cond_expr),
203             _ => false,
204         },
205         ExprKind::Call(call_expr, args_expr) => {
206             if_chain! {
207                 if smbl == sym::Result;
208                 if let ExprKind::Path(QPath::Resolved(_, path)) = &call_expr.kind;
209                 if let Some(segment) = path.segments.first();
210                 if let Some(err_sym) = err_sym;
211                 if let Some(arg) = args_expr.first();
212                 if let ExprKind::Path(QPath::Resolved(_, arg_path)) = &arg.kind;
213                 if let Some(PathSegment { ident, .. }) = arg_path.segments.first();
214                 then {
215                     return segment.ident.name == sym::Err && err_sym == ident.name;
216                 }
217             }
218             false
219         },
220         _ => false,
221     }
222 }
223
224 impl<'tcx> LateLintPass<'tcx> for QuestionMark {
225     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
226         if !in_constant(cx, expr.hir_id) {
227             check_is_none_or_err_and_early_return(cx, expr);
228             check_if_let_some_or_err_and_early_return(cx, expr);
229         }
230     }
231 }