rust/clippy_lints/src/transmute/utils.rs
lcnr bb93c23c08 use TypingEnv when no infcx is available
the behavior of the type system not only depends on the current
assumptions, but also the currentnphase of the compiler. This is
mostly necessary as we need to decide whether and how to reveal
opaque types. We track this via the `TypingMode`.
2024-11-18 10:38:56 +01:00

18 lines
813 B
Rust

use rustc_lint::LateContext;
use rustc_middle::ty::Ty;
// check if the component types of the transmuted collection and the result have different ABI,
// size or alignment
pub(super) fn is_layout_incompatible<'tcx>(cx: &LateContext<'tcx>, from: Ty<'tcx>, to: Ty<'tcx>) -> bool {
let typing_env = cx.typing_env();
if let Ok(from) = cx.tcx.try_normalize_erasing_regions(typing_env, from)
&& let Ok(to) = cx.tcx.try_normalize_erasing_regions(typing_env, to)
&& let Ok(from_layout) = cx.tcx.layout_of(typing_env.as_query_input(from))
&& let Ok(to_layout) = cx.tcx.layout_of(typing_env.as_query_input(to))
{
from_layout.size != to_layout.size || from_layout.align.abi != to_layout.align.abi
} else {
// no idea about layout, so don't lint
false
}
}