2021-03-25 19:29:11 +01:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
|
|
|
use clippy_utils::ty::walk_ptrs_ty_depth;
|
2022-10-06 09:44:38 +02:00
|
|
|
use clippy_utils::{get_parent_expr, is_trait_method};
|
2021-03-12 15:30:50 +01:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
2022-10-06 09:44:38 +02:00
|
|
|
use rustc_span::sym;
|
2021-03-12 15:30:50 +01:00
|
|
|
|
|
|
|
use super::USELESS_ASREF;
|
|
|
|
|
|
|
|
/// Checks for the `USELESS_ASREF` lint.
|
2021-04-08 17:50:13 +02:00
|
|
|
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, recvr: &hir::Expr<'_>) {
|
2021-03-12 15:30:50 +01:00
|
|
|
// when we get here, we've already checked that the call name is "as_ref" or "as_mut"
|
|
|
|
// check if the call is to the actual `AsRef` or `AsMut` trait
|
2022-10-06 09:44:38 +02:00
|
|
|
if is_trait_method(cx, expr, sym::AsRef) || is_trait_method(cx, expr, sym::AsMut) {
|
2021-03-12 15:30:50 +01:00
|
|
|
// check if the type after `as_ref` or `as_mut` is the same as before
|
|
|
|
let rcv_ty = cx.typeck_results().expr_ty(recvr);
|
|
|
|
let res_ty = cx.typeck_results().expr_ty(expr);
|
|
|
|
let (base_res_ty, res_depth) = walk_ptrs_ty_depth(res_ty);
|
|
|
|
let (base_rcv_ty, rcv_depth) = walk_ptrs_ty_depth(rcv_ty);
|
|
|
|
if base_rcv_ty == base_res_ty && rcv_depth >= res_depth {
|
|
|
|
// allow the `as_ref` or `as_mut` if it is followed by another method call
|
2023-11-16 19:13:24 +01:00
|
|
|
if let Some(parent) = get_parent_expr(cx, expr)
|
|
|
|
&& let hir::ExprKind::MethodCall(segment, ..) = parent.kind
|
|
|
|
&& segment.ident.span != expr.span
|
|
|
|
{
|
|
|
|
return;
|
2021-03-12 15:30:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
USELESS_ASREF,
|
|
|
|
expr.span,
|
2022-10-06 09:44:38 +02:00
|
|
|
&format!("this call to `{call_name}` does nothing"),
|
2023-07-17 10:19:29 +02:00
|
|
|
"try",
|
2021-03-12 15:30:50 +01:00
|
|
|
snippet_with_applicability(cx, recvr.span, "..", &mut applicability).to_string(),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|