2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-03-14 23:17:44 +00:00
|
|
|
use clippy_utils::source::snippet;
|
2021-03-13 23:01:03 +00:00
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2021-04-26 20:49:25 +00:00
|
|
|
use clippy_utils::{differing_macro_contexts, is_lang_ctor};
|
2021-03-16 16:06:34 +00:00
|
|
|
use if_chain::if_chain;
|
2020-12-26 17:02:46 +00:00
|
|
|
use rustc_errors::Applicability;
|
2021-04-02 22:09:58 +00:00
|
|
|
use rustc_hir::LangItem::{OptionSome, ResultOk};
|
2020-12-26 17:02:46 +00:00
|
|
|
use rustc_hir::{Body, Expr, ExprKind, LangItem, MatchSource, QPath};
|
2021-04-26 20:49:25 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2020-12-26 17:02:46 +00:00
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:**
|
|
|
|
/// Suggests alternatives for useless applications of `?` in terminating expressions
|
|
|
|
///
|
2021-01-07 23:45:15 +00:00
|
|
|
/// **Why is this bad?** There's no reason to use `?` to short-circuit when execution of the body will end there anyway.
|
2020-12-26 17:02:46 +00:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct TO {
|
|
|
|
/// magic: Option<usize>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn f(to: TO) -> Option<usize> {
|
|
|
|
/// Some(to.magic?)
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// struct TR {
|
|
|
|
/// magic: Result<usize, bool>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn g(tr: Result<TR, bool>) -> Result<usize, bool> {
|
|
|
|
/// tr.and_then(|t| Ok(t.magic?))
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// struct TO {
|
|
|
|
/// magic: Option<usize>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn f(to: TO) -> Option<usize> {
|
|
|
|
/// to.magic
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// struct TR {
|
|
|
|
/// magic: Result<usize, bool>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn g(tr: Result<TR, bool>) -> Result<usize, bool> {
|
|
|
|
/// tr.and_then(|t| t.magic)
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub NEEDLESS_QUESTION_MARK,
|
|
|
|
complexity,
|
2021-01-07 23:45:15 +00:00
|
|
|
"Suggest `value.inner_option` instead of `Some(value.inner_option?)`. The same goes for `Result<T, E>`."
|
2020-12-26 17:02:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 20:49:25 +00:00
|
|
|
declare_lint_pass!(NeedlessQuestionMark => [NEEDLESS_QUESTION_MARK]);
|
2020-12-26 17:02:46 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum SomeOkCall<'a> {
|
|
|
|
SomeCall(&'a Expr<'a>, &'a Expr<'a>),
|
|
|
|
OkCall(&'a Expr<'a>, &'a Expr<'a>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for NeedlessQuestionMark {
|
|
|
|
/*
|
|
|
|
* The question mark operator is compatible with both Result<T, E> and Option<T>,
|
|
|
|
* from Rust 1.13 and 1.22 respectively.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* What do we match:
|
|
|
|
* Expressions that look like this:
|
|
|
|
* Some(option?), Ok(result?)
|
|
|
|
*
|
|
|
|
* Where do we match:
|
|
|
|
* Last expression of a body
|
|
|
|
* Return statement
|
|
|
|
* A body's value (single line closure)
|
|
|
|
*
|
|
|
|
* What do we not match:
|
|
|
|
* Implicit calls to `from(..)` on the error value
|
|
|
|
*/
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ Expr<'_>) {
|
|
|
|
let e = match &expr.kind {
|
|
|
|
ExprKind::Ret(Some(e)) => e,
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
|
2021-04-26 20:49:25 +00:00
|
|
|
if let Some(ok_some_call) = is_some_or_ok_call(cx, e) {
|
2020-12-26 17:02:46 +00:00
|
|
|
emit_lint(cx, &ok_some_call);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
|
|
|
|
// Function / Closure block
|
|
|
|
let expr_opt = if let ExprKind::Block(block, _) = &body.value.kind {
|
|
|
|
block.expr
|
|
|
|
} else {
|
|
|
|
// Single line closure
|
|
|
|
Some(&body.value)
|
|
|
|
};
|
|
|
|
|
|
|
|
if_chain! {
|
|
|
|
if let Some(expr) = expr_opt;
|
2021-04-26 20:49:25 +00:00
|
|
|
if let Some(ok_some_call) = is_some_or_ok_call(cx, expr);
|
2020-12-26 17:02:46 +00:00
|
|
|
then {
|
|
|
|
emit_lint(cx, &ok_some_call);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn emit_lint(cx: &LateContext<'_>, expr: &SomeOkCall<'_>) {
|
|
|
|
let (entire_expr, inner_expr) = match expr {
|
|
|
|
SomeOkCall::OkCall(outer, inner) | SomeOkCall::SomeCall(outer, inner) => (outer, inner),
|
|
|
|
};
|
|
|
|
|
2021-03-16 00:55:45 +00:00
|
|
|
span_lint_and_sugg(
|
2020-12-26 17:02:46 +00:00
|
|
|
cx,
|
|
|
|
NEEDLESS_QUESTION_MARK,
|
|
|
|
entire_expr.span,
|
2021-02-24 13:02:51 +00:00
|
|
|
"question mark operator is useless here",
|
2020-12-26 17:02:46 +00:00
|
|
|
"try",
|
2021-03-14 23:17:44 +00:00
|
|
|
format!("{}", snippet(cx, inner_expr.span, r#""...""#)),
|
2020-12-26 17:02:46 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-26 20:49:25 +00:00
|
|
|
fn is_some_or_ok_call<'a>(cx: &'a LateContext<'_>, expr: &'a Expr<'_>) -> Option<SomeOkCall<'a>> {
|
2020-12-26 17:02:46 +00:00
|
|
|
if_chain! {
|
|
|
|
// Check outer expression matches CALL_IDENT(ARGUMENT) format
|
|
|
|
if let ExprKind::Call(path, args) = &expr.kind;
|
2021-04-02 22:09:58 +00:00
|
|
|
if let ExprKind::Path(ref qpath) = &path.kind;
|
|
|
|
if is_lang_ctor(cx, qpath, OptionSome) || is_lang_ctor(cx, qpath, ResultOk);
|
2020-12-26 17:02:46 +00:00
|
|
|
|
|
|
|
// Extract inner expression from ARGUMENT
|
|
|
|
if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar) = &args[0].kind;
|
|
|
|
if let ExprKind::Call(called, args) = &inner_expr_with_q.kind;
|
|
|
|
if args.len() == 1;
|
|
|
|
|
|
|
|
if let ExprKind::Path(QPath::LangItem(LangItem::TryIntoResult, _)) = &called.kind;
|
|
|
|
then {
|
|
|
|
// Extract inner expr type from match argument generated by
|
|
|
|
// question mark operator
|
|
|
|
let inner_expr = &args[0];
|
|
|
|
|
2021-03-18 22:01:42 +00:00
|
|
|
// if the inner expr is inside macro but the outer one is not, do not lint (#6921)
|
|
|
|
if differing_macro_contexts(expr.span, inner_expr.span) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-12-26 17:02:46 +00:00
|
|
|
let inner_ty = cx.typeck_results().expr_ty(inner_expr);
|
|
|
|
let outer_ty = cx.typeck_results().expr_ty(expr);
|
|
|
|
|
|
|
|
// Check if outer and inner type are Option
|
2021-03-13 23:01:03 +00:00
|
|
|
let outer_is_some = is_type_diagnostic_item(cx, outer_ty, sym::option_type);
|
|
|
|
let inner_is_some = is_type_diagnostic_item(cx, inner_ty, sym::option_type);
|
2020-12-26 17:02:46 +00:00
|
|
|
|
|
|
|
// Check for Option MSRV
|
2021-04-26 20:49:25 +00:00
|
|
|
if outer_is_some && inner_is_some {
|
2020-12-26 17:02:46 +00:00
|
|
|
return Some(SomeOkCall::SomeCall(expr, inner_expr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if outer and inner type are Result
|
2021-03-13 23:01:03 +00:00
|
|
|
let outer_is_result = is_type_diagnostic_item(cx, outer_ty, sym::result_type);
|
|
|
|
let inner_is_result = is_type_diagnostic_item(cx, inner_ty, sym::result_type);
|
2020-12-26 17:02:46 +00:00
|
|
|
|
|
|
|
// Additional check: if the error type of the Result can be converted
|
|
|
|
// via the From trait, then don't match
|
|
|
|
let does_not_call_from = !has_implicit_error_from(cx, expr, inner_expr);
|
|
|
|
|
|
|
|
// Must meet Result MSRV
|
2021-04-26 20:49:25 +00:00
|
|
|
if outer_is_result && inner_is_result && does_not_call_from {
|
2020-12-26 17:02:46 +00:00
|
|
|
return Some(SomeOkCall::OkCall(expr, inner_expr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_implicit_error_from(cx: &LateContext<'_>, entire_expr: &Expr<'_>, inner_result_expr: &Expr<'_>) -> bool {
|
|
|
|
return cx.typeck_results().expr_ty(entire_expr) != cx.typeck_results().expr_ty(inner_result_expr);
|
|
|
|
}
|