rustc_lint/
transmute.rs

1use rustc_ast::LitKind;
2use rustc_errors::Applicability;
3use rustc_hir::def::{DefKind, Res};
4use rustc_hir::def_id::LocalDefId;
5use rustc_hir::{self as hir};
6use rustc_macros::LintDiagnostic;
7use rustc_middle::ty::{self, Ty};
8use rustc_session::{declare_lint, impl_lint_pass};
9use rustc_span::sym;
10
11use crate::lints::{IntegerToPtrTransmutes, IntegerToPtrTransmutesSuggestion};
12use crate::{LateContext, LateLintPass};
13
14declare_lint! {
15    /// The `ptr_to_integer_transmute_in_consts` lint detects pointer to integer
16    /// transmute in const functions and associated constants.
17    ///
18    /// ### Example
19    ///
20    /// ```rust
21    /// const fn foo(ptr: *const u8) -> usize {
22    ///    unsafe {
23    ///        std::mem::transmute::<*const u8, usize>(ptr)
24    ///    }
25    /// }
26    /// ```
27    ///
28    /// {{produces}}
29    ///
30    /// ### Explanation
31    ///
32    /// Transmuting pointers to integers in a `const` context is undefined behavior.
33    /// Any attempt to use the resulting integer will abort const-evaluation.
34    ///
35    /// But sometimes the compiler might not emit an error for pointer to integer transmutes
36    /// inside const functions and associated consts because they are evaluated only when referenced.
37    /// Therefore, this lint serves as an extra layer of defense to prevent any undefined behavior
38    /// from compiling without any warnings or errors.
39    ///
40    /// See [std::mem::transmute] in the reference for more details.
41    ///
42    /// [std::mem::transmute]: https://doc.rust-lang.org/std/mem/fn.transmute.html
43    pub PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS,
44    Warn,
45    "detects pointer to integer transmutes in const functions and associated constants",
46}
47
48declare_lint! {
49    /// The `unnecessary_transmutes` lint detects transmutations that have safer alternatives.
50    ///
51    /// ### Example
52    ///
53    /// ```rust
54    /// fn bytes_at_home(x: [u8; 4]) -> u32 {
55    ///   unsafe { std::mem::transmute(x) }
56    /// }
57    /// ```
58    ///
59    /// {{produces}}
60    ///
61    /// ### Explanation
62    ///
63    /// Using an explicit method is preferable over calls to
64    /// [`transmute`](https://doc.rust-lang.org/std/mem/fn.transmute.html) as
65    /// they more clearly communicate the intent, are easier to review, and
66    /// are less likely to accidentally result in unsoundness.
67    pub UNNECESSARY_TRANSMUTES,
68    Warn,
69    "detects transmutes that can also be achieved by other operations"
70}
71
72declare_lint! {
73    /// The `integer_to_ptr_transmutes` lint detects integer to pointer
74    /// transmutes where the resulting pointers are undefined behavior to dereference.
75    ///
76    /// ### Example
77    ///
78    /// ```rust
79    /// fn foo(a: usize) -> *const u8 {
80    ///    unsafe {
81    ///        std::mem::transmute::<usize, *const u8>(a)
82    ///    }
83    /// }
84    /// ```
85    ///
86    /// {{produces}}
87    ///
88    /// ### Explanation
89    ///
90    /// Any attempt to use the resulting pointers are undefined behavior as the resulting
91    /// pointers won't have any provenance.
92    ///
93    /// Alternatively, [`std::ptr::with_exposed_provenance`] should be used, as they do not
94    /// carry the provenance requirement. If wanting to create pointers without provenance
95    /// [`std::ptr::without_provenance`] should be used instead.
96    ///
97    /// See [`std::mem::transmute`] in the reference for more details.
98    ///
99    /// [`std::mem::transmute`]: https://doc.rust-lang.org/std/mem/fn.transmute.html
100    /// [`std::ptr::with_exposed_provenance`]: https://doc.rust-lang.org/std/ptr/fn.with_exposed_provenance.html
101    /// [`std::ptr::without_provenance`]: https://doc.rust-lang.org/std/ptr/fn.without_provenance.html
102    pub INTEGER_TO_PTR_TRANSMUTES,
103    Warn,
104    "detects integer to pointer transmutes",
105}
106
107pub(crate) struct CheckTransmutes;
108
109impl_lint_pass!(CheckTransmutes => [PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS, UNNECESSARY_TRANSMUTES, INTEGER_TO_PTR_TRANSMUTES]);
110
111impl<'tcx> LateLintPass<'tcx> for CheckTransmutes {
112    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
113        let hir::ExprKind::Call(callee, [arg]) = expr.kind else {
114            return;
115        };
116        let hir::ExprKind::Path(qpath) = callee.kind else {
117            return;
118        };
119        let Res::Def(DefKind::Fn, def_id) = cx.qpath_res(&qpath, callee.hir_id) else {
120            return;
121        };
122        if !cx.tcx.is_intrinsic(def_id, sym::transmute) {
123            return;
124        };
125        let body_owner_def_id = cx.tcx.hir_enclosing_body_owner(expr.hir_id);
126        let const_context = cx.tcx.hir_body_const_context(body_owner_def_id);
127        let args = cx.typeck_results().node_args(callee.hir_id);
128
129        let src = args.type_at(0);
130        let dst = args.type_at(1);
131
132        check_ptr_transmute_in_const(cx, expr, body_owner_def_id, const_context, src, dst);
133        check_unnecessary_transmute(cx, expr, callee, arg, const_context, src, dst);
134        check_int_to_ptr_transmute(cx, expr, arg, src, dst);
135    }
136}
137
138/// Check for transmutes from integer to pointers (*const/*mut and &/&mut).
139///
140/// Using the resulting pointers would be undefined behavior.
141fn check_int_to_ptr_transmute<'tcx>(
142    cx: &LateContext<'tcx>,
143    expr: &'tcx hir::Expr<'tcx>,
144    arg: &'tcx hir::Expr<'tcx>,
145    src: Ty<'tcx>,
146    dst: Ty<'tcx>,
147) {
148    if !matches!(src.kind(), ty::Uint(_) | ty::Int(_)) {
149        return;
150    }
151    let (ty::Ref(_, inner_ty, mutbl) | ty::RawPtr(inner_ty, mutbl)) = dst.kind() else {
152        return;
153    };
154    // bail-out if the argument is literal 0 as we have other lints for those cases
155    if matches!(arg.kind, hir::ExprKind::Lit(hir::Lit { node: LitKind::Int(v, _), .. }) if v == 0) {
156        return;
157    }
158    // bail-out if the inner type is a ZST
159    let Ok(layout_inner_ty) = cx.tcx.layout_of(cx.typing_env().as_query_input(*inner_ty)) else {
160        return;
161    };
162    if layout_inner_ty.is_1zst() {
163        return;
164    }
165
166    let suffix = if mutbl.is_mut() { "_mut" } else { "" };
167    cx.tcx.emit_node_span_lint(
168        INTEGER_TO_PTR_TRANSMUTES,
169        expr.hir_id,
170        expr.span,
171        IntegerToPtrTransmutes {
172            suggestion: if layout_inner_ty.is_sized() {
173                Some(if dst.is_ref() {
174                    IntegerToPtrTransmutesSuggestion::ToRef {
175                        dst: *inner_ty,
176                        suffix,
177                        ref_mutbl: mutbl.prefix_str(),
178                        start_call: expr.span.shrink_to_lo().until(arg.span),
179                    }
180                } else {
181                    IntegerToPtrTransmutesSuggestion::ToPtr {
182                        dst: *inner_ty,
183                        suffix,
184                        start_call: expr.span.shrink_to_lo().until(arg.span),
185                    }
186                })
187            } else {
188                // We can't suggest using `with_exposed_provenance` for unsized type
189                // so don't suggest anything.
190                None
191            },
192        },
193    );
194}
195
196/// Check for transmutes that exhibit undefined behavior.
197/// For example, transmuting pointers to integers in a const context.
198///
199/// Why do we consider const functions and associated constants only?
200///
201/// Generally, undefined behavior in const items are handled by the evaluator.
202/// But, const functions and associated constants are evaluated only when referenced.
203/// This can result in undefined behavior in a library going unnoticed until
204/// the function or constant is actually used.
205///
206/// Therefore, we only consider const functions and associated constants here and leave
207/// other const items to be handled by the evaluator.
208fn check_ptr_transmute_in_const<'tcx>(
209    cx: &LateContext<'tcx>,
210    expr: &'tcx hir::Expr<'tcx>,
211    body_owner_def_id: LocalDefId,
212    const_context: Option<hir::ConstContext>,
213    src: Ty<'tcx>,
214    dst: Ty<'tcx>,
215) {
216    if matches!(const_context, Some(hir::ConstContext::ConstFn))
217        || matches!(cx.tcx.def_kind(body_owner_def_id), DefKind::AssocConst)
218    {
219        if src.is_raw_ptr() && dst.is_integral() {
220            cx.tcx.emit_node_span_lint(
221                PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS,
222                expr.hir_id,
223                expr.span,
224                UndefinedTransmuteLint,
225            );
226        }
227    }
228}
229
230/// Check for transmutes that overlap with stdlib methods.
231/// For example, transmuting `[u8; 4]` to `u32`.
232///
233/// We chose not to lint u8 -> bool transmutes, see #140431.
234fn check_unnecessary_transmute<'tcx>(
235    cx: &LateContext<'tcx>,
236    expr: &'tcx hir::Expr<'tcx>,
237    callee: &'tcx hir::Expr<'tcx>,
238    arg: &'tcx hir::Expr<'tcx>,
239    const_context: Option<hir::ConstContext>,
240    src: Ty<'tcx>,
241    dst: Ty<'tcx>,
242) {
243    let callee_span = callee.span.find_ancestor_inside(expr.span).unwrap_or(callee.span);
244    let (sugg, help) = match (src.kind(), dst.kind()) {
245        // dont check the length; transmute does that for us.
246        // [u8; _] => primitive
247        (ty::Array(t, _), ty::Uint(_) | ty::Float(_) | ty::Int(_))
248            if *t.kind() == ty::Uint(ty::UintTy::U8) =>
249        {
250            (
251                Some(vec![(callee_span, format!("{dst}::from_ne_bytes"))]),
252                Some(
253                    "there's also `from_le_bytes` and `from_be_bytes` if you expect a particular byte order",
254                ),
255            )
256        }
257        // primitive => [u8; _]
258        (ty::Uint(_) | ty::Float(_) | ty::Int(_), ty::Array(t, _))
259            if *t.kind() == ty::Uint(ty::UintTy::U8) =>
260        {
261            (
262                Some(vec![(callee_span, format!("{src}::to_ne_bytes"))]),
263                Some(
264                    "there's also `to_le_bytes` and `to_be_bytes` if you expect a particular byte order",
265                ),
266            )
267        }
268        // char → u32
269        (ty::Char, ty::Uint(ty::UintTy::U32)) => {
270            (Some(vec![(callee_span, "u32::from".to_string())]), None)
271        }
272        // char (→ u32) → i32
273        (ty::Char, ty::Int(ty::IntTy::I32)) => (
274            Some(vec![
275                (callee_span, "u32::from".to_string()),
276                (expr.span.shrink_to_hi(), ".cast_signed()".to_string()),
277            ]),
278            None,
279        ),
280        // u32 → char
281        (ty::Uint(ty::UintTy::U32), ty::Char) => (
282            Some(vec![(callee_span, "char::from_u32_unchecked".to_string())]),
283            Some("consider using `char::from_u32(…).unwrap()`"),
284        ),
285        // i32 → char
286        (ty::Int(ty::IntTy::I32), ty::Char) => (
287            Some(vec![
288                (callee_span, "char::from_u32_unchecked(i32::cast_unsigned".to_string()),
289                (expr.span.shrink_to_hi(), ")".to_string()),
290            ]),
291            Some("consider using `char::from_u32(i32::cast_unsigned(…)).unwrap()`"),
292        ),
293        // uNN → iNN
294        (ty::Uint(_), ty::Int(_)) => {
295            (Some(vec![(callee_span, format!("{src}::cast_signed"))]), None)
296        }
297        // iNN → uNN
298        (ty::Int(_), ty::Uint(_)) => {
299            (Some(vec![(callee_span, format!("{src}::cast_unsigned"))]), None)
300        }
301        // fNN → usize, isize
302        (ty::Float(_), ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize)) => (
303            Some(vec![
304                (callee_span, format!("{src}::to_bits")),
305                (expr.span.shrink_to_hi(), format!(" as {dst}")),
306            ]),
307            None,
308        ),
309        // fNN (→ uNN) → iNN
310        (ty::Float(_), ty::Int(..)) => (
311            Some(vec![
312                (callee_span, format!("{src}::to_bits")),
313                (expr.span.shrink_to_hi(), ".cast_signed()".to_string()),
314            ]),
315            None,
316        ),
317        // fNN → uNN
318        (ty::Float(_), ty::Uint(..)) => {
319            (Some(vec![(callee_span, format!("{src}::to_bits"))]), None)
320        }
321        // xsize → fNN
322        (ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize), ty::Float(_)) => (
323            Some(vec![
324                (callee_span, format!("{dst}::from_bits")),
325                (arg.span.shrink_to_hi(), " as _".to_string()),
326            ]),
327            None,
328        ),
329        // iNN (→ uNN) → fNN
330        (ty::Int(_), ty::Float(_)) => (
331            Some(vec![
332                (callee_span, format!("{dst}::from_bits({src}::cast_unsigned")),
333                (expr.span.shrink_to_hi(), ")".to_string()),
334            ]),
335            None,
336        ),
337        // uNN → fNN
338        (ty::Uint(_), ty::Float(_)) => {
339            (Some(vec![(callee_span, format!("{dst}::from_bits"))]), None)
340        }
341        // bool → x8 in const context since `From::from` is not const yet
342        // FIXME: Consider arg expr's precedence to avoid parentheses.
343        // FIXME(const_traits): Remove this when `From::from` is constified.
344        (ty::Bool, ty::Int(..) | ty::Uint(..)) if const_context.is_some() => (
345            Some(vec![
346                (callee_span, "".to_string()),
347                (expr.span.shrink_to_hi(), format!(" as {dst}")),
348            ]),
349            None,
350        ),
351        // bool → x8 using `x8::from`
352        (ty::Bool, ty::Int(..) | ty::Uint(..)) => {
353            (Some(vec![(callee_span, format!("{dst}::from"))]), None)
354        }
355        _ => return,
356    };
357
358    cx.tcx.node_span_lint(UNNECESSARY_TRANSMUTES, expr.hir_id, expr.span, |diag| {
359        diag.primary_message("unnecessary transmute");
360        if let Some(sugg) = sugg {
361            diag.multipart_suggestion("replace this with", sugg, Applicability::MachineApplicable);
362        }
363        if let Some(help) = help {
364            diag.help(help);
365        }
366    });
367}
368
369#[derive(LintDiagnostic)]
370#[diag(lint_undefined_transmute)]
371#[note]
372#[note(lint_note2)]
373#[help]
374pub(crate) struct UndefinedTransmuteLint;