Make message for &T -> &mut T transmute more accurate

This commit is contained in:
5225225 2022-01-09 22:15:23 +00:00
parent 092e1c9d23
commit 36a11417fa
3 changed files with 5 additions and 5 deletions

View File

@ -1248,7 +1248,7 @@ fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
/// [`UnsafeCell`]: https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html /// [`UnsafeCell`]: https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html
MUTABLE_TRANSMUTES, MUTABLE_TRANSMUTES,
Deny, Deny,
"mutating transmuted &mut T from &T may cause undefined behavior" "transmuting &T to &mut T is undefined behavior, even if the reference is unused"
} }
declare_lint_pass!(MutableTransmutes => [MUTABLE_TRANSMUTES]); declare_lint_pass!(MutableTransmutes => [MUTABLE_TRANSMUTES]);
@ -1260,8 +1260,8 @@ fn check_expr(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (ty1.kind(), ty2.kind())) get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (ty1.kind(), ty2.kind()))
{ {
if to_mt == hir::Mutability::Mut && from_mt == hir::Mutability::Not { if to_mt == hir::Mutability::Mut && from_mt == hir::Mutability::Not {
let msg = "mutating transmuted &mut T from &T may cause undefined behavior, \ let msg = "transmuting &T to &mut T is undefined behavior, \
consider instead using an UnsafeCell"; even if the reference is unused, consider instead using an UnsafeCell";
cx.struct_span_lint(MUTABLE_TRANSMUTES, expr.span, |lint| lint.build(msg).emit()); cx.struct_span_lint(MUTABLE_TRANSMUTES, expr.span, |lint| lint.build(msg).emit());
} }
} }

View File

@ -4,5 +4,5 @@
fn main() { fn main() {
let _a: &mut u8 = unsafe { transmute(&1u8) }; let _a: &mut u8 = unsafe { transmute(&1u8) };
//~^ ERROR mutating transmuted &mut T from &T may cause undefined behavior //~^ ERROR transmuting &T to &mut T is undefined behavior, even if the reference is unused, consider instead using an UnsafeCell
} }

View File

@ -1,4 +1,4 @@
error: mutating transmuted &mut T from &T may cause undefined behavior, consider instead using an UnsafeCell error: transmuting &T to &mut T is undefined behavior, even if the reference is unused, consider instead using an UnsafeCell
--> $DIR/transmute-imut-to-mut.rs:6:32 --> $DIR/transmute-imut-to-mut.rs:6:32
| |
LL | let _a: &mut u8 = unsafe { transmute(&1u8) }; LL | let _a: &mut u8 = unsafe { transmute(&1u8) };