2 use rustc::{declare_lint, lint_array};
3 use if_chain::if_chain;
5 use rustc::hir::def::Def;
6 use crate::utils::sugg::Sugg;
9 use crate::utils::{match_def_path, match_type, span_lint_and_then};
10 use crate::utils::paths::*;
12 /// **What it does:** Checks for expressions that could be replaced by the question mark operator
14 /// **Why is this bad?** Question mark usage is more idiomatic
16 /// **Known problems:** None
20 /// if option.is_none() {
33 "checks for expressions that could be replaced by the question mark operator"
36 #[derive(Copy, Clone)]
37 pub struct QuestionMarkPass;
39 impl LintPass for QuestionMarkPass {
40 fn get_lints(&self) -> LintArray {
41 lint_array!(QUESTION_MARK)
45 impl QuestionMarkPass {
46 /// Check if the given expression on the given context matches the following structure:
49 /// if option.is_none() {
54 /// If it matches, it will suggest to use the question mark operator instead
55 fn check_is_none_and_early_return_none(cx: &LateContext, expr: &Expr) {
57 if let ExprKind::If(ref if_expr, ref body, _) = expr.node;
58 if let ExprKind::MethodCall(ref segment, _, ref args) = if_expr.node;
59 if segment.ident.name == "is_none";
60 if Self::expression_returns_none(cx, body);
61 if let Some(subject) = args.get(0);
62 if Self::is_option(cx, subject);
69 "this block may be rewritten with the `?` operator",
71 let receiver_str = &Sugg::hir(cx, subject, "..");
76 format!("{}?;", receiver_str),
84 fn is_option(cx: &LateContext, expression: &Expr) -> bool {
85 let expr_ty = cx.tables.expr_ty(expression);
87 match_type(cx, expr_ty, &OPTION)
90 fn expression_returns_none(cx: &LateContext, expression: &Expr) -> bool {
91 match expression.node {
92 ExprKind::Block(ref block, _) => {
93 if let Some(return_expression) = Self::return_expression(block) {
94 return Self::expression_returns_none(cx, &return_expression);
99 ExprKind::Ret(Some(ref expr)) => {
100 Self::expression_returns_none(cx, expr)
102 ExprKind::Path(ref qp) => {
103 if let Def::VariantCtor(def_id, _) = cx.tables.qpath_def(qp, expression.hir_id) {
104 return match_def_path(cx.tcx, def_id, &OPTION_NONE);
113 fn return_expression(block: &Block) -> Option<P<Expr>> {
114 // Check if last expression is a return statement. Then, return the expression
116 if block.stmts.len() == 1;
117 if let Some(expr) = block.stmts.iter().last();
118 if let StmtKind::Semi(ref expr, _) = expr.node;
119 if let ExprKind::Ret(ref ret_expr) = expr.node;
120 if let &Some(ref ret_expr) = ret_expr;
123 return Some(ret_expr.clone());
127 // Check if the block has an implicit return expression
128 if let Some(ref ret_expr) = block.expr {
129 return Some(ret_expr.clone());
136 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for QuestionMarkPass {
137 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
138 Self::check_is_none_and_early_return_none(cx, expr);