Skip to content

Commit

Permalink
Make message for &T -> &mut T transmute more accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
5225225 committed Jan 9, 2022
1 parent 092e1c9 commit 36a1141
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/builtin.rs
Expand Up @@ -1248,7 +1248,7 @@ declare_lint! {
/// [`UnsafeCell`]: https://doc.rust-lang.org/std/cell/struct.UnsafeCell.html
MUTABLE_TRANSMUTES,
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]);
Expand All @@ -1260,8 +1260,8 @@ impl<'tcx> LateLintPass<'tcx> for MutableTransmutes {
get_transmute_from_to(cx, expr).map(|(ty1, ty2)| (ty1.kind(), ty2.kind()))
{
if to_mt == hir::Mutability::Mut && from_mt == hir::Mutability::Not {
let msg = "mutating transmuted &mut T from &T may cause undefined behavior, \
consider instead using an UnsafeCell";
let msg = "transmuting &T to &mut T is undefined behavior, \
even if the reference is unused, consider instead using an UnsafeCell";
cx.struct_span_lint(MUTABLE_TRANSMUTES, expr.span, |lint| lint.build(msg).emit());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/transmute/transmute-imut-to-mut.rs
Expand Up @@ -4,5 +4,5 @@ use std::mem::transmute;

fn main() {
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
}
2 changes: 1 addition & 1 deletion src/test/ui/transmute/transmute-imut-to-mut.stderr
@@ -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
|
LL | let _a: &mut u8 = unsafe { transmute(&1u8) };
Expand Down

0 comments on commit 36a1141

Please sign in to comment.