]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/question_mark.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / question_mark.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::higher;
3 use clippy_utils::is_lang_ctor;
4 use clippy_utils::source::snippet_with_applicability;
5 use clippy_utils::sugg::Sugg;
6 use clippy_utils::ty::is_type_diagnostic_item;
7 use clippy_utils::{eq_expr_value, path_to_local_id};
8 use if_chain::if_chain;
9 use rustc_errors::Applicability;
10 use rustc_hir::LangItem::{OptionNone, OptionSome};
11 use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, PatKind, StmtKind};
12 use rustc_lint::{LateContext, LateLintPass};
13 use rustc_session::{declare_lint_pass, declare_tool_lint};
14 use rustc_span::sym;
15
16 declare_clippy_lint! {
17 /// ### What it does
18 /// Checks for expressions that could be replaced by the question mark operator.
19 ///
20 /// ### Why is this bad?
21 /// Question mark usage is more idiomatic.
22 ///
23 /// ### Example
24 /// ```ignore
25 /// if option.is_none() {
26 /// return None;
27 /// }
28 /// ```
29 ///
30 /// Could be written:
31 ///
32 /// ```ignore
33 /// option?;
34 /// ```
35 pub QUESTION_MARK,
36 style,
37 "checks for expressions that could be replaced by the question mark operator"
38 }
39
40 declare_lint_pass!(QuestionMark => [QUESTION_MARK]);
41
42 impl QuestionMark {
43 /// Checks if the given expression on the given context matches the following structure:
44 ///
45 /// ```ignore
46 /// if option.is_none() {
47 /// return None;
48 /// }
49 /// ```
50 ///
51 /// If it matches, it will suggest to use the question mark operator instead
52 fn check_is_none_and_early_return_none(cx: &LateContext<'_>, expr: &Expr<'_>) {
53 if_chain! {
54 if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr);
55 if let ExprKind::MethodCall(segment, _, args, _) = &cond.kind;
56 if segment.ident.name == sym!(is_none);
57 if Self::expression_returns_none(cx, then);
58 if let Some(subject) = args.get(0);
59 if Self::is_option(cx, subject);
60
61 then {
62 let mut applicability = Applicability::MachineApplicable;
63 let receiver_str = &Sugg::hir_with_applicability(cx, subject, "..", &mut applicability);
64 let mut replacement: Option<String> = None;
65 if let Some(else_inner) = r#else {
66 if_chain! {
67 if let ExprKind::Block(block, None) = &else_inner.kind;
68 if block.stmts.is_empty();
69 if let Some(block_expr) = &block.expr;
70 if eq_expr_value(cx, subject, block_expr);
71 then {
72 replacement = Some(format!("Some({}?)", receiver_str));
73 }
74 }
75 } else if Self::moves_by_default(cx, subject)
76 && !matches!(subject.kind, ExprKind::Call(..) | ExprKind::MethodCall(..))
77 {
78 replacement = Some(format!("{}.as_ref()?;", receiver_str));
79 } else {
80 replacement = Some(format!("{}?;", receiver_str));
81 }
82
83 if let Some(replacement_str) = replacement {
84 span_lint_and_sugg(
85 cx,
86 QUESTION_MARK,
87 expr.span,
88 "this block may be rewritten with the `?` operator",
89 "replace it with",
90 replacement_str,
91 applicability,
92 );
93 }
94 }
95 }
96 }
97
98 fn check_if_let_some_and_early_return_none(cx: &LateContext<'_>, expr: &Expr<'_>) {
99 if_chain! {
100 if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else: Some(if_else) }) = higher::IfLet::hir(cx, expr);
101 if Self::is_option(cx, let_expr);
102
103 if let PatKind::TupleStruct(ref path1, fields, None) = let_pat.kind;
104 if is_lang_ctor(cx, path1, OptionSome);
105 if let PatKind::Binding(annot, bind_id, _, _) = fields[0].kind;
106 let by_ref = matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut);
107
108 if let ExprKind::Block(ref block, None) = if_then.kind;
109 if block.stmts.is_empty();
110 if let Some(trailing_expr) = &block.expr;
111 if path_to_local_id(trailing_expr, bind_id);
112
113 if Self::expression_returns_none(cx, if_else);
114 then {
115 let mut applicability = Applicability::MachineApplicable;
116 let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
117 let replacement = format!(
118 "{}{}?",
119 receiver_str,
120 if by_ref { ".as_ref()" } else { "" },
121 );
122
123 span_lint_and_sugg(
124 cx,
125 QUESTION_MARK,
126 expr.span,
127 "this if-let-else may be rewritten with the `?` operator",
128 "replace it with",
129 replacement,
130 applicability,
131 );
132 }
133 }
134 }
135
136 fn moves_by_default(cx: &LateContext<'_>, expression: &Expr<'_>) -> bool {
137 let expr_ty = cx.typeck_results().expr_ty(expression);
138
139 !expr_ty.is_copy_modulo_regions(cx.tcx.at(expression.span), cx.param_env)
140 }
141
142 fn is_option(cx: &LateContext<'_>, expression: &Expr<'_>) -> bool {
143 let expr_ty = cx.typeck_results().expr_ty(expression);
144
145 is_type_diagnostic_item(cx, expr_ty, sym::option_type)
146 }
147
148 fn expression_returns_none(cx: &LateContext<'_>, expression: &Expr<'_>) -> bool {
149 match expression.kind {
150 ExprKind::Block(block, _) => {
151 if let Some(return_expression) = Self::return_expression(block) {
152 return Self::expression_returns_none(cx, return_expression);
153 }
154
155 false
156 },
157 ExprKind::Ret(Some(expr)) => Self::expression_returns_none(cx, expr),
158 ExprKind::Path(ref qpath) => is_lang_ctor(cx, qpath, OptionNone),
159 _ => false,
160 }
161 }
162
163 fn return_expression<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
164 // Check if last expression is a return statement. Then, return the expression
165 if_chain! {
166 if block.stmts.len() == 1;
167 if let Some(expr) = block.stmts.iter().last();
168 if let StmtKind::Semi(expr) = expr.kind;
169 if let ExprKind::Ret(Some(ret_expr)) = expr.kind;
170
171 then {
172 return Some(ret_expr);
173 }
174 }
175
176 // Check for `return` without a semicolon.
177 if_chain! {
178 if block.stmts.is_empty();
179 if let Some(ExprKind::Ret(Some(ret_expr))) = block.expr.as_ref().map(|e| &e.kind);
180 then {
181 return Some(ret_expr);
182 }
183 }
184
185 None
186 }
187 }
188
189 impl<'tcx> LateLintPass<'tcx> for QuestionMark {
190 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
191 Self::check_is_none_and_early_return_none(cx, expr);
192 Self::check_if_let_some_and_early_return_none(cx, expr);
193 }
194 }