[Clippy] Swap iter_over_hash_type to use diagnostic items instead of paths

This commit is contained in:
GnomedDev 2024-09-18 23:21:35 +01:00
parent 43b8e04d46
commit 364e552940
No known key found for this signature in database
GPG Key ID: 9BF10F8372B254D1
5 changed files with 30 additions and 32 deletions

View File

@ -981,8 +981,16 @@
half_open_range_patterns_in_slices,
hash,
hashmap_contains_key,
hashmap_drain_ty,
hashmap_insert,
hashmap_iter_mut_ty,
hashmap_iter_ty,
hashmap_keys_ty,
hashmap_values_mut_ty,
hashmap_values_ty,
hashset_drain_ty,
hashset_iter,
hashset_iter_ty,
hexagon_target_feature,
hidden,
homogeneous_aggregate,

View File

@ -1393,6 +1393,7 @@ fn index(&self, key: &Q) -> &V {
/// let iter = map.iter();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_iter_ty")]
pub struct Iter<'a, K: 'a, V: 'a> {
base: base::Iter<'a, K, V>,
}
@ -1431,6 +1432,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// let iter = map.iter_mut();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_iter_mut_ty")]
pub struct IterMut<'a, K: 'a, V: 'a> {
base: base::IterMut<'a, K, V>,
}
@ -1491,6 +1493,7 @@ pub(super) fn iter(&self) -> Iter<'_, K, V> {
/// let iter_keys = map.keys();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_keys_ty")]
pub struct Keys<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V>,
}
@ -1529,6 +1532,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// let iter_values = map.values();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_values_ty")]
pub struct Values<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V>,
}
@ -1567,6 +1571,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// let iter = map.drain();
/// ```
#[stable(feature = "drain", since = "1.6.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_drain_ty")]
pub struct Drain<'a, K: 'a, V: 'a> {
base: base::Drain<'a, K, V>,
}
@ -1624,6 +1629,7 @@ pub struct ExtractIf<'a, K, V, F>
/// let iter_values = map.values_mut();
/// ```
#[stable(feature = "map_values_mut", since = "1.10.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_values_mut_ty")]
pub struct ValuesMut<'a, K: 'a, V: 'a> {
inner: IterMut<'a, K, V>,
}

View File

@ -1271,6 +1271,7 @@ fn sub(self, rhs: &HashSet<T, S>) -> HashSet<T, S> {
/// let mut iter = a.iter();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_iter_ty")]
pub struct Iter<'a, K: 'a> {
base: base::Iter<'a, K>,
}
@ -1313,6 +1314,7 @@ pub struct IntoIter<K> {
/// let mut drain = a.drain();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_drain_ty")]
pub struct Drain<'a, K: 'a> {
base: base::Drain<'a, K>,
}

View File

@ -1,10 +1,5 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::higher::ForLoop;
use clippy_utils::match_any_def_paths;
use clippy_utils::paths::{
HASHMAP_DRAIN, HASHMAP_ITER, HASHMAP_ITER_MUT, HASHMAP_KEYS, HASHMAP_VALUES, HASHMAP_VALUES_MUT, HASHSET_DRAIN,
HASHSET_ITER_TY,
};
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
@ -44,28 +39,23 @@
impl LateLintPass<'_> for IterOverHashType {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) {
let hash_iter_tys = [
sym::HashMap,
sym::HashSet,
sym::hashmap_keys_ty,
sym::hashmap_values_ty,
sym::hashmap_values_mut_ty,
sym::hashmap_iter_ty,
sym::hashmap_iter_mut_ty,
sym::hashmap_drain_ty,
sym::hashset_iter_ty,
sym::hashset_drain_ty,
];
if let Some(for_loop) = ForLoop::hir(expr)
&& !for_loop.body.span.from_expansion()
&& let ty = cx.typeck_results().expr_ty(for_loop.arg).peel_refs()
&& let Some(adt) = ty.ty_adt_def()
&& let did = adt.did()
&& (match_any_def_paths(
cx,
did,
&[
&HASHMAP_KEYS,
&HASHMAP_VALUES,
&HASHMAP_VALUES_MUT,
&HASHMAP_ITER,
&HASHMAP_ITER_MUT,
&HASHMAP_DRAIN,
&HASHSET_ITER_TY,
&HASHSET_DRAIN,
],
)
.is_some()
|| is_type_diagnostic_item(cx, ty, sym::HashMap)
|| is_type_diagnostic_item(cx, ty, sym::HashSet))
&& hash_iter_tys.into_iter().any(|sym| is_type_diagnostic_item(cx, ty, sym))
{
span_lint(
cx,

View File

@ -19,14 +19,6 @@
pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"];
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
pub const FUTURES_IO_ASYNCWRITEEXT: [&str; 3] = ["futures_util", "io", "AsyncWriteExt"];
pub const HASHMAP_ITER: [&str; 5] = ["std", "collections", "hash", "map", "Iter"];
pub const HASHMAP_ITER_MUT: [&str; 5] = ["std", "collections", "hash", "map", "IterMut"];
pub const HASHMAP_KEYS: [&str; 5] = ["std", "collections", "hash", "map", "Keys"];
pub const HASHMAP_VALUES: [&str; 5] = ["std", "collections", "hash", "map", "Values"];
pub const HASHMAP_DRAIN: [&str; 5] = ["std", "collections", "hash", "map", "Drain"];
pub const HASHMAP_VALUES_MUT: [&str; 5] = ["std", "collections", "hash", "map", "ValuesMut"];
pub const HASHSET_ITER_TY: [&str; 5] = ["std", "collections", "hash", "set", "Iter"];
pub const HASHSET_DRAIN: [&str; 5] = ["std", "collections", "hash", "set", "Drain"];
pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"];
pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"];
pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"];