2022-04-08 19:54:44 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2022-04-08 18:07:19 +00:00
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2022-04-08 19:54:44 +00:00
|
|
|
use rustc_errors::Applicability;
|
2022-04-08 18:07:19 +00:00
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
2022-04-08 19:27:01 +00:00
|
|
|
use rustc_span::sym;
|
2022-04-08 18:07:19 +00:00
|
|
|
|
|
|
|
use super::NEEDLESS_OPTION_TAKE;
|
|
|
|
|
2022-04-08 19:54:44 +00:00
|
|
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
|
2022-04-08 19:27:01 +00:00
|
|
|
// Checks if expression type is equal to sym::Option and if the expr is not a syntactic place
|
2022-04-10 08:59:43 +00:00
|
|
|
if is_expr_option(cx, recv) && !recv.is_syntactic_place_expr() {
|
2022-04-08 19:54:44 +00:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
NEEDLESS_OPTION_TAKE,
|
|
|
|
expr.span,
|
|
|
|
"Called `Option::take()` on a temporary value",
|
|
|
|
"try",
|
|
|
|
format!(
|
|
|
|
"{}",
|
|
|
|
snippet_with_applicability(cx, recv.span, "..", &mut applicability)
|
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
2022-04-08 19:27:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_expr_option(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
|
|
|
let expr_type = cx.typeck_results().expr_ty(expr);
|
|
|
|
is_type_diagnostic_item(cx, expr_type, sym::Option)
|
2022-04-08 18:07:19 +00:00
|
|
|
}
|