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
14#[doc =
r" The `ptr_to_integer_transmute_in_consts` lint detects pointer to integer"]
#[doc = r" transmute in const functions and associated constants."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" const fn foo(ptr: *const u8) -> usize {"]
#[doc = r" unsafe {"]
#[doc = r" std::mem::transmute::<*const u8, usize>(ptr)"]
#[doc = r" }"]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" Transmuting pointers to integers in a `const` context is undefined behavior."]
#[doc =
r" Any attempt to use the resulting integer will abort const-evaluation."]
#[doc = r""]
#[doc =
r" But sometimes the compiler might not emit an error for pointer to integer transmutes"]
#[doc =
r" inside const functions and associated consts because they are evaluated only when referenced."]
#[doc =
r" Therefore, this lint serves as an extra layer of defense to prevent any undefined behavior"]
#[doc = r" from compiling without any warnings or errors."]
#[doc = r""]
#[doc = r" See [std::mem::transmute] in the reference for more details."]
#[doc = r""]
#[doc =
r" [std::mem::transmute]: https://doc.rust-lang.org/std/mem/fn.transmute.html"]
pub static PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS",
default_level: ::rustc_lint_defs::Warn,
desc: "detects pointer to integer transmutes in const functions and associated constants",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
15 pub PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS,
44 Warn,
45 "detects pointer to integer transmutes in const functions and associated constants",
46}
47
48#[doc =
r" The `unnecessary_transmutes` lint detects transmutations that have safer alternatives."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" fn bytes_at_home(x: [u8; 4]) -> u32 {"]
#[doc = r" unsafe { std::mem::transmute(x) }"]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc = r" Using an explicit method is preferable over calls to"]
#[doc =
r" [`transmute`](https://doc.rust-lang.org/std/mem/fn.transmute.html) as"]
#[doc =
r" they more clearly communicate the intent, are easier to review, and"]
#[doc = r" are less likely to accidentally result in unsoundness."]
pub static UNNECESSARY_TRANSMUTES: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "UNNECESSARY_TRANSMUTES",
default_level: ::rustc_lint_defs::Warn,
desc: "detects transmutes that can also be achieved by other operations",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
49 pub UNNECESSARY_TRANSMUTES,
68 Warn,
69 "detects transmutes that can also be achieved by other operations"
70}
71
72#[doc = r" The `integer_to_ptr_transmutes` lint detects integer to pointer"]
#[doc =
r" transmutes where the resulting pointers are undefined behavior to dereference."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" fn foo(a: usize) -> *const u8 {"]
#[doc = r" unsafe {"]
#[doc = r" std::mem::transmute::<usize, *const u8>(a)"]
#[doc = r" }"]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" Any attempt to use the resulting pointers are undefined behavior as the resulting"]
#[doc = r" pointers won't have any provenance."]
#[doc = r""]
#[doc =
r" Alternatively, [`std::ptr::with_exposed_provenance`] should be used, as they do not"]
#[doc =
r" carry the provenance requirement. If wanting to create pointers without provenance"]
#[doc = r" [`std::ptr::without_provenance`] should be used instead."]
#[doc = r""]
#[doc = r" See [`std::mem::transmute`] in the reference for more details."]
#[doc = r""]
#[doc =
r" [`std::mem::transmute`]: https://doc.rust-lang.org/std/mem/fn.transmute.html"]
#[doc =
r" [`std::ptr::with_exposed_provenance`]: https://doc.rust-lang.org/std/ptr/fn.with_exposed_provenance.html"]
#[doc =
r" [`std::ptr::without_provenance`]: https://doc.rust-lang.org/std/ptr/fn.without_provenance.html"]
pub static INTEGER_TO_PTR_TRANSMUTES: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "INTEGER_TO_PTR_TRANSMUTES",
default_level: ::rustc_lint_defs::Warn,
desc: "detects integer to pointer transmutes",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
73 pub INTEGER_TO_PTR_TRANSMUTES,
103 Warn,
104 "detects integer to pointer transmutes",
105}
106
107pub(crate) struct CheckTransmutes;
108
109impl ::rustc_lint_defs::LintPass for CheckTransmutes {
fn name(&self) -> &'static str { "CheckTransmutes" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
<[_]>::into_vec(::alloc::boxed::box_new([PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS,
UNNECESSARY_TRANSMUTES, INTEGER_TO_PTR_TRANSMUTES]))
}
}
impl CheckTransmutes {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
<[_]>::into_vec(::alloc::boxed::box_new([PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS,
UNNECESSARY_TRANSMUTES, INTEGER_TO_PTR_TRANSMUTES]))
}
}impl_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
138fn 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 !#[allow(non_exhaustive_omitted_patterns)] match src.kind() {
ty::Uint(_) | ty::Int(_) => true,
_ => false,
}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 if let hir::ExprKind::Lit(hir::Lit { node: LitKind::Int(v, _), .. }) = arg.kind
156 && v == 0
157 {
158 return;
159 }
160 let Ok(layout_inner_ty) = cx.tcx.layout_of(cx.typing_env().as_query_input(*inner_ty)) else {
162 return;
163 };
164 if layout_inner_ty.is_1zst() {
165 return;
166 }
167
168 let suffix = if mutbl.is_mut() { "_mut" } else { "" };
169 cx.tcx.emit_node_span_lint(
170 INTEGER_TO_PTR_TRANSMUTES,
171 expr.hir_id,
172 expr.span,
173 IntegerToPtrTransmutes {
174 suggestion: if layout_inner_ty.is_sized() {
175 Some(if dst.is_ref() {
176 IntegerToPtrTransmutesSuggestion::ToRef {
177 dst: *inner_ty,
178 suffix,
179 ref_mutbl: mutbl.prefix_str(),
180 start_call: expr.span.shrink_to_lo().until(arg.span),
181 }
182 } else {
183 IntegerToPtrTransmutesSuggestion::ToPtr {
184 dst: *inner_ty,
185 suffix,
186 start_call: expr.span.shrink_to_lo().until(arg.span),
187 }
188 })
189 } else {
190 None
193 },
194 },
195 );
196}
197
198fn check_ptr_transmute_in_const<'tcx>(
211 cx: &LateContext<'tcx>,
212 expr: &'tcx hir::Expr<'tcx>,
213 body_owner_def_id: LocalDefId,
214 const_context: Option<hir::ConstContext>,
215 src: Ty<'tcx>,
216 dst: Ty<'tcx>,
217) {
218 if #[allow(non_exhaustive_omitted_patterns)] match const_context {
Some(hir::ConstContext::ConstFn) => true,
_ => false,
}matches!(const_context, Some(hir::ConstContext::ConstFn))
219 || #[allow(non_exhaustive_omitted_patterns)] match cx.tcx.def_kind(body_owner_def_id)
{
DefKind::AssocConst => true,
_ => false,
}matches!(cx.tcx.def_kind(body_owner_def_id), DefKind::AssocConst)
220 {
221 if src.is_raw_ptr() && dst.is_integral() {
222 cx.tcx.emit_node_span_lint(
223 PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS,
224 expr.hir_id,
225 expr.span,
226 UndefinedTransmuteLint,
227 );
228 }
229 }
230}
231
232fn check_unnecessary_transmute<'tcx>(
237 cx: &LateContext<'tcx>,
238 expr: &'tcx hir::Expr<'tcx>,
239 callee: &'tcx hir::Expr<'tcx>,
240 arg: &'tcx hir::Expr<'tcx>,
241 const_context: Option<hir::ConstContext>,
242 src: Ty<'tcx>,
243 dst: Ty<'tcx>,
244) {
245 let callee_span = callee.span.find_ancestor_inside(expr.span).unwrap_or(callee.span);
246 let (sugg, help) = match (src.kind(), dst.kind()) {
247 (ty::Array(t, _), ty::Uint(_) | ty::Float(_) | ty::Int(_))
250 if *t.kind() == ty::Uint(ty::UintTy::U8) =>
251 {
252 (
253 Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::from_ne_bytes",
dst))
}))]))vec![(callee_span, format!("{dst}::from_ne_bytes"))]),
254 Some(
255 "there's also `from_le_bytes` and `from_be_bytes` if you expect a particular byte order",
256 ),
257 )
258 }
259 (ty::Uint(_) | ty::Float(_) | ty::Int(_), ty::Array(t, _))
261 if *t.kind() == ty::Uint(ty::UintTy::U8) =>
262 {
263 (
264 Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::to_ne_bytes", src))
}))]))vec![(callee_span, format!("{src}::to_ne_bytes"))]),
265 Some(
266 "there's also `to_le_bytes` and `to_be_bytes` if you expect a particular byte order",
267 ),
268 )
269 }
270 (ty::Char, ty::Uint(ty::UintTy::U32)) => {
272 (Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
"u32::from".to_string())]))vec![(callee_span, "u32::from".to_string())]), None)
273 }
274 (ty::Char, ty::Int(ty::IntTy::I32)) => (
276 Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
"u32::from".to_string()),
(expr.span.shrink_to_hi(), ".cast_signed()".to_string())]))vec![
277 (callee_span, "u32::from".to_string()),
278 (expr.span.shrink_to_hi(), ".cast_signed()".to_string()),
279 ]),
280 None,
281 ),
282 (ty::Uint(ty::UintTy::U32), ty::Char) => (
284 Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
"char::from_u32_unchecked".to_string())]))vec![(callee_span, "char::from_u32_unchecked".to_string())]),
285 Some("consider using `char::from_u32(…).unwrap()`"),
286 ),
287 (ty::Int(ty::IntTy::I32), ty::Char) => (
289 Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
"char::from_u32_unchecked(i32::cast_unsigned".to_string()),
(expr.span.shrink_to_hi(), ")".to_string())]))vec![
290 (callee_span, "char::from_u32_unchecked(i32::cast_unsigned".to_string()),
291 (expr.span.shrink_to_hi(), ")".to_string()),
292 ]),
293 Some("consider using `char::from_u32(i32::cast_unsigned(…)).unwrap()`"),
294 ),
295 (ty::Uint(_), ty::Int(_)) => {
297 (Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::cast_signed", src))
}))]))vec![(callee_span, format!("{src}::cast_signed"))]), None)
298 }
299 (ty::Int(_), ty::Uint(_)) => {
301 (Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::cast_unsigned",
src))
}))]))vec![(callee_span, format!("{src}::cast_unsigned"))]), None)
302 }
303 (ty::Float(_), ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize)) => (
305 Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::to_bits", src))
})),
(expr.span.shrink_to_hi(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" as {0}", dst))
}))]))vec![
306 (callee_span, format!("{src}::to_bits")),
307 (expr.span.shrink_to_hi(), format!(" as {dst}")),
308 ]),
309 None,
310 ),
311 (ty::Float(_), ty::Int(..)) => (
313 Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::to_bits", src))
})),
(expr.span.shrink_to_hi(), ".cast_signed()".to_string())]))vec![
314 (callee_span, format!("{src}::to_bits")),
315 (expr.span.shrink_to_hi(), ".cast_signed()".to_string()),
316 ]),
317 None,
318 ),
319 (ty::Float(_), ty::Uint(..)) => {
321 (Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::to_bits", src))
}))]))vec![(callee_span, format!("{src}::to_bits"))]), None)
322 }
323 (ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize), ty::Float(_)) => (
325 Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::from_bits", dst))
})), (arg.span.shrink_to_hi(), " as _".to_string())]))vec![
326 (callee_span, format!("{dst}::from_bits")),
327 (arg.span.shrink_to_hi(), " as _".to_string()),
328 ]),
329 None,
330 ),
331 (ty::Int(_), ty::Float(_)) => (
333 Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::from_bits({1}::cast_unsigned",
dst, src))
})), (expr.span.shrink_to_hi(), ")".to_string())]))vec![
334 (callee_span, format!("{dst}::from_bits({src}::cast_unsigned")),
335 (expr.span.shrink_to_hi(), ")".to_string()),
336 ]),
337 None,
338 ),
339 (ty::Uint(_), ty::Float(_)) => {
341 (Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::from_bits", dst))
}))]))vec![(callee_span, format!("{dst}::from_bits"))]), None)
342 }
343 (ty::Bool, ty::Int(..) | ty::Uint(..)) if const_context.is_some() => (
347 Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span, "".to_string()),
(expr.span.shrink_to_hi(),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" as {0}", dst))
}))]))vec![
348 (callee_span, "".to_string()),
349 (expr.span.shrink_to_hi(), format!(" as {dst}")),
350 ]),
351 None,
352 ),
353 (ty::Bool, ty::Int(..) | ty::Uint(..)) => {
355 (Some(<[_]>::into_vec(::alloc::boxed::box_new([(callee_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::from", dst))
}))]))vec![(callee_span, format!("{dst}::from"))]), None)
356 }
357 _ => return,
358 };
359
360 cx.tcx.node_span_lint(UNNECESSARY_TRANSMUTES, expr.hir_id, expr.span, |diag| {
361 diag.primary_message("unnecessary transmute");
362 if let Some(sugg) = sugg {
363 diag.multipart_suggestion("replace this with", sugg, Applicability::MachineApplicable);
364 }
365 if let Some(help) = help {
366 diag.help(help);
367 }
368 });
369}
370
371#[derive(const _: () =
{
impl<'__a> rustc_errors::LintDiagnostic<'__a, ()> for
UndefinedTransmuteLint {
#[track_caller]
fn decorate_lint<'__b>(self,
diag: &'__b mut rustc_errors::Diag<'__a, ()>) {
match self {
UndefinedTransmuteLint => {
diag.primary_message(crate::fluent_generated::lint_undefined_transmute);
diag.note(crate::fluent_generated::_subdiag::note);
diag.note(crate::fluent_generated::lint_note2);
diag.help(crate::fluent_generated::_subdiag::help);
;
diag
}
};
}
}
};LintDiagnostic)]
372#[diag(lint_undefined_transmute)]
373#[note]
374#[note(lint_note2)]
375#[help]
376pub(crate) struct UndefinedTransmuteLint;