| 
 | 1 | +use rustc_ast::Mutability;  | 
 | 2 | +use rustc_hir::{Expr, ExprKind, MutTy, TyKind, UnOp};  | 
 | 3 | +use rustc_middle::ty;  | 
 | 4 | +use rustc_span::sym;  | 
 | 5 | + | 
 | 6 | +use crate::{lints::CastRefToMutDiag, LateContext, LateLintPass, LintContext};  | 
 | 7 | + | 
 | 8 | +declare_lint! {  | 
 | 9 | +    /// The `cast_ref_to_mut` lint checks for casts of `&T` to `&mut T`  | 
 | 10 | +    /// without using interior mutability.  | 
 | 11 | +    ///  | 
 | 12 | +    /// ### Example  | 
 | 13 | +    ///  | 
 | 14 | +    /// ```rust,compile_fail  | 
 | 15 | +    /// fn x(r: &i32) {  | 
 | 16 | +    ///     unsafe {  | 
 | 17 | +    ///         *(r as *const i32 as *mut i32) += 1;  | 
 | 18 | +    ///     }  | 
 | 19 | +    /// }  | 
 | 20 | +    /// ```  | 
 | 21 | +    ///  | 
 | 22 | +    /// {{produces}}  | 
 | 23 | +    ///  | 
 | 24 | +    /// ### Explanation  | 
 | 25 | +    ///  | 
 | 26 | +    /// Casting `&T` to `&mut T` without using interior mutability is undefined behavior,  | 
 | 27 | +    /// as it's a violation of Rust reference aliasing requirements.  | 
 | 28 | +    ///  | 
 | 29 | +    /// `UnsafeCell` is the only way to obtain aliasable data that is considered  | 
 | 30 | +    /// mutable.  | 
 | 31 | +    CAST_REF_TO_MUT,  | 
 | 32 | +    Deny,  | 
 | 33 | +    "casts of `&T` to `&mut T` without interior mutability"  | 
 | 34 | +}  | 
 | 35 | + | 
 | 36 | +declare_lint_pass!(CastRefToMut => [CAST_REF_TO_MUT]);  | 
 | 37 | + | 
 | 38 | +impl<'tcx> LateLintPass<'tcx> for CastRefToMut {  | 
 | 39 | +    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {  | 
 | 40 | +        let ExprKind::Unary(UnOp::Deref, e) = &expr.kind else { return; };  | 
 | 41 | + | 
 | 42 | +        let e = e.peel_blocks();  | 
 | 43 | +        let e = if let ExprKind::Cast(e, t) = e.kind  | 
 | 44 | +            && let TyKind::Ptr(MutTy { mutbl: Mutability::Mut, .. }) = t.kind {  | 
 | 45 | +            e  | 
 | 46 | +        } else if let ExprKind::MethodCall(_, expr, [], _) = e.kind  | 
 | 47 | +            && let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)  | 
 | 48 | +            && cx.tcx.is_diagnostic_item(sym::ptr_cast_mut, def_id) {  | 
 | 49 | +            expr  | 
 | 50 | +        } else {  | 
 | 51 | +            return;  | 
 | 52 | +        };  | 
 | 53 | + | 
 | 54 | +        let e = e.peel_blocks();  | 
 | 55 | +        let e = if let ExprKind::Cast(e, t) = e.kind  | 
 | 56 | +            && let TyKind::Ptr(MutTy { mutbl: Mutability::Not, .. }) = t.kind {  | 
 | 57 | +            e  | 
 | 58 | +        } else if let ExprKind::Call(path, [arg]) = e.kind  | 
 | 59 | +            && let ExprKind::Path(ref qpath) = path.kind  | 
 | 60 | +            && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()  | 
 | 61 | +            && cx.tcx.is_diagnostic_item(sym::ptr_from_ref, def_id) {  | 
 | 62 | +            arg  | 
 | 63 | +        } else {  | 
 | 64 | +            return;  | 
 | 65 | +        };  | 
 | 66 | + | 
 | 67 | +        let e = e.peel_blocks();  | 
 | 68 | +        if let ty::Ref(..) = cx.typeck_results().node_type(e.hir_id).kind() {  | 
 | 69 | +            cx.emit_spanned_lint(CAST_REF_TO_MUT, expr.span, CastRefToMutDiag);  | 
 | 70 | +        }  | 
 | 71 | +    }  | 
 | 72 | +}  | 
0 commit comments