|
1 | | -use super::utils::can_be_expressed_as_pointer_cast; |
| 1 | +use super::utils::check_cast; |
2 | 2 | use super::TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS; |
3 | | -use clippy_utils::diagnostics::span_lint_and_then; |
4 | | -use clippy_utils::sugg; |
| 3 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 4 | +use clippy_utils::sugg::Sugg; |
5 | 5 | use rustc_errors::Applicability; |
6 | 6 | use rustc_hir::Expr; |
7 | 7 | use rustc_lint::LateContext; |
8 | | -use rustc_middle::ty::Ty; |
| 8 | +use rustc_middle::ty::{cast::CastKind, Ty}; |
9 | 9 |
|
10 | 10 | /// Checks for `transmutes_expressible_as_ptr_casts` lint. |
11 | 11 | /// Returns `true` if it's triggered, otherwise returns `false`. |
12 | 12 | pub(super) fn check<'tcx>( |
13 | 13 | cx: &LateContext<'tcx>, |
14 | 14 | e: &'tcx Expr<'_>, |
15 | 15 | from_ty: Ty<'tcx>, |
| 16 | + from_ty_adjusted: bool, |
16 | 17 | to_ty: Ty<'tcx>, |
17 | 18 | arg: &'tcx Expr<'_>, |
18 | 19 | ) -> bool { |
19 | | - if can_be_expressed_as_pointer_cast(cx, e, from_ty, to_ty) { |
20 | | - span_lint_and_then( |
21 | | - cx, |
22 | | - TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, |
23 | | - e.span, |
24 | | - &format!("transmute from `{from_ty}` to `{to_ty}` which could be expressed as a pointer cast instead"), |
25 | | - |diag| { |
26 | | - if let Some(arg) = sugg::Sugg::hir_opt(cx, arg) { |
27 | | - let sugg = arg.as_ty(to_ty.to_string()).to_string(); |
28 | | - diag.span_suggestion(e.span, "try", sugg, Applicability::MachineApplicable); |
29 | | - } |
30 | | - }, |
31 | | - ); |
32 | | - true |
33 | | - } else { |
34 | | - false |
35 | | - } |
| 20 | + use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast}; |
| 21 | + let mut app = Applicability::MachineApplicable; |
| 22 | + let sugg = match check_cast(cx, e, from_ty, to_ty) { |
| 23 | + Some(PtrPtrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast) => { |
| 24 | + Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app) |
| 25 | + .as_ty(to_ty.to_string()) |
| 26 | + .to_string() |
| 27 | + }, |
| 28 | + Some(PtrAddrCast) if !from_ty_adjusted => Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app) |
| 29 | + .as_ty(to_ty.to_string()) |
| 30 | + .to_string(), |
| 31 | + |
| 32 | + // The only adjustments here would be ref-to-ptr and unsize coercions. The result of an unsize coercions can't |
| 33 | + // be transmuted to a usize. For ref-to-ptr coercions, borrows need to be cast to a pointer before being cast to |
| 34 | + // a usize. |
| 35 | + Some(PtrAddrCast) => format!( |
| 36 | + "{} as {to_ty}", |
| 37 | + Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app).as_ty(from_ty) |
| 38 | + ), |
| 39 | + _ => return false, |
| 40 | + }; |
| 41 | + |
| 42 | + span_lint_and_sugg( |
| 43 | + cx, |
| 44 | + TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS, |
| 45 | + e.span, |
| 46 | + &format!("transmute from `{from_ty}` to `{to_ty}` which could be expressed as a pointer cast instead"), |
| 47 | + "try", |
| 48 | + sugg, |
| 49 | + app, |
| 50 | + ); |
| 51 | + true |
36 | 52 | } |
0 commit comments