1 use clippy_utils::diagnostics::span_lint;
2 use if_chain::if_chain;
3 use rustc_hir::{Expr, ExprKind, MutTy, Mutability, TyKind, UnOp};
4 use rustc_lint::LateContext;
7 use super::CAST_REF_TO_MUT;
9 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
11 if let ExprKind::Unary(UnOp::Deref, e) = &expr.kind;
12 if let ExprKind::Cast(e, t) = &e.kind;
13 if let TyKind::Ptr(MutTy { mutbl: Mutability::Mut, .. }) = t.kind;
14 if let ExprKind::Cast(e, t) = &e.kind;
15 if let TyKind::Ptr(MutTy { mutbl: Mutability::Not, .. }) = t.kind;
16 if let ty::Ref(..) = cx.typeck_results().node_type(e.hir_id).kind();
22 "casting `&T` to `&mut T` may cause undefined behavior, consider instead using an `UnsafeCell`",