1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::snippet_opt;
3 use rustc_errors::Applicability;
4 use rustc_hir::{Expr, ExprKind};
5 use rustc_lint::LateContext;
8 ty::{self, Ty, TypeAndMut},
11 use super::AS_PTR_CAST_MUT;
13 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_to: Ty<'_>) {
14 if let ty::RawPtr(ptrty @ TypeAndMut { mutbl: Mutability::Mut, .. }) = cast_to.kind()
15 && let ty::RawPtr(TypeAndMut { mutbl: Mutability::Not, .. }) =
16 cx.typeck_results().node_type(cast_expr.hir_id).kind()
17 && let ExprKind::MethodCall(method_name, receiver, [], _) = cast_expr.peel_blocks().kind
18 && method_name.ident.name == rustc_span::sym::as_ptr
19 && let Some(as_ptr_did) = cx.typeck_results().type_dependent_def_id(cast_expr.peel_blocks().hir_id)
20 && let as_ptr_sig = cx.tcx.fn_sig(as_ptr_did)
21 && let Some(first_param_ty) = as_ptr_sig.skip_binder().inputs().iter().next()
22 && let ty::Ref(_, _, Mutability::Not) = first_param_ty.kind()
23 && let Some(recv) = snippet_opt(cx, receiver.span)
25 // `as_mut_ptr` might not exist
26 let applicability = Applicability::MaybeIncorrect;
32 &format!("casting the result of `as_ptr` to *{ptrty}"),
34 format!("{recv}.as_mut_ptr()"),