1 use crate::utils::sugg::Sugg;
2 use if_chain::if_chain;
3 use rustc::hir::def::Def;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_tool_lint, lint_array};
9 use crate::utils::paths::*;
10 use crate::utils::{match_def_path, match_type, span_lint_and_then, SpanlessEq};
11 use rustc_errors::Applicability;
13 /// **What it does:** Checks for expressions that could be replaced by the question mark operator
15 /// **Why is this bad?** Question mark usage is more idiomatic
17 /// **Known problems:** None
21 /// if option.is_none() {
31 declare_clippy_lint! {
34 "checks for expressions that could be replaced by the question mark operator"
37 #[derive(Copy, Clone)]
40 impl LintPass for Pass {
41 fn get_lints(&self) -> LintArray {
42 lint_array!(QUESTION_MARK)
47 /// Check if the given expression on the given context matches the following structure:
50 /// if option.is_none() {
55 /// If it matches, it will suggest to use the question mark operator instead
56 fn check_is_none_and_early_return_none(cx: &LateContext<'_, '_>, expr: &Expr) {
58 if let ExprKind::If(if_expr, body, else_) = &expr.node;
59 if let ExprKind::MethodCall(segment, _, args) = &if_expr.node;
60 if segment.ident.name == "is_none";
61 if Self::expression_returns_none(cx, body);
62 if let Some(subject) = args.get(0);
63 if Self::is_option(cx, subject);
66 let receiver_str = &Sugg::hir(cx, subject, "..");
67 let mut replacement: Option<String> = None;
68 if let Some(else_) = else_ {
70 if let ExprKind::Block(block, None) = &else_.node;
71 if block.stmts.len() == 0;
72 if let Some(block_expr) = &block.expr;
73 if SpanlessEq::new(cx).ignore_fn().eq_expr(subject, block_expr);
75 replacement = Some(format!("Some({}?)", receiver_str));
78 } else if Self::moves_by_default(cx, subject) {
79 replacement = Some(format!("{}.as_ref()?;", receiver_str));
81 replacement = Some(format!("{}?;", receiver_str));
84 if let Some(replacement_str) = replacement {
89 "this block may be rewritten with the `?` operator",
91 db.span_suggestion_with_applicability(
95 Applicability::MaybeIncorrect, // snippet
104 fn moves_by_default(cx: &LateContext<'_, '_>, expression: &Expr) -> bool {
105 let expr_ty = cx.tables.expr_ty(expression);
107 !expr_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, expression.span)
110 fn is_option(cx: &LateContext<'_, '_>, expression: &Expr) -> bool {
111 let expr_ty = cx.tables.expr_ty(expression);
113 match_type(cx, expr_ty, &OPTION)
116 fn expression_returns_none(cx: &LateContext<'_, '_>, expression: &Expr) -> bool {
117 match expression.node {
118 ExprKind::Block(ref block, _) => {
119 if let Some(return_expression) = Self::return_expression(block) {
120 return Self::expression_returns_none(cx, &return_expression);
125 ExprKind::Ret(Some(ref expr)) => Self::expression_returns_none(cx, expr),
126 ExprKind::Path(ref qp) => {
127 if let Def::VariantCtor(def_id, _) = cx.tables.qpath_def(qp, expression.hir_id) {
128 return match_def_path(cx.tcx, def_id, &OPTION_NONE);
137 fn return_expression(block: &Block) -> Option<P<Expr>> {
138 // Check if last expression is a return statement. Then, return the expression
140 if block.stmts.len() == 1;
141 if let Some(expr) = block.stmts.iter().last();
142 if let StmtKind::Semi(ref expr) = expr.node;
143 if let ExprKind::Ret(ref ret_expr) = expr.node;
144 if let &Some(ref ret_expr) = ret_expr;
147 return Some(ret_expr.clone());
151 // Check for `return` without a semicolon.
153 if block.stmts.len() == 0;
154 if let Some(ExprKind::Ret(Some(ret_expr))) = block.expr.as_ref().map(|e| &e.node);
156 return Some(ret_expr.clone());
164 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
165 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
166 Self::check_is_none_and_early_return_none(cx, expr);