Skip to content

Commit 2a606b5

Browse files
Don't lint lifetime-only transmutes
1 parent 400aab9 commit 2a606b5

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

clippy_lints/src/transmute.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ declare_clippy_lint! {
192192
declare_clippy_lint! {
193193
pub TRANSMUTE_PTR_TO_PTR,
194194
complexity,
195-
"transmutes from a pointer to a reference type"
195+
"transmutes from a pointer to a pointer / a reference to a reference"
196196
}
197197

198198
pub struct Transmute;
@@ -363,23 +363,26 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
363363
}
364364
)
365365
} else {
366-
span_lint_and_then(
367-
cx,
368-
TRANSMUTE_PTR_TO_PTR,
369-
e.span,
370-
"transmute from a reference to a reference",
371-
|db| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
372-
let ty_from_and_mut = ty::TypeAndMut { ty: ty_from, mutbl: from_mutbl };
373-
let ty_to_and_mut = ty::TypeAndMut { ty: ty_to, mutbl: to_mutbl };
374-
let sugg_paren = arg.as_ty(cx.tcx.mk_ptr(ty_from_and_mut)).as_ty(cx.tcx.mk_ptr(ty_to_and_mut));
375-
let sugg = if to_mutbl == Mutability::MutMutable {
376-
sugg_paren.mut_addr_deref()
377-
} else {
378-
sugg_paren.addr_deref()
379-
};
380-
db.span_suggestion(e.span, "try", sugg.to_string());
381-
},
382-
)
366+
// In this case they differ only in lifetime
367+
if ty_from != ty_to {
368+
span_lint_and_then(
369+
cx,
370+
TRANSMUTE_PTR_TO_PTR,
371+
e.span,
372+
"transmute from a reference to a reference",
373+
|db| if let Some(arg) = sugg::Sugg::hir_opt(cx, &args[0]) {
374+
let ty_from_and_mut = ty::TypeAndMut { ty: ty_from, mutbl: from_mutbl };
375+
let ty_to_and_mut = ty::TypeAndMut { ty: ty_to, mutbl: to_mutbl };
376+
let sugg_paren = arg.as_ty(cx.tcx.mk_ptr(ty_from_and_mut)).as_ty(cx.tcx.mk_ptr(ty_to_and_mut));
377+
let sugg = if to_mutbl == Mutability::MutMutable {
378+
sugg_paren.mut_addr_deref()
379+
} else {
380+
sugg_paren.addr_deref()
381+
};
382+
db.span_suggestion(e.span, "try", sugg.to_string());
383+
},
384+
)
385+
}
383386
}
384387
}
385388
},

tests/ui/transmute.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,17 @@ fn transmute_ptr_to_ptr() {
152152
let _: &f32 = std::mem::transmute(&1u32);
153153
let _: &mut f32 = std::mem::transmute(&mut 1u32);
154154
}
155-
// These should be fine
155+
// These should be fine:
156+
// Recommendations for solving the above; if these break we need to update
157+
// those suggestions
156158
let _ = ptr as *const f32;
157159
let _ = mut_ptr as *mut f32;
158160
let _ = unsafe { &*(&1u32 as *const u32 as *const f32) };
159161
let _ = unsafe { &mut *(&mut 1u32 as *mut u32 as *mut f32) };
162+
// This is just modifying the lifetime, and is one of the recommended uses
163+
// of transmute
164+
let n = 1u32;
165+
let _ = unsafe { std::mem::transmute::<&'_ u32, &'static u32>(&n) };
160166
}
161167

162168
fn main() { }

0 commit comments

Comments
 (0)