Factor out match_any_diagnostic_items

This commit is contained in:
Cameron Steffen 2022-01-06 09:39:46 -06:00
parent 3771fe4ade
commit ece7fa4f9c
3 changed files with 18 additions and 27 deletions

View File

@ -3,10 +3,7 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::expr_sig;
use clippy_utils::{
expr_path_res, get_expr_use_or_unification_node, is_lint_allowed, is_lint_allowed, match_any_diagnostic_items,
path_def_id, path_to_local, paths, paths,
};
use clippy_utils::{get_expr_use_or_unification_node, is_lint_allowed, path_def_id, path_to_local, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::def_id::DefId;

View File

@ -1,29 +1,31 @@
use super::utils::is_layout_incompatible;
use super::UNSOUND_COLLECTION_TRANSMUTE;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::match_any_diagnostic_items;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty};
use rustc_span::symbol::{sym, Symbol};
// used to check for UNSOUND_COLLECTION_TRANSMUTE
static COLLECTIONS: &[Symbol] = &[
sym::Vec,
sym::VecDeque,
sym::BinaryHeap,
sym::BTreeSet,
sym::BTreeMap,
sym::HashSet,
sym::HashMap,
];
use rustc_span::symbol::sym;
/// 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 || match_any_diagnostic_items(cx, to_adt.did, COLLECTIONS).is_none() {
if from_adt.did != to_adt.did {
return false;
}
if !matches!(
cx.tcx.get_diagnostic_name(to_adt.did),
Some(
sym::BTreeMap
| sym::BTreeSet
| sym::BinaryHeap
| sym::HashMap
| sym::HashSet
| sym::Vec
| sym::VecDeque
)
) {
return false;
}
if from_substs

View File

@ -1615,7 +1615,7 @@ pub fn match_function_call<'tcx>(
/// Checks if the given `DefId` matches any of the paths. Returns the index of matching path, if
/// any.
///
/// Please use `match_any_diagnostic_items` if the targets are all diagnostic items.
/// Please use `tcx.get_diagnostic_name` if the targets are all diagnostic items.
pub fn match_any_def_paths(cx: &LateContext<'_>, did: DefId, paths: &[&[&str]]) -> Option<usize> {
let search_path = cx.get_def_path(did);
paths
@ -1623,14 +1623,6 @@ pub fn match_any_def_paths(cx: &LateContext<'_>, did: DefId, paths: &[&[&str]])
.position(|p| p.iter().map(|x| Symbol::intern(x)).eq(search_path.iter().copied()))
}
/// Checks if the given `DefId` matches any of provided diagnostic items. Returns the index of
/// matching path, if any.
pub fn match_any_diagnostic_items(cx: &LateContext<'_>, def_id: DefId, diag_items: &[Symbol]) -> Option<usize> {
diag_items
.iter()
.position(|item| cx.tcx.is_diagnostic_item(*item, def_id))
}
/// Checks if the given `DefId` matches the path.
pub fn match_def_path<'tcx>(cx: &LateContext<'tcx>, did: DefId, syms: &[&str]) -> bool {
// We should probably move to Symbols in Clippy as well rather than interning every time.