1 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use crate::rustc::{declare_tool_lint, lint_array};
3 use if_chain::if_chain;
4 use crate::rustc::hir::*;
5 use crate::rustc::hir::def::Def;
6 use crate::utils::sugg::Sugg;
7 use crate::syntax::ptr::P;
9 use crate::utils::{match_def_path, match_type, span_lint_and_then};
10 use crate::utils::paths::*;
11 use crate::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() {
34 "checks for expressions that could be replaced by the question mark operator"
37 #[derive(Copy, Clone)]
38 pub struct QuestionMarkPass;
40 impl LintPass for QuestionMarkPass {
41 fn get_lints(&self) -> LintArray {
42 lint_array!(QUESTION_MARK)
46 impl QuestionMarkPass {
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(ref if_expr, ref body, _) = expr.node;
59 if let ExprKind::MethodCall(ref segment, _, ref 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);
70 "this block may be rewritten with the `?` operator",
72 let receiver_str = &Sugg::hir(cx, subject, "..");
74 db.span_suggestion_with_applicability(
77 format!("{}?;", receiver_str),
78 Applicability::Unspecified,
86 fn is_option(cx: &LateContext<'_, '_>, expression: &Expr) -> bool {
87 let expr_ty = cx.tables.expr_ty(expression);
89 match_type(cx, expr_ty, &OPTION)
92 fn expression_returns_none(cx: &LateContext<'_, '_>, expression: &Expr) -> bool {
93 match expression.node {
94 ExprKind::Block(ref block, _) => {
95 if let Some(return_expression) = Self::return_expression(block) {
96 return Self::expression_returns_none(cx, &return_expression);
101 ExprKind::Ret(Some(ref expr)) => {
102 Self::expression_returns_none(cx, expr)
104 ExprKind::Path(ref qp) => {
105 if let Def::VariantCtor(def_id, _) = cx.tables.qpath_def(qp, expression.hir_id) {
106 return match_def_path(cx.tcx, def_id, &OPTION_NONE);
115 fn return_expression(block: &Block) -> Option<P<Expr>> {
116 // Check if last expression is a return statement. Then, return the expression
118 if block.stmts.len() == 1;
119 if let Some(expr) = block.stmts.iter().last();
120 if let StmtKind::Semi(ref expr, _) = expr.node;
121 if let ExprKind::Ret(ref ret_expr) = expr.node;
122 if let &Some(ref ret_expr) = ret_expr;
125 return Some(ret_expr.clone());
129 // Check if the block has an implicit return expression
130 if let Some(ref ret_expr) = block.expr {
131 return Some(ret_expr.clone());
138 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for QuestionMarkPass {
139 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
140 Self::check_is_none_and_early_return_none(cx, expr);