2021-03-12 08:30:50 -06:00
|
|
|
use super::utils::is_layout_incompatible;
|
|
|
|
use super::UNSOUND_COLLECTION_TRANSMUTE;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2021-07-29 05:16:06 -05:00
|
|
|
use clippy_utils::match_any_diagnostic_items;
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_hir::Expr;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_middle::ty::{self, Ty};
|
2021-07-29 05:16:06 -05:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2021-03-12 08:30:50 -06:00
|
|
|
|
|
|
|
// used to check for UNSOUND_COLLECTION_TRANSMUTE
|
2021-07-29 05:16:06 -05:00
|
|
|
static COLLECTIONS: &[Symbol] = &[
|
2021-10-02 18:51:01 -05:00
|
|
|
sym::Vec,
|
|
|
|
sym::VecDeque,
|
2021-07-29 05:16:06 -05:00
|
|
|
sym::BinaryHeap,
|
|
|
|
sym::BTreeSet,
|
|
|
|
sym::BTreeMap,
|
2021-10-02 18:51:01 -05:00
|
|
|
sym::HashSet,
|
|
|
|
sym::HashMap,
|
2021-03-12 08:30:50 -06:00
|
|
|
];
|
|
|
|
|
|
|
|
/// Checks for `unsound_collection_transmute` lint.
|
|
|
|
/// Returns `true` if it's triggered, otherwise returns `false`.
|
|
|
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
|
|
|
|
match (&from_ty.kind(), &to_ty.kind()) {
|
|
|
|
(ty::Adt(from_adt, from_substs), ty::Adt(to_adt, to_substs)) => {
|
2021-07-29 05:16:06 -05:00
|
|
|
if from_adt.did != to_adt.did || match_any_diagnostic_items(cx, to_adt.did, COLLECTIONS).is_none() {
|
2021-03-12 08:30:50 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if from_substs
|
|
|
|
.types()
|
|
|
|
.zip(to_substs.types())
|
|
|
|
.any(|(from_ty, to_ty)| is_layout_incompatible(cx, from_ty, to_ty))
|
|
|
|
{
|
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
UNSOUND_COLLECTION_TRANSMUTE,
|
|
|
|
e.span,
|
|
|
|
&format!(
|
|
|
|
"transmute from `{}` to `{}` with mismatched layout is unsound",
|
|
|
|
from_ty, to_ty
|
|
|
|
),
|
|
|
|
);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|