]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | use rustc_hir::{Expr, ExprKind, MutTy, Mutability, TyKind, UnOp}; |
2 | use rustc_lint::LateContext; | |
3 | use rustc_middle::ty; | |
4 | ||
5 | use if_chain::if_chain; | |
6 | ||
7 | use crate::utils::span_lint; | |
8 | ||
9 | use super::CAST_REF_TO_MUT; | |
10 | ||
11 | pub(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 | } |