2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-08-08 14:49:13 +00:00
|
|
|
use clippy_utils::higher;
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2022-07-18 07:39:37 +00:00
|
|
|
use clippy_utils::{
|
2022-10-06 07:44:38 +00:00
|
|
|
eq_expr_value, get_parent_node, in_constant, is_else_clause, is_res_lang_ctor, path_to_local, path_to_local_id,
|
|
|
|
peel_blocks, peel_blocks_with_stmt,
|
2022-07-18 07:39:37 +00:00
|
|
|
};
|
2018-11-27 20:14:15 +00:00
|
|
|
use if_chain::if_chain;
|
2019-01-31 01:15:29 +00:00
|
|
|
use rustc_errors::Applicability;
|
2022-10-06 07:44:38 +00:00
|
|
|
use rustc_hir::def::Res;
|
2022-07-18 07:39:37 +00:00
|
|
|
use rustc_hir::LangItem::{OptionNone, OptionSome, ResultErr, ResultOk};
|
2022-08-30 22:36:53 +00:00
|
|
|
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, Node, PatKind, PathSegment, QPath};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2022-07-18 07:39:37 +00:00
|
|
|
use rustc_middle::ty::Ty;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2022-07-18 07:39:37 +00:00
|
|
|
use rustc_span::{sym, symbol::Symbol};
|
2018-01-28 00:04:22 +00:00
|
|
|
|
2018-11-27 20:14:15 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for expressions that could be replaced by the question mark operator.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Question mark usage is more idiomatic.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 22:23:50 +00:00
|
|
|
/// ```ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// if option.is_none() {
|
|
|
|
/// return None;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written:
|
|
|
|
///
|
2019-03-05 22:23:50 +00:00
|
|
|
/// ```ignore
|
2019-03-05 16:50:33 +00:00
|
|
|
/// option?;
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2018-01-28 00:04:22 +00:00
|
|
|
pub QUESTION_MARK,
|
2018-03-28 13:24:26 +00:00
|
|
|
style,
|
2018-01-28 00:04:22 +00:00
|
|
|
"checks for expressions that could be replaced by the question mark operator"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(QuestionMark => [QUESTION_MARK]);
|
2018-01-28 00:04:22 +00:00
|
|
|
|
2022-07-18 07:39:37 +00:00
|
|
|
enum IfBlockType<'hir> {
|
|
|
|
/// An `if x.is_xxx() { a } else { b } ` expression.
|
2018-01-28 00:04:22 +00:00
|
|
|
///
|
2022-07-18 07:39:37 +00:00
|
|
|
/// Contains: caller (x), caller_type, call_sym (is_xxx), if_then (a), if_else (b)
|
|
|
|
IfIs(
|
|
|
|
&'hir Expr<'hir>,
|
|
|
|
Ty<'hir>,
|
|
|
|
Symbol,
|
|
|
|
&'hir Expr<'hir>,
|
|
|
|
Option<&'hir Expr<'hir>>,
|
|
|
|
),
|
|
|
|
/// An `if let Xxx(a) = b { c } else { d }` expression.
|
2021-10-21 11:11:36 +00:00
|
|
|
///
|
2022-07-18 07:39:37 +00:00
|
|
|
/// Contains: let_pat_qpath (Xxx), let_pat_type, let_pat_sym (a), let_expr (b), if_then (c),
|
|
|
|
/// if_else (d)
|
|
|
|
IfLet(
|
2022-10-06 07:44:38 +00:00
|
|
|
Res,
|
2022-07-18 07:39:37 +00:00
|
|
|
Ty<'hir>,
|
|
|
|
Symbol,
|
|
|
|
&'hir Expr<'hir>,
|
|
|
|
&'hir Expr<'hir>,
|
|
|
|
Option<&'hir Expr<'hir>>,
|
|
|
|
),
|
|
|
|
}
|
2018-12-19 19:55:01 +00:00
|
|
|
|
2022-07-18 07:39:37 +00:00
|
|
|
/// Checks if the given expression on the given context matches the following structure:
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// if option.is_none() {
|
|
|
|
/// return None;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// if result.is_err() {
|
|
|
|
/// return result;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// If it matches, it will suggest to use the question mark operator instead
|
|
|
|
fn check_is_none_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
|
|
|
|
if_chain! {
|
|
|
|
if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr);
|
|
|
|
if !is_else_clause(cx.tcx, expr);
|
2022-09-01 09:43:35 +00:00
|
|
|
if let ExprKind::MethodCall(segment, caller, ..) = &cond.kind;
|
2022-07-18 07:39:37 +00:00
|
|
|
let caller_ty = cx.typeck_results().expr_ty(caller);
|
|
|
|
let if_block = IfBlockType::IfIs(caller, caller_ty, segment.ident.name, then, r#else);
|
|
|
|
if is_early_return(sym::Option, cx, &if_block) || is_early_return(sym::Result, cx, &if_block);
|
|
|
|
then {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let receiver_str = snippet_with_applicability(cx, caller.span, "..", &mut applicability);
|
2022-10-27 14:32:17 +00:00
|
|
|
let by_ref = !caller_ty.is_copy_modulo_regions(cx.tcx, cx.param_env) &&
|
2022-07-18 07:39:37 +00:00
|
|
|
!matches!(caller.kind, ExprKind::Call(..) | ExprKind::MethodCall(..));
|
|
|
|
let sugg = if let Some(else_inner) = r#else {
|
|
|
|
if eq_expr_value(cx, caller, peel_blocks(else_inner)) {
|
2022-10-06 07:44:38 +00:00
|
|
|
format!("Some({receiver_str}?)")
|
2022-07-18 07:39:37 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2020-06-09 14:36:01 +00:00
|
|
|
}
|
2022-07-18 07:39:37 +00:00
|
|
|
} else {
|
2022-10-06 07:44:38 +00:00
|
|
|
format!("{receiver_str}{}?;", if by_ref { ".as_ref()" } else { "" })
|
2022-07-18 07:39:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
QUESTION_MARK,
|
|
|
|
expr.span,
|
|
|
|
"this block may be rewritten with the `?` operator",
|
|
|
|
"replace it with",
|
|
|
|
sugg,
|
|
|
|
applicability,
|
|
|
|
);
|
2018-01-28 00:04:22 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-18 07:39:37 +00:00
|
|
|
}
|
2018-01-28 00:04:22 +00:00
|
|
|
|
2022-07-18 07:39:37 +00:00
|
|
|
fn check_if_let_some_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
|
|
|
|
if_chain! {
|
|
|
|
if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else }) = higher::IfLet::hir(cx, expr);
|
|
|
|
if !is_else_clause(cx.tcx, expr);
|
2022-09-01 03:29:57 +00:00
|
|
|
if let PatKind::TupleStruct(ref path1, [field], ddpos) = let_pat.kind;
|
|
|
|
if ddpos.as_opt_usize().is_none();
|
2022-08-30 22:36:53 +00:00
|
|
|
if let PatKind::Binding(BindingAnnotation(by_ref, _), bind_id, ident, None) = field.kind;
|
2022-07-18 07:39:37 +00:00
|
|
|
let caller_ty = cx.typeck_results().expr_ty(let_expr);
|
2022-10-06 07:44:38 +00:00
|
|
|
let if_block = IfBlockType::IfLet(
|
|
|
|
cx.qpath_res(path1, let_pat.hir_id),
|
|
|
|
caller_ty,
|
|
|
|
ident.name,
|
|
|
|
let_expr,
|
|
|
|
if_then,
|
|
|
|
if_else
|
|
|
|
);
|
2022-07-18 07:39:37 +00:00
|
|
|
if (is_early_return(sym::Option, cx, &if_block) && path_to_local_id(peel_blocks(if_then), bind_id))
|
|
|
|
|| is_early_return(sym::Result, cx, &if_block);
|
|
|
|
if if_else.map(|e| eq_expr_value(cx, let_expr, peel_blocks(e))).filter(|e| *e).is_none();
|
|
|
|
then {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
|
|
|
|
let requires_semi = matches!(get_parent_node(cx.tcx, expr.hir_id), Some(Node::Stmt(_)));
|
|
|
|
let sugg = format!(
|
2022-10-06 07:44:38 +00:00
|
|
|
"{receiver_str}{}?{}",
|
2022-08-30 22:36:53 +00:00
|
|
|
if by_ref == ByRef::Yes { ".as_ref()" } else { "" },
|
2022-07-18 07:39:37 +00:00
|
|
|
if requires_semi { ";" } else { "" }
|
|
|
|
);
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
QUESTION_MARK,
|
|
|
|
expr.span,
|
|
|
|
"this block may be rewritten with the `?` operator",
|
|
|
|
"replace it with",
|
|
|
|
sugg,
|
|
|
|
applicability,
|
|
|
|
);
|
2020-03-04 07:59:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-18 07:39:37 +00:00
|
|
|
}
|
2020-03-04 07:59:16 +00:00
|
|
|
|
2022-07-18 07:39:37 +00:00
|
|
|
fn is_early_return(smbl: Symbol, cx: &LateContext<'_>, if_block: &IfBlockType<'_>) -> bool {
|
|
|
|
match *if_block {
|
|
|
|
IfBlockType::IfIs(caller, caller_ty, call_sym, if_then, _) => {
|
|
|
|
// If the block could be identified as `if x.is_none()/is_err()`,
|
|
|
|
// we then only need to check the if_then return to see if it is none/err.
|
|
|
|
is_type_diagnostic_item(cx, caller_ty, smbl)
|
|
|
|
&& expr_return_none_or_err(smbl, cx, if_then, caller, None)
|
|
|
|
&& match smbl {
|
|
|
|
sym::Option => call_sym == sym!(is_none),
|
|
|
|
sym::Result => call_sym == sym!(is_err),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
},
|
2022-10-06 07:44:38 +00:00
|
|
|
IfBlockType::IfLet(res, let_expr_ty, let_pat_sym, let_expr, if_then, if_else) => {
|
2022-07-18 07:39:37 +00:00
|
|
|
is_type_diagnostic_item(cx, let_expr_ty, smbl)
|
|
|
|
&& match smbl {
|
|
|
|
sym::Option => {
|
|
|
|
// We only need to check `if let Some(x) = option` not `if let None = option`,
|
|
|
|
// because the later one will be suggested as `if option.is_none()` thus causing conflict.
|
2022-10-06 07:44:38 +00:00
|
|
|
is_res_lang_ctor(cx, res, OptionSome)
|
2022-07-18 07:39:37 +00:00
|
|
|
&& if_else.is_some()
|
|
|
|
&& expr_return_none_or_err(smbl, cx, if_else.unwrap(), let_expr, None)
|
|
|
|
},
|
|
|
|
sym::Result => {
|
2022-10-06 07:44:38 +00:00
|
|
|
(is_res_lang_ctor(cx, res, ResultOk)
|
2022-07-18 07:39:37 +00:00
|
|
|
&& if_else.is_some()
|
|
|
|
&& expr_return_none_or_err(smbl, cx, if_else.unwrap(), let_expr, Some(let_pat_sym)))
|
2022-10-06 07:44:38 +00:00
|
|
|
|| is_res_lang_ctor(cx, res, ResultErr)
|
2022-07-18 07:39:37 +00:00
|
|
|
&& expr_return_none_or_err(smbl, cx, if_then, let_expr, Some(let_pat_sym))
|
2022-10-26 04:16:28 +00:00
|
|
|
&& if_else.is_none()
|
2022-07-18 07:39:37 +00:00
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
},
|
2018-01-28 00:04:22 +00:00
|
|
|
}
|
2022-07-18 07:39:37 +00:00
|
|
|
}
|
2018-01-28 00:04:22 +00:00
|
|
|
|
2022-07-18 07:39:37 +00:00
|
|
|
fn expr_return_none_or_err(
|
|
|
|
smbl: Symbol,
|
|
|
|
cx: &LateContext<'_>,
|
|
|
|
expr: &Expr<'_>,
|
|
|
|
cond_expr: &Expr<'_>,
|
|
|
|
err_sym: Option<Symbol>,
|
|
|
|
) -> bool {
|
|
|
|
match peel_blocks_with_stmt(expr).kind {
|
|
|
|
ExprKind::Ret(Some(ret_expr)) => expr_return_none_or_err(smbl, cx, ret_expr, cond_expr, err_sym),
|
|
|
|
ExprKind::Path(ref qpath) => match smbl {
|
2022-10-06 07:44:38 +00:00
|
|
|
sym::Option => is_res_lang_ctor(cx, cx.qpath_res(qpath, expr.hir_id), OptionNone),
|
2022-07-18 07:39:37 +00:00
|
|
|
sym::Result => path_to_local(expr).is_some() && path_to_local(expr) == path_to_local(cond_expr),
|
2021-10-21 11:11:36 +00:00
|
|
|
_ => false,
|
2022-07-18 07:39:37 +00:00
|
|
|
},
|
|
|
|
ExprKind::Call(call_expr, args_expr) => {
|
|
|
|
if_chain! {
|
|
|
|
if smbl == sym::Result;
|
|
|
|
if let ExprKind::Path(QPath::Resolved(_, path)) = &call_expr.kind;
|
|
|
|
if let Some(segment) = path.segments.first();
|
|
|
|
if let Some(err_sym) = err_sym;
|
|
|
|
if let Some(arg) = args_expr.first();
|
|
|
|
if let ExprKind::Path(QPath::Resolved(_, arg_path)) = &arg.kind;
|
|
|
|
if let Some(PathSegment { ident, .. }) = arg_path.segments.first();
|
|
|
|
then {
|
|
|
|
return segment.ident.name == sym::Err && err_sym == ident.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
},
|
|
|
|
_ => false,
|
2021-10-21 11:11:36 +00:00
|
|
|
}
|
2018-01-28 00:04:22 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for QuestionMark {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2022-10-06 07:44:38 +00:00
|
|
|
if !in_constant(cx, expr.hir_id) {
|
|
|
|
check_is_none_or_err_and_early_return(cx, expr);
|
|
|
|
check_if_let_some_or_err_and_early_return(cx, expr);
|
|
|
|
}
|
2018-01-28 00:04:22 +00:00
|
|
|
}
|
|
|
|
}
|