1 use if_chain::if_chain;
2 use rustc::hir::def::{DefKind, Res};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_lint_pass, declare_tool_lint};
6 use rustc_errors::Applicability;
9 use crate::utils::paths::*;
10 use crate::utils::sugg::Sugg;
11 use crate::utils::{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 ExprKind::If(if_expr, body, else_) = &expr.node;
52 if let ExprKind::MethodCall(segment, _, args) = &if_expr.node;
53 if segment.ident.name == "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_.node;
64 if block.stmts.len() == 0;
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.node {
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 cx.match_def_path(def_id, &OPTION_NONE);
132 fn return_expression(block: &Block) -> Option<P<Expr>> {
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.node;
138 if let ExprKind::Ret(ref ret_expr) = expr.node;
139 if let &Some(ref ret_expr) = ret_expr;
142 return Some(ret_expr.clone());
146 // Check for `return` without a semicolon.
148 if block.stmts.len() == 0;
149 if let Some(ExprKind::Ret(Some(ret_expr))) = block.expr.as_ref().map(|e| &e.node);
151 return Some(ret_expr.clone());
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);