]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/casts/cast_ref_to_mut.rs
Rollup merge of #102323 - Stoozy:master, r=cjgillot
[rust.git] / src / tools / clippy / clippy_lints / src / casts / cast_ref_to_mut.rs
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;
5 use rustc_middle::ty;
6
7 use super::CAST_REF_TO_MUT;
8
9 pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
10     if_chain! {
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();
17         then {
18             span_lint(
19                 cx,
20                 CAST_REF_TO_MUT,
21                 expr.span,
22                 "casting `&T` to `&mut T` may cause undefined behavior, consider instead using an `UnsafeCell`",
23             );
24         }
25     }
26 }