|
| 1 | +use rustc_middle::mir::visit::Visitor; |
| 2 | +use rustc_middle::mir::{Body, Location, Operand, Terminator, TerminatorKind}; |
| 3 | +use rustc_middle::ty::*; |
| 4 | +use rustc_session::lint::builtin::UNNECESSARY_TRANSMUTATE; |
| 5 | +use rustc_span::source_map::Spanned; |
| 6 | +use rustc_span::{Span, sym}; |
| 7 | + |
| 8 | +use crate::errors::UnnecessaryTransmute as Error; |
| 9 | + |
| 10 | +/// Check for transmutes that overlap with stdlib methods. |
| 11 | +/// For example, transmuting `[u8; 4]` to `u32`. |
| 12 | +pub(super) struct CheckUnnecessaryTransmutes; |
| 13 | + |
| 14 | +impl<'tcx> crate::MirLint<'tcx> for CheckUnnecessaryTransmutes { |
| 15 | + fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) { |
| 16 | + let mut checker = UnnecessaryTransmuteChecker { body, tcx }; |
| 17 | + checker.visit_body(body); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +struct UnnecessaryTransmuteChecker<'a, 'tcx> { |
| 22 | + body: &'a Body<'tcx>, |
| 23 | + tcx: TyCtxt<'tcx>, |
| 24 | +} |
| 25 | + |
| 26 | +impl<'a, 'tcx> UnnecessaryTransmuteChecker<'a, 'tcx> { |
| 27 | + fn is_redundant_transmute( |
| 28 | + &self, |
| 29 | + function: &Operand<'tcx>, |
| 30 | + arg: String, |
| 31 | + span: Span, |
| 32 | + ) -> Option<Error> { |
| 33 | + let fn_sig = function.ty(self.body, self.tcx).fn_sig(self.tcx).skip_binder(); |
| 34 | + let [input] = fn_sig.inputs() else { return None }; |
| 35 | + |
| 36 | + let err = |sugg| Error { span, sugg, help: None }; |
| 37 | + |
| 38 | + Some(match (input.kind(), fn_sig.output().kind()) { |
| 39 | + // dont check the length; transmute does that for us. |
| 40 | + // [u8; _] => primitive |
| 41 | + (Array(t, _), Uint(_) | Float(_) | Int(_)) if *t.kind() == Uint(UintTy::U8) => Error { |
| 42 | + sugg: format!("{}::from_ne_bytes({arg})", fn_sig.output()), |
| 43 | + help: Some( |
| 44 | + "there's also `from_le_bytes` and `from_ne_bytes` if you expect a particular byte order", |
| 45 | + ), |
| 46 | + span, |
| 47 | + }, |
| 48 | + // primitive => [u8; _] |
| 49 | + (Uint(_) | Float(_) | Int(_), Array(t, _)) if *t.kind() == Uint(UintTy::U8) => Error { |
| 50 | + sugg: format!("{input}::to_ne_bytes({arg})"), |
| 51 | + help: Some( |
| 52 | + "there's also `to_le_bytes` and `to_ne_bytes` if you expect a particular byte order", |
| 53 | + ), |
| 54 | + span, |
| 55 | + }, |
| 56 | + // char → u32 |
| 57 | + (Char, Uint(UintTy::U32)) => err(format!("u32::from({arg})")), |
| 58 | + // u32 → char |
| 59 | + (Uint(UintTy::U32), Char) => Error { |
| 60 | + sugg: format!("char::from_u32_unchecked({arg})"), |
| 61 | + help: Some("consider `char::from_u32(…).unwrap()`"), |
| 62 | + span, |
| 63 | + }, |
| 64 | + // uNN → iNN |
| 65 | + (Uint(ty), Int(_)) => err(format!("{}::cast_signed({arg})", ty.name_str())), |
| 66 | + // iNN → uNN |
| 67 | + (Int(ty), Uint(_)) => err(format!("{}::cast_unsigned({arg})", ty.name_str())), |
| 68 | + // fNN → uNN |
| 69 | + (Float(ty), Uint(..)) => err(format!("{}::to_bits({arg})", ty.name_str())), |
| 70 | + // uNN → fNN |
| 71 | + (Uint(_), Float(ty)) => err(format!("{}::from_bits({arg})", ty.name_str())), |
| 72 | + // bool → { x8 } |
| 73 | + (Bool, Int(..) | Uint(..)) => err(format!("({arg}) as {}", fn_sig.output())), |
| 74 | + // u8 → bool |
| 75 | + (Uint(_), Bool) => err(format!("({arg} == 1)")), |
| 76 | + _ => return None, |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<'tcx> Visitor<'tcx> for UnnecessaryTransmuteChecker<'_, 'tcx> { |
| 82 | + // Check each block's terminator for calls to pointer to integer transmutes |
| 83 | + // in const functions or associated constants and emit a lint. |
| 84 | + fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) { |
| 85 | + if let TerminatorKind::Call { func, args, .. } = &terminator.kind |
| 86 | + && let [Spanned { span: arg, .. }] = **args |
| 87 | + && let Some((func_def_id, _)) = func.const_fn_def() |
| 88 | + && self.tcx.is_intrinsic(func_def_id, sym::transmute) |
| 89 | + && let span = self.body.source_info(location).span |
| 90 | + && let Some(lint) = self.is_redundant_transmute( |
| 91 | + func, |
| 92 | + self.tcx.sess.source_map().span_to_snippet(arg).expect("ok"), |
| 93 | + span, |
| 94 | + ) |
| 95 | + && let Some(call_id) = self.body.source.def_id().as_local() |
| 96 | + { |
| 97 | + let hir_id = self.tcx.local_def_id_to_hir_id(call_id); |
| 98 | + |
| 99 | + self.tcx.emit_node_span_lint(UNNECESSARY_TRANSMUTATE, hir_id, span, lint); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments