Skip to content

Commit 917775f

Browse files
committed
[Clippy] Swap iter_over_hash_type to use diagnostic items instead of paths
1 parent 37e3832 commit 917775f

File tree

2 files changed

+14
-32
lines changed

2 files changed

+14
-32
lines changed

clippy_lints/src/iter_over_hash_type.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::higher::ForLoop;
3-
use clippy_utils::match_any_def_paths;
4-
use clippy_utils::paths::{
5-
HASHMAP_DRAIN, HASHMAP_ITER, HASHMAP_ITER_MUT, HASHMAP_KEYS, HASHMAP_VALUES, HASHMAP_VALUES_MUT, HASHSET_DRAIN,
6-
HASHSET_ITER_TY,
7-
};
83
use clippy_utils::ty::is_type_diagnostic_item;
94
use rustc_lint::{LateContext, LateLintPass};
105
use rustc_session::declare_lint_pass;
@@ -44,28 +39,23 @@ declare_lint_pass!(IterOverHashType => [ITER_OVER_HASH_TYPE]);
4439

4540
impl LateLintPass<'_> for IterOverHashType {
4641
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) {
42+
let hash_iter_tys = [
43+
sym::HashMap,
44+
sym::HashSet,
45+
sym::hashmap_keys_ty,
46+
sym::hashmap_values_ty,
47+
sym::hashmap_values_mut_ty,
48+
sym::hashmap_iter_ty,
49+
sym::hashmap_iter_mut_ty,
50+
sym::hashmap_drain_ty,
51+
sym::hashset_iter_ty,
52+
sym::hashset_drain_ty,
53+
];
54+
4755
if let Some(for_loop) = ForLoop::hir(expr)
4856
&& !for_loop.body.span.from_expansion()
4957
&& let ty = cx.typeck_results().expr_ty(for_loop.arg).peel_refs()
50-
&& let Some(adt) = ty.ty_adt_def()
51-
&& let did = adt.did()
52-
&& (match_any_def_paths(
53-
cx,
54-
did,
55-
&[
56-
&HASHMAP_KEYS,
57-
&HASHMAP_VALUES,
58-
&HASHMAP_VALUES_MUT,
59-
&HASHMAP_ITER,
60-
&HASHMAP_ITER_MUT,
61-
&HASHMAP_DRAIN,
62-
&HASHSET_ITER_TY,
63-
&HASHSET_DRAIN,
64-
],
65-
)
66-
.is_some()
67-
|| is_type_diagnostic_item(cx, ty, sym::HashMap)
68-
|| is_type_diagnostic_item(cx, ty, sym::HashSet))
58+
&& hash_iter_tys.into_iter().any(|sym| is_type_diagnostic_item(cx, ty, sym))
6959
{
7060
span_lint(
7161
cx,

clippy_utils/src/paths.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ pub const FILE_OPTIONS: [&str; 4] = ["std", "fs", "File", "options"];
1919
pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"];
2020
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
2121
pub const FUTURES_IO_ASYNCWRITEEXT: [&str; 3] = ["futures_util", "io", "AsyncWriteExt"];
22-
pub const HASHMAP_ITER: [&str; 5] = ["std", "collections", "hash", "map", "Iter"];
23-
pub const HASHMAP_ITER_MUT: [&str; 5] = ["std", "collections", "hash", "map", "IterMut"];
24-
pub const HASHMAP_KEYS: [&str; 5] = ["std", "collections", "hash", "map", "Keys"];
25-
pub const HASHMAP_VALUES: [&str; 5] = ["std", "collections", "hash", "map", "Values"];
26-
pub const HASHMAP_DRAIN: [&str; 5] = ["std", "collections", "hash", "map", "Drain"];
27-
pub const HASHMAP_VALUES_MUT: [&str; 5] = ["std", "collections", "hash", "map", "ValuesMut"];
28-
pub const HASHSET_ITER_TY: [&str; 5] = ["std", "collections", "hash", "set", "Iter"];
29-
pub const HASHSET_DRAIN: [&str; 5] = ["std", "collections", "hash", "set", "Drain"];
3022
pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"];
3123
pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"];
3224
pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"];

0 commit comments

Comments
 (0)