2023-05-14 14:20:32 +00:00
|
|
|
use rustc_ast::Mutability;
|
|
|
|
use rustc_hir::{Expr, ExprKind, MutTy, TyKind, UnOp};
|
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_span::sym;
|
|
|
|
|
2023-07-06 19:45:24 +00:00
|
|
|
use crate::{lints::InvalidReferenceCastingDiag, LateContext, LateLintPass, LintContext};
|
2023-05-14 14:20:32 +00:00
|
|
|
|
|
|
|
declare_lint! {
|
2023-07-06 19:45:24 +00:00
|
|
|
/// The `invalid_reference_casting` lint checks for casts of `&T` to `&mut T`
|
2023-05-14 14:20:32 +00:00
|
|
|
/// without using interior mutability.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```rust,compile_fail
|
2023-07-06 19:50:34 +00:00
|
|
|
/// # #![deny(invalid_reference_casting)]
|
2023-05-14 14:20:32 +00:00
|
|
|
/// fn x(r: &i32) {
|
|
|
|
/// unsafe {
|
|
|
|
/// *(r as *const i32 as *mut i32) += 1;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// {{produces}}
|
|
|
|
///
|
|
|
|
/// ### Explanation
|
|
|
|
///
|
|
|
|
/// Casting `&T` to `&mut T` without using interior mutability is undefined behavior,
|
|
|
|
/// as it's a violation of Rust reference aliasing requirements.
|
|
|
|
///
|
|
|
|
/// `UnsafeCell` is the only way to obtain aliasable data that is considered
|
|
|
|
/// mutable.
|
2023-07-06 19:45:24 +00:00
|
|
|
INVALID_REFERENCE_CASTING,
|
2023-07-06 19:50:34 +00:00
|
|
|
Allow,
|
2023-05-14 14:20:32 +00:00
|
|
|
"casts of `&T` to `&mut T` without interior mutability"
|
|
|
|
}
|
|
|
|
|
2023-07-06 19:45:24 +00:00
|
|
|
declare_lint_pass!(InvalidReferenceCasting => [INVALID_REFERENCE_CASTING]);
|
2023-05-14 14:20:32 +00:00
|
|
|
|
2023-07-06 19:45:24 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
|
2023-05-14 14:20:32 +00:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
2023-07-13 01:49:27 +00:00
|
|
|
let ExprKind::Unary(UnOp::Deref, e) = &expr.kind else {
|
|
|
|
return;
|
|
|
|
};
|
2023-05-14 14:20:32 +00:00
|
|
|
|
|
|
|
let e = e.peel_blocks();
|
|
|
|
let e = if let ExprKind::Cast(e, t) = e.kind
|
|
|
|
&& let TyKind::Ptr(MutTy { mutbl: Mutability::Mut, .. }) = t.kind {
|
|
|
|
e
|
|
|
|
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
|
|
|
|
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
|
|
|
|
&& cx.tcx.is_diagnostic_item(sym::ptr_cast_mut, def_id) {
|
|
|
|
expr
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let e = e.peel_blocks();
|
|
|
|
let e = if let ExprKind::Cast(e, t) = e.kind
|
|
|
|
&& let TyKind::Ptr(MutTy { mutbl: Mutability::Not, .. }) = t.kind {
|
|
|
|
e
|
|
|
|
} else if let ExprKind::Call(path, [arg]) = e.kind
|
|
|
|
&& let ExprKind::Path(ref qpath) = path.kind
|
|
|
|
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
|
|
|
&& cx.tcx.is_diagnostic_item(sym::ptr_from_ref, def_id) {
|
|
|
|
arg
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
let e = e.peel_blocks();
|
|
|
|
if let ty::Ref(..) = cx.typeck_results().node_type(e.hir_id).kind() {
|
2023-07-06 19:45:24 +00:00
|
|
|
cx.emit_spanned_lint(INVALID_REFERENCE_CASTING, expr.span, InvalidReferenceCastingDiag);
|
2023-05-14 14:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|