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;
6 eq_expr_value, get_parent_node, in_constant, is_else_clause, is_res_lang_ctor, path_to_local, path_to_local_id,
7 peel_blocks, peel_blocks_with_stmt,
9 use if_chain::if_chain;
10 use rustc_errors::Applicability;
11 use rustc_hir::def::Res;
12 use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr, ResultOk};
13 use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, Node, PatKind, PathSegment, QPath};
14 use rustc_lint::{LateContext, LateLintPass};
15 use rustc_middle::ty::Ty;
16 use rustc_session::{declare_lint_pass, declare_tool_lint};
17 use rustc_span::{sym, symbol::Symbol};
19 declare_clippy_lint! {
21 /// Checks for expressions that could be replaced by the question mark operator.
23 /// ### Why is this bad?
24 /// Question mark usage is more idiomatic.
28 /// if option.is_none() {
38 #[clippy::version = "pre 1.29.0"]
41 "checks for expressions that could be replaced by the question mark operator"
44 declare_lint_pass!(QuestionMark => [QUESTION_MARK]);
46 enum IfBlockType<'hir> {
47 /// An `if x.is_xxx() { a } else { b } ` expression.
49 /// Contains: caller (x), caller_type, call_sym (is_xxx), if_then (a), if_else (b)
55 Option<&'hir Expr<'hir>>,
57 /// An `if let Xxx(a) = b { c } else { d }` expression.
59 /// Contains: let_pat_qpath (Xxx), let_pat_type, let_pat_sym (a), let_expr (b), if_then (c),
67 Option<&'hir Expr<'hir>>,
71 /// Checks if the given expression on the given context matches the following structure:
74 /// if option.is_none() {
80 /// if result.is_err() {
85 /// If it matches, it will suggest to use the question mark operator instead
86 fn check_is_none_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
88 if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr);
89 if !is_else_clause(cx.tcx, expr);
90 if let ExprKind::MethodCall(segment, caller, ..) = &cond.kind;
91 let caller_ty = cx.typeck_results().expr_ty(caller);
92 let if_block = IfBlockType::IfIs(caller, caller_ty, segment.ident.name, then, r#else);
93 if is_early_return(sym::Option, cx, &if_block) || is_early_return(sym::Result, cx, &if_block);
95 let mut applicability = Applicability::MachineApplicable;
96 let receiver_str = snippet_with_applicability(cx, caller.span, "..", &mut applicability);
97 let by_ref = !caller_ty.is_copy_modulo_regions(cx.tcx, cx.param_env) &&
98 !matches!(caller.kind, ExprKind::Call(..) | ExprKind::MethodCall(..));
99 let sugg = if let Some(else_inner) = r#else {
100 if eq_expr_value(cx, caller, peel_blocks(else_inner)) {
101 format!("Some({receiver_str}?)")
106 format!("{receiver_str}{}?;", if by_ref { ".as_ref()" } else { "" })
113 "this block may be rewritten with the `?` operator",
122 fn check_if_let_some_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
124 if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else }) = higher::IfLet::hir(cx, expr);
125 if !is_else_clause(cx.tcx, expr);
126 if let PatKind::TupleStruct(ref path1, [field], ddpos) = let_pat.kind;
127 if ddpos.as_opt_usize().is_none();
128 if let PatKind::Binding(BindingAnnotation(by_ref, _), bind_id, ident, None) = field.kind;
129 let caller_ty = cx.typeck_results().expr_ty(let_expr);
130 let if_block = IfBlockType::IfLet(
131 cx.qpath_res(path1, let_pat.hir_id),
138 if (is_early_return(sym::Option, cx, &if_block) && path_to_local_id(peel_blocks(if_then), bind_id))
139 || is_early_return(sym::Result, cx, &if_block);
140 if if_else.map(|e| eq_expr_value(cx, let_expr, peel_blocks(e))).filter(|e| *e).is_none();
142 let mut applicability = Applicability::MachineApplicable;
143 let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
144 let requires_semi = matches!(get_parent_node(cx.tcx, expr.hir_id), Some(Node::Stmt(_)));
146 "{receiver_str}{}?{}",
147 if by_ref == ByRef::Yes { ".as_ref()" } else { "" },
148 if requires_semi { ";" } else { "" }
154 "this block may be rewritten with the `?` operator",
163 fn is_early_return(smbl: Symbol, cx: &LateContext<'_>, if_block: &IfBlockType<'_>) -> bool {
165 IfBlockType::IfIs(caller, caller_ty, call_sym, if_then, _) => {
166 // If the block could be identified as `if x.is_none()/is_err()`,
167 // we then only need to check the if_then return to see if it is none/err.
168 is_type_diagnostic_item(cx, caller_ty, smbl)
169 && expr_return_none_or_err(smbl, cx, if_then, caller, None)
171 sym::Option => call_sym == sym!(is_none),
172 sym::Result => call_sym == sym!(is_err),
176 IfBlockType::IfLet(res, let_expr_ty, let_pat_sym, let_expr, if_then, if_else) => {
177 is_type_diagnostic_item(cx, let_expr_ty, smbl)
180 // We only need to check `if let Some(x) = option` not `if let None = option`,
181 // because the later one will be suggested as `if option.is_none()` thus causing conflict.
182 is_res_lang_ctor(cx, res, OptionSome)
184 && expr_return_none_or_err(smbl, cx, if_else.unwrap(), let_expr, None)
187 (is_res_lang_ctor(cx, res, ResultOk)
189 && expr_return_none_or_err(smbl, cx, if_else.unwrap(), let_expr, Some(let_pat_sym)))
190 || is_res_lang_ctor(cx, res, ResultErr)
191 && expr_return_none_or_err(smbl, cx, if_then, let_expr, Some(let_pat_sym))
200 fn expr_return_none_or_err(
202 cx: &LateContext<'_>,
204 cond_expr: &Expr<'_>,
205 err_sym: Option<Symbol>,
207 match peel_blocks_with_stmt(expr).kind {
208 ExprKind::Ret(Some(ret_expr)) => expr_return_none_or_err(smbl, cx, ret_expr, cond_expr, err_sym),
209 ExprKind::Path(ref qpath) => match smbl {
210 sym::Option => is_res_lang_ctor(cx, cx.qpath_res(qpath, expr.hir_id), OptionNone),
211 sym::Result => path_to_local(expr).is_some() && path_to_local(expr) == path_to_local(cond_expr),
214 ExprKind::Call(call_expr, args_expr) => {
216 if smbl == sym::Result;
217 if let ExprKind::Path(QPath::Resolved(_, path)) = &call_expr.kind;
218 if let Some(segment) = path.segments.first();
219 if let Some(err_sym) = err_sym;
220 if let Some(arg) = args_expr.first();
221 if let ExprKind::Path(QPath::Resolved(_, arg_path)) = &arg.kind;
222 if let Some(PathSegment { ident, .. }) = arg_path.segments.first();
224 return segment.ident.name == sym::Err && err_sym == ident.name;
233 impl<'tcx> LateLintPass<'tcx> for QuestionMark {
234 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
235 if !in_constant(cx, expr.hir_id) {
236 check_is_none_or_err_and_early_return(cx, expr);
237 check_if_let_some_or_err_and_early_return(cx, expr);