Skip to content

Commit 364e552

Browse files
committed
[Clippy] Swap iter_over_hash_type to use diagnostic items instead of paths
1 parent 43b8e04 commit 364e552

File tree

5 files changed

+30
-32
lines changed

5 files changed

+30
-32
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,8 +981,16 @@ symbols! {
981981
half_open_range_patterns_in_slices,
982982
hash,
983983
hashmap_contains_key,
984+
hashmap_drain_ty,
984985
hashmap_insert,
986+
hashmap_iter_mut_ty,
987+
hashmap_iter_ty,
988+
hashmap_keys_ty,
989+
hashmap_values_mut_ty,
990+
hashmap_values_ty,
991+
hashset_drain_ty,
985992
hashset_iter,
993+
hashset_iter_ty,
986994
hexagon_target_feature,
987995
hidden,
988996
homogeneous_aggregate,

library/std/src/collections/hash/map.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,7 @@ where
13931393
/// let iter = map.iter();
13941394
/// ```
13951395
#[stable(feature = "rust1", since = "1.0.0")]
1396+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_iter_ty")]
13961397
pub struct Iter<'a, K: 'a, V: 'a> {
13971398
base: base::Iter<'a, K, V>,
13981399
}
@@ -1431,6 +1432,7 @@ impl<K: Debug, V: Debug> fmt::Debug for Iter<'_, K, V> {
14311432
/// let iter = map.iter_mut();
14321433
/// ```
14331434
#[stable(feature = "rust1", since = "1.0.0")]
1435+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_iter_mut_ty")]
14341436
pub struct IterMut<'a, K: 'a, V: 'a> {
14351437
base: base::IterMut<'a, K, V>,
14361438
}
@@ -1491,6 +1493,7 @@ impl<K, V> IntoIter<K, V> {
14911493
/// let iter_keys = map.keys();
14921494
/// ```
14931495
#[stable(feature = "rust1", since = "1.0.0")]
1496+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_keys_ty")]
14941497
pub struct Keys<'a, K: 'a, V: 'a> {
14951498
inner: Iter<'a, K, V>,
14961499
}
@@ -1529,6 +1532,7 @@ impl<K: Debug, V> fmt::Debug for Keys<'_, K, V> {
15291532
/// let iter_values = map.values();
15301533
/// ```
15311534
#[stable(feature = "rust1", since = "1.0.0")]
1535+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_values_ty")]
15321536
pub struct Values<'a, K: 'a, V: 'a> {
15331537
inner: Iter<'a, K, V>,
15341538
}
@@ -1567,6 +1571,7 @@ impl<K, V: Debug> fmt::Debug for Values<'_, K, V> {
15671571
/// let iter = map.drain();
15681572
/// ```
15691573
#[stable(feature = "drain", since = "1.6.0")]
1574+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_drain_ty")]
15701575
pub struct Drain<'a, K: 'a, V: 'a> {
15711576
base: base::Drain<'a, K, V>,
15721577
}
@@ -1624,6 +1629,7 @@ where
16241629
/// let iter_values = map.values_mut();
16251630
/// ```
16261631
#[stable(feature = "map_values_mut", since = "1.10.0")]
1632+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_values_mut_ty")]
16271633
pub struct ValuesMut<'a, K: 'a, V: 'a> {
16281634
inner: IterMut<'a, K, V>,
16291635
}

library/std/src/collections/hash/set.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,7 @@ where
12711271
/// let mut iter = a.iter();
12721272
/// ```
12731273
#[stable(feature = "rust1", since = "1.0.0")]
1274+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_iter_ty")]
12741275
pub struct Iter<'a, K: 'a> {
12751276
base: base::Iter<'a, K>,
12761277
}
@@ -1313,6 +1314,7 @@ pub struct IntoIter<K> {
13131314
/// let mut drain = a.drain();
13141315
/// ```
13151316
#[stable(feature = "rust1", since = "1.0.0")]
1317+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_drain_ty")]
13161318
pub struct Drain<'a, K: 'a> {
13171319
base: base::Drain<'a, K>,
13181320
}

src/tools/clippy/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,

src/tools/clippy/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)