Also consider transmute
with the invalid_reference_casting
lint
This commit is contained in:
parent
b03864d546
commit
b517dd5bc9
@ -98,32 +98,56 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
||||
fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> bool {
|
||||
let e = e.peel_blocks();
|
||||
|
||||
// <expr> as *mut ...
|
||||
let e = if let ExprKind::Cast(e, t) = e.kind
|
||||
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
|
||||
e
|
||||
// <expr>.cast_mut()
|
||||
} 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 false;
|
||||
};
|
||||
fn from_casts<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
|
||||
// <expr> as *mut ...
|
||||
let e = if let ExprKind::Cast(e, t) = e.kind
|
||||
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
|
||||
e
|
||||
// <expr>.cast_mut()
|
||||
} 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 None;
|
||||
};
|
||||
|
||||
let e = e.peel_blocks();
|
||||
let e = e.peel_blocks();
|
||||
|
||||
// <expr> as *const ...
|
||||
let e = if let ExprKind::Cast(e, t) = e.kind
|
||||
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Not, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
|
||||
e
|
||||
// ptr::from_ref(<expr>)
|
||||
} 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 {
|
||||
// <expr> as *const ...
|
||||
let e = if let ExprKind::Cast(e, t) = e.kind
|
||||
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Not, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
|
||||
e
|
||||
// ptr::from_ref(<expr>)
|
||||
} 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 None;
|
||||
};
|
||||
|
||||
Some(e)
|
||||
}
|
||||
|
||||
fn from_transmute<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
e: &'tcx Expr<'tcx>,
|
||||
) -> Option<&'tcx Expr<'tcx>> {
|
||||
// mem::transmute::<_, *mut _>(<expr>)
|
||||
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::transmute, def_id)
|
||||
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(e.hir_id).kind() {
|
||||
Some(arg)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
let Some(e) = from_casts(cx, e).or_else(|| from_transmute(cx, e)) else {
|
||||
return false;
|
||||
};
|
||||
|
||||
|
@ -24,6 +24,8 @@ unsafe fn ref_to_mut() {
|
||||
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
|
||||
let _num = &mut *(std::ptr::from_ref({ num }) as *mut i32);
|
||||
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
|
||||
let _num = &mut *std::mem::transmute::<_, *mut i32>(num);
|
||||
//~^ ERROR casting `&T` to `&mut T` is undefined behavior
|
||||
|
||||
let deferred = num as *const i32 as *mut i32;
|
||||
let _num = &mut *deferred;
|
||||
@ -47,6 +49,9 @@ unsafe fn assign_to_ref() {
|
||||
//~^ ERROR assigning to `&T` is undefined behavior
|
||||
*(std::ptr::from_ref({ num }) as *mut i32) += 1;
|
||||
//~^ ERROR assigning to `&T` is undefined behavior
|
||||
*std::mem::transmute::<_, *mut i32>(num) += 1;
|
||||
//~^ ERROR assigning to `&T` is undefined behavior
|
||||
|
||||
let value = num as *const i32 as *mut i32;
|
||||
*value = 1;
|
||||
//~^ ERROR assigning to `&T` is undefined behavior
|
||||
|
@ -37,7 +37,13 @@ LL | let _num = &mut *(std::ptr::from_ref({ num }) as *mut i32);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
|
||||
--> $DIR/reference_casting.rs:29:16
|
||||
--> $DIR/reference_casting.rs:27:16
|
||||
|
|
||||
LL | let _num = &mut *std::mem::transmute::<_, *mut i32>(num);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
|
||||
--> $DIR/reference_casting.rs:31:16
|
||||
|
|
||||
LL | let deferred = num as *const i32 as *mut i32;
|
||||
| ----------------------------- casting happend here
|
||||
@ -45,48 +51,54 @@ LL | let _num = &mut *deferred;
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||
--> $DIR/reference_casting.rs:38:5
|
||||
--> $DIR/reference_casting.rs:40:5
|
||||
|
|
||||
LL | *(a as *const _ as *mut _) = String::from("Replaced");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||
--> $DIR/reference_casting.rs:40:5
|
||||
--> $DIR/reference_casting.rs:42:5
|
||||
|
|
||||
LL | *(a as *const _ as *mut String) += " world";
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||
--> $DIR/reference_casting.rs:42:5
|
||||
--> $DIR/reference_casting.rs:44:5
|
||||
|
|
||||
LL | *std::ptr::from_ref(num).cast_mut() += 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||
--> $DIR/reference_casting.rs:44:5
|
||||
--> $DIR/reference_casting.rs:46:5
|
||||
|
|
||||
LL | *std::ptr::from_ref({ num }).cast_mut() += 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||
--> $DIR/reference_casting.rs:46:5
|
||||
--> $DIR/reference_casting.rs:48:5
|
||||
|
|
||||
LL | *{ std::ptr::from_ref(num) }.cast_mut() += 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||
--> $DIR/reference_casting.rs:48:5
|
||||
--> $DIR/reference_casting.rs:50:5
|
||||
|
|
||||
LL | *(std::ptr::from_ref({ num }) as *mut i32) += 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||
--> $DIR/reference_casting.rs:51:5
|
||||
--> $DIR/reference_casting.rs:52:5
|
||||
|
|
||||
LL | *std::mem::transmute::<_, *mut i32>(num) += 1;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: assigning to `&T` is undefined behavior, consider using an `UnsafeCell`
|
||||
--> $DIR/reference_casting.rs:56:5
|
||||
|
|
||||
LL | let value = num as *const i32 as *mut i32;
|
||||
| ----------------------------- casting happend here
|
||||
LL | *value = 1;
|
||||
| ^^^^^^^^^^
|
||||
|
||||
error: aborting due to 14 previous errors
|
||||
error: aborting due to 16 previous errors
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user