Skip to content

Commit ecf85f4

Browse files
committed
Use diagnostic items for Vec, VecDeque and connected refactorings
1 parent 6ce6b29 commit ecf85f4

File tree

6 files changed

+30
-42
lines changed

6 files changed

+30
-42
lines changed

clippy_lints/src/infinite_iter.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::ty::{implements_trait, match_type};
2+
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
33
use clippy_utils::{get_trait_def_id, higher, is_qpath_def_path, paths};
44
use rustc_hir::{BorrowKind, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::symbol::{sym, Symbol};
78

89
declare_clippy_lint! {
910
/// **What it does:** Checks for iteration that is guaranteed to be infinite.
@@ -202,15 +203,15 @@ const COMPLETING_METHODS: [(&str, usize); 12] = [
202203
];
203204

204205
/// the paths of types that are known to be infinitely allocating
205-
const INFINITE_COLLECTORS: [&[&str]; 8] = [
206-
&paths::BINARY_HEAP,
207-
&paths::BTREEMAP,
208-
&paths::BTREESET,
209-
&paths::HASHMAP,
210-
&paths::HASHSET,
211-
&paths::LINKED_LIST,
212-
&paths::VEC,
213-
&paths::VEC_DEQUE,
206+
const INFINITE_COLLECTORS: &[Symbol] = &[
207+
sym::BinaryHeap,
208+
sym::BTreeMap,
209+
sym::BTreeSet,
210+
sym::hashmap_type,
211+
sym::hashset_type,
212+
sym::LinkedList,
213+
sym::vec_type,
214+
sym::vecdeque_type,
214215
];
215216

216217
fn complete_infinite_iter(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
@@ -235,7 +236,10 @@ fn complete_infinite_iter(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
235236
}
236237
} else if method.ident.name == sym!(collect) {
237238
let ty = cx.typeck_results().expr_ty(expr);
238-
if INFINITE_COLLECTORS.iter().any(|path| match_type(cx, ty, path)) {
239+
if INFINITE_COLLECTORS
240+
.iter()
241+
.any(|diag_item| is_type_diagnostic_item(cx, ty, *diag_item))
242+
{
239243
return is_infinite(cx, &args[0]);
240244
}
241245
}

clippy_lints/src/size_of_in_element_count.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn get_pointee_ty_and_count_expr(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -
6767
const FUNCTIONS: [&[&str]; 8] = [
6868
&paths::PTR_COPY_NONOVERLAPPING,
6969
&paths::PTR_COPY,
70-
&paths::WRITE_BYTES,
70+
&paths::PTR_WRITE_BYTES,
7171
&paths::PTR_SWAP_NONOVERLAPPING,
7272
&paths::PTR_SLICE_FROM_RAW_PARTS,
7373
&paths::PTR_SLICE_FROM_RAW_PARTS_MUT,

clippy_lints/src/transmute/unsound_collection_transmute.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
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_def_path, paths};
4+
use clippy_utils::match_any_diagnostic_items;
55
use rustc_hir::Expr;
66
use rustc_lint::LateContext;
77
use rustc_middle::ty::{self, Ty};
8+
use rustc_span::symbol::{sym, Symbol};
89

910
// used to check for UNSOUND_COLLECTION_TRANSMUTE
10-
static COLLECTIONS: &[&[&str]] = &[
11-
&paths::VEC,
12-
&paths::VEC_DEQUE,
13-
&paths::BINARY_HEAP,
14-
&paths::BTREESET,
15-
&paths::BTREEMAP,
16-
&paths::HASHSET,
17-
&paths::HASHMAP,
11+
static COLLECTIONS: &[Symbol] = &[
12+
sym::vec_type,
13+
sym::vecdeque_type,
14+
sym::BinaryHeap,
15+
sym::BTreeSet,
16+
sym::BTreeMap,
17+
sym::hashset_type,
18+
sym::hashmap_type,
1819
];
1920

2021
/// Checks for `unsound_collection_transmute` lint.
2122
/// Returns `true` if it's triggered, otherwise returns `false`.
2223
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
2324
match (&from_ty.kind(), &to_ty.kind()) {
2425
(ty::Adt(from_adt, from_substs), ty::Adt(to_adt, to_substs)) => {
25-
if from_adt.did != to_adt.did || !COLLECTIONS.iter().any(|path| match_def_path(cx, to_adt.did, path)) {
26+
if from_adt.did != to_adt.did || match_any_diagnostic_items(cx, to_adt.did, COLLECTIONS).is_none() {
2627
return false;
2728
}
2829
if from_substs

clippy_utils/src/paths.rs

-16
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,11 @@ pub const ASMUT_TRAIT: [&str; 3] = ["core", "convert", "AsMut"];
2121
pub const ASREF_TRAIT: [&str; 3] = ["core", "convert", "AsRef"];
2222
pub(super) const BEGIN_PANIC: [&str; 3] = ["std", "panicking", "begin_panic"];
2323
pub(super) const BEGIN_PANIC_FMT: [&str; 3] = ["std", "panicking", "begin_panic_fmt"];
24-
/// Preferably use the diagnostic item `sym::BinaryHeap` where possible
25-
pub const BINARY_HEAP: [&str; 4] = ["alloc", "collections", "binary_heap", "BinaryHeap"];
2624
/// Preferably use the diagnostic item `sym::Borrow` where possible
2725
pub const BORROW_TRAIT: [&str; 3] = ["core", "borrow", "Borrow"];
28-
/// Preferably use the diagnostic item `sym::BTreeMap` where possible
29-
pub const BTREEMAP: [&str; 5] = ["alloc", "collections", "btree", "map", "BTreeMap"];
3026
pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"];
3127
pub const BTREEMAP_ENTRY: [&str; 6] = ["alloc", "collections", "btree", "map", "entry", "Entry"];
3228
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
33-
/// Preferably use the diagnostic item `sym::BTreeSet` where possible
34-
pub const BTREESET: [&str; 5] = ["alloc", "collections", "btree", "set", "BTreeSet"];
3529
pub const CLONE_TRAIT_METHOD: [&str; 4] = ["core", "clone", "Clone", "clone"];
3630
pub const CMP_MAX: [&str; 3] = ["core", "cmp", "max"];
3731
pub const CMP_MIN: [&str; 3] = ["core", "cmp", "min"];
@@ -59,13 +53,9 @@ pub const FROM_ITERATOR_METHOD: [&str; 6] = ["core", "iter", "traits", "collect"
5953
pub const FROM_STR_METHOD: [&str; 5] = ["core", "str", "traits", "FromStr", "from_str"];
6054
pub const FUTURE_FROM_GENERATOR: [&str; 3] = ["core", "future", "from_generator"];
6155
pub const HASH: [&str; 3] = ["core", "hash", "Hash"];
62-
/// Preferably use the diagnostic item `sym::hashmap_type` where possible
63-
pub const HASHMAP: [&str; 5] = ["std", "collections", "hash", "map", "HashMap"];
6456
pub const HASHMAP_CONTAINS_KEY: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "contains_key"];
6557
pub const HASHMAP_ENTRY: [&str; 5] = ["std", "collections", "hash", "map", "Entry"];
6658
pub const HASHMAP_INSERT: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "insert"];
67-
/// Preferably use the diagnostic item `sym::hashset_type` where possible
68-
pub const HASHSET: [&str; 5] = ["std", "collections", "hash", "set", "HashSet"];
6959
#[cfg(feature = "internal-lints")]
7060
pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"];
7161
#[cfg(feature = "internal-lints")]
@@ -83,8 +73,6 @@ pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"];
8373
#[cfg(feature = "internal-lints")]
8474
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
8575
pub const LIBC_STRLEN: [&str; 2] = ["libc", "strlen"];
86-
/// Preferably use the diagnostic item `sym::LinkedList` where possible
87-
pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"];
8876
#[cfg(any(feature = "internal-lints", feature = "metadata-collector-lint"))]
8977
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];
9078
pub const MEM_DISCRIMINANT: [&str; 3] = ["core", "mem", "discriminant"];
@@ -182,14 +170,10 @@ pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"]
182170
pub const TO_OWNED_METHOD: [&str; 4] = ["alloc", "borrow", "ToOwned", "to_owned"];
183171
pub const TO_STRING_METHOD: [&str; 4] = ["alloc", "string", "ToString", "to_string"];
184172
pub const TRY_FROM: [&str; 4] = ["core", "convert", "TryFrom", "try_from"];
185-
186-
pub const VEC: [&str; 3] = ["alloc", "vec", "Vec"];
187173
pub const VEC_AS_MUT_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_mut_slice"];
188174
pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"];
189-
pub const VEC_DEQUE: [&str; 4] = ["alloc", "collections", "vec_deque", "VecDeque"];
190175
pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"];
191176
pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
192177
pub const VEC_RESIZE: [&str; 4] = ["alloc", "vec", "Vec", "resize"];
193178
pub const WEAK_ARC: [&str; 3] = ["alloc", "sync", "Weak"];
194179
pub const WEAK_RC: [&str; 3] = ["alloc", "rc", "Weak"];
195-
pub const WRITE_BYTES: [&str; 3] = ["core", "intrinsics", "write_bytes"];

tests/ui-internal/match_type_on_diag_item.rs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ impl<'tcx> LateLintPass<'tcx> for Pass {
2727
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr) {
2828
let ty = cx.typeck_results().expr_ty(expr);
2929

30-
let _ = match_type(cx, ty, &paths::VEC); // FIXME: Doesn't lint external paths
3130
let _ = match_type(cx, ty, &OPTION);
3231
let _ = match_type(cx, ty, &["core", "result", "Result"]);
3332

tests/ui-internal/match_type_on_diag_item.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: usage of `clippy_utils::ty::match_type()` on a type diagnostic item
2-
--> $DIR/match_type_on_diag_item.rs:31:17
2+
--> $DIR/match_type_on_diag_item.rs:30:17
33
|
44
LL | let _ = match_type(cx, ty, &OPTION);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `clippy_utils::ty::is_type_diagnostic_item(cx, ty, sym::option_type)`
@@ -12,13 +12,13 @@ LL | #![deny(clippy::internal)]
1212
= note: `#[deny(clippy::match_type_on_diagnostic_item)]` implied by `#[deny(clippy::internal)]`
1313

1414
error: usage of `clippy_utils::ty::match_type()` on a type diagnostic item
15-
--> $DIR/match_type_on_diag_item.rs:32:17
15+
--> $DIR/match_type_on_diag_item.rs:31:17
1616
|
1717
LL | let _ = match_type(cx, ty, &["core", "result", "Result"]);
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `clippy_utils::ty::is_type_diagnostic_item(cx, ty, sym::result_type)`
1919

2020
error: usage of `clippy_utils::ty::match_type()` on a type diagnostic item
21-
--> $DIR/match_type_on_diag_item.rs:35:17
21+
--> $DIR/match_type_on_diag_item.rs:34:17
2222
|
2323
LL | let _ = clippy_utils::ty::match_type(cx, ty, rc_path);
2424
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `clippy_utils::ty::is_type_diagnostic_item(cx, ty, sym::Rc)`

0 commit comments

Comments
 (0)