Skip to content

Commit ece7fa4

Browse files
committed
Factor out match_any_diagnostic_items
1 parent 3771fe4 commit ece7fa4

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

clippy_lints/src/ptr.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
44
use clippy_utils::source::snippet_opt;
55
use clippy_utils::ty::expr_sig;
6-
use clippy_utils::{
7-
expr_path_res, get_expr_use_or_unification_node, is_lint_allowed, is_lint_allowed, match_any_diagnostic_items,
8-
path_def_id, path_to_local, paths, paths,
9-
};
6+
use clippy_utils::{get_expr_use_or_unification_node, is_lint_allowed, path_def_id, path_to_local, paths};
107
use if_chain::if_chain;
118
use rustc_errors::Applicability;
129
use rustc_hir::def_id::DefId;

clippy_lints/src/transmute/unsound_collection_transmute.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
use super::utils::is_layout_incompatible;
22
use super::UNSOUND_COLLECTION_TRANSMUTE;
33
use clippy_utils::diagnostics::span_lint;
4-
use clippy_utils::match_any_diagnostic_items;
54
use rustc_hir::Expr;
65
use rustc_lint::LateContext;
76
use rustc_middle::ty::{self, Ty};
8-
use rustc_span::symbol::{sym, Symbol};
9-
10-
// used to check for UNSOUND_COLLECTION_TRANSMUTE
11-
static COLLECTIONS: &[Symbol] = &[
12-
sym::Vec,
13-
sym::VecDeque,
14-
sym::BinaryHeap,
15-
sym::BTreeSet,
16-
sym::BTreeMap,
17-
sym::HashSet,
18-
sym::HashMap,
19-
];
7+
use rustc_span::symbol::sym;
208

219
/// Checks for `unsound_collection_transmute` lint.
2210
/// Returns `true` if it's triggered, otherwise returns `false`.
2311
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
2412
match (&from_ty.kind(), &to_ty.kind()) {
2513
(ty::Adt(from_adt, from_substs), ty::Adt(to_adt, to_substs)) => {
26-
if from_adt.did != to_adt.did || match_any_diagnostic_items(cx, to_adt.did, COLLECTIONS).is_none() {
14+
if from_adt.did != to_adt.did {
15+
return false;
16+
}
17+
if !matches!(
18+
cx.tcx.get_diagnostic_name(to_adt.did),
19+
Some(
20+
sym::BTreeMap
21+
| sym::BTreeSet
22+
| sym::BinaryHeap
23+
| sym::HashMap
24+
| sym::HashSet
25+
| sym::Vec
26+
| sym::VecDeque
27+
)
28+
) {
2729
return false;
2830
}
2931
if from_substs

clippy_utils/src/lib.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,22 +1615,14 @@ pub fn match_function_call<'tcx>(
16151615
/// Checks if the given `DefId` matches any of the paths. Returns the index of matching path, if
16161616
/// any.
16171617
///
1618-
/// Please use `match_any_diagnostic_items` if the targets are all diagnostic items.
1618+
/// Please use `tcx.get_diagnostic_name` if the targets are all diagnostic items.
16191619
pub fn match_any_def_paths(cx: &LateContext<'_>, did: DefId, paths: &[&[&str]]) -> Option<usize> {
16201620
let search_path = cx.get_def_path(did);
16211621
paths
16221622
.iter()
16231623
.position(|p| p.iter().map(|x| Symbol::intern(x)).eq(search_path.iter().copied()))
16241624
}
16251625

1626-
/// Checks if the given `DefId` matches any of provided diagnostic items. Returns the index of
1627-
/// matching path, if any.
1628-
pub fn match_any_diagnostic_items(cx: &LateContext<'_>, def_id: DefId, diag_items: &[Symbol]) -> Option<usize> {
1629-
diag_items
1630-
.iter()
1631-
.position(|item| cx.tcx.is_diagnostic_item(*item, def_id))
1632-
}
1633-
16341626
/// Checks if the given `DefId` matches the path.
16351627
pub fn match_def_path<'tcx>(cx: &LateContext<'tcx>, did: DefId, syms: &[&str]) -> bool {
16361628
// We should probably move to Symbols in Clippy as well rather than interning every time.

0 commit comments

Comments
 (0)