1 use if_chain::if_chain;
2 use rustc::declare_lint_pass;
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc_errors::Applicability;
5 use rustc_hir::def::{DefKind, Res};
7 use rustc_session::declare_tool_lint;
9 use crate::utils::paths::*;
10 use crate::utils::sugg::Sugg;
11 use crate::utils::{higher, match_def_path, match_type, span_lint_and_then, SpanlessEq};
13 declare_clippy_lint! {
14 /// **What it does:** Checks for expressions that could be replaced by the question mark operator.
16 /// **Why is this bad?** Question mark usage is more idiomatic.
18 /// **Known problems:** None
22 /// if option.is_none() {
34 "checks for expressions that could be replaced by the question mark operator"
37 declare_lint_pass!(QuestionMark => [QUESTION_MARK]);
40 /// Checks if the given expression on the given context matches the following structure:
43 /// if option.is_none() {
48 /// If it matches, it will suggest to use the question mark operator instead
49 fn check_is_none_and_early_return_none(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
51 if let Some((if_expr, body, else_)) = higher::if_block(&expr);
52 if let ExprKind::MethodCall(segment, _, args) = &if_expr.kind;
53 if segment.ident.name == sym!(is_none);
54 if Self::expression_returns_none(cx, body);
55 if let Some(subject) = args.get(0);
56 if Self::is_option(cx, subject);
59 let receiver_str = &Sugg::hir(cx, subject, "..");
60 let mut replacement: Option<String> = None;
61 if let Some(else_) = else_ {
63 if let ExprKind::Block(block, None) = &else_.kind;
64 if block.stmts.is_empty();
65 if let Some(block_expr) = &block.expr;
66 if SpanlessEq::new(cx).ignore_fn().eq_expr(subject, block_expr);
68 replacement = Some(format!("Some({}?)", receiver_str));
71 } else if Self::moves_by_default(cx, subject) {
72 replacement = Some(format!("{}.as_ref()?;", receiver_str));
74 replacement = Some(format!("{}?;", receiver_str));
77 if let Some(replacement_str) = replacement {
82 "this block may be rewritten with the `?` operator",
88 Applicability::MaybeIncorrect, // snippet
97 fn moves_by_default(cx: &LateContext<'_, '_>, expression: &Expr<'_>) -> bool {
98 let expr_ty = cx.tables.expr_ty(expression);
100 !expr_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, expression.span)
103 fn is_option(cx: &LateContext<'_, '_>, expression: &Expr<'_>) -> bool {
104 let expr_ty = cx.tables.expr_ty(expression);
106 match_type(cx, expr_ty, &OPTION)
109 fn expression_returns_none(cx: &LateContext<'_, '_>, expression: &Expr<'_>) -> bool {
110 match expression.kind {
111 ExprKind::Block(ref block, _) => {
112 if let Some(return_expression) = Self::return_expression(block) {
113 return Self::expression_returns_none(cx, &return_expression);
118 ExprKind::Ret(Some(ref expr)) => Self::expression_returns_none(cx, expr),
119 ExprKind::Path(ref qp) => {
120 if let Res::Def(DefKind::Ctor(def::CtorOf::Variant, def::CtorKind::Const), def_id) =
121 cx.tables.qpath_res(qp, expression.hir_id)
123 return match_def_path(cx, def_id, &OPTION_NONE);
132 fn return_expression<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
133 // Check if last expression is a return statement. Then, return the expression
135 if block.stmts.len() == 1;
136 if let Some(expr) = block.stmts.iter().last();
137 if let StmtKind::Semi(ref expr) = expr.kind;
138 if let ExprKind::Ret(ret_expr) = expr.kind;
139 if let Some(ret_expr) = ret_expr;
142 return Some(ret_expr);
146 // Check for `return` without a semicolon.
148 if block.stmts.is_empty();
149 if let Some(ExprKind::Ret(Some(ret_expr))) = block.expr.as_ref().map(|e| &e.kind);
151 return Some(ret_expr);
159 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for QuestionMark {
160 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
161 Self::check_is_none_and_early_return_none(cx, expr);