]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/casts/cast_ref_to_mut.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / src / tools / clippy / clippy_lints / src / casts / cast_ref_to_mut.rs
CommitLineData
f20569fa
XL
1use rustc_hir::{Expr, ExprKind, MutTy, Mutability, TyKind, UnOp};
2use rustc_lint::LateContext;
3use rustc_middle::ty;
4
5use if_chain::if_chain;
6
7use crate::utils::span_lint;
8
9use super::CAST_REF_TO_MUT;
10
11pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
12 if_chain! {
13 if let ExprKind::Unary(UnOp::Deref, e) = &expr.kind;
14 if let ExprKind::Cast(e, t) = &e.kind;
15 if let TyKind::Ptr(MutTy { mutbl: Mutability::Mut, .. }) = t.kind;
16 if let ExprKind::Cast(e, t) = &e.kind;
17 if let TyKind::Ptr(MutTy { mutbl: Mutability::Not, .. }) = t.kind;
18 if let ty::Ref(..) = cx.typeck_results().node_type(e.hir_id).kind();
19 then {
20 span_lint(
21 cx,
22 CAST_REF_TO_MUT,
23 expr.span,
24 "casting `&T` to `&mut T` may cause undefined behavior, consider instead using an `UnsafeCell`",
25 );
26 }
27 }
28}