Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move unsound_collection_transmute to its own module
- Loading branch information
1 parent
e7d2393
commit c57a826
Showing
2 changed files
with
54 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
clippy_lints/src/transmute/unsound_collection_transmute.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use super::utils::is_layout_incompatible; | ||
use super::UNSOUND_COLLECTION_TRANSMUTE; | ||
use crate::utils::{match_def_path, paths, span_lint}; | ||
use rustc_hir::Expr; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty; | ||
use rustc_middle::ty::Ty; | ||
|
||
// used to check for UNSOUND_COLLECTION_TRANSMUTE | ||
static COLLECTIONS: &[&[&str]] = &[ | ||
&paths::VEC, | ||
&paths::VEC_DEQUE, | ||
&paths::BINARY_HEAP, | ||
&paths::BTREESET, | ||
&paths::BTREEMAP, | ||
&paths::HASHSET, | ||
&paths::HASHMAP, | ||
]; | ||
|
||
/// 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)) => { | ||
if from_adt.did != to_adt.did || !COLLECTIONS.iter().any(|path| match_def_path(cx, to_adt.did, path)) { | ||
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, | ||
} | ||
} |