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