[Clippy] Swap iter_over_hash_type
to use diagnostic items instead of paths
This commit is contained in:
parent
43b8e04d46
commit
364e552940
5 changed files with 30 additions and 32 deletions
|
@ -981,8 +981,16 @@ symbols! {
|
||||||
half_open_range_patterns_in_slices,
|
half_open_range_patterns_in_slices,
|
||||||
hash,
|
hash,
|
||||||
hashmap_contains_key,
|
hashmap_contains_key,
|
||||||
|
hashmap_drain_ty,
|
||||||
hashmap_insert,
|
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,
|
||||||
|
hashset_iter_ty,
|
||||||
hexagon_target_feature,
|
hexagon_target_feature,
|
||||||
hidden,
|
hidden,
|
||||||
homogeneous_aggregate,
|
homogeneous_aggregate,
|
||||||
|
|
|
@ -1393,6 +1393,7 @@ where
|
||||||
/// let iter = map.iter();
|
/// let iter = map.iter();
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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> {
|
pub struct Iter<'a, K: 'a, V: 'a> {
|
||||||
base: base::Iter<'a, K, V>,
|
base: base::Iter<'a, K, V>,
|
||||||
}
|
}
|
||||||
|
@ -1431,6 +1432,7 @@ impl<K: Debug, V: Debug> fmt::Debug for Iter<'_, K, V> {
|
||||||
/// let iter = map.iter_mut();
|
/// let iter = map.iter_mut();
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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> {
|
pub struct IterMut<'a, K: 'a, V: 'a> {
|
||||||
base: base::IterMut<'a, K, V>,
|
base: base::IterMut<'a, K, V>,
|
||||||
}
|
}
|
||||||
|
@ -1491,6 +1493,7 @@ impl<K, V> IntoIter<K, V> {
|
||||||
/// let iter_keys = map.keys();
|
/// let iter_keys = map.keys();
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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> {
|
pub struct Keys<'a, K: 'a, V: 'a> {
|
||||||
inner: Iter<'a, K, V>,
|
inner: Iter<'a, K, V>,
|
||||||
}
|
}
|
||||||
|
@ -1529,6 +1532,7 @@ impl<K: Debug, V> fmt::Debug for Keys<'_, K, V> {
|
||||||
/// let iter_values = map.values();
|
/// let iter_values = map.values();
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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> {
|
pub struct Values<'a, K: 'a, V: 'a> {
|
||||||
inner: Iter<'a, K, V>,
|
inner: Iter<'a, K, V>,
|
||||||
}
|
}
|
||||||
|
@ -1567,6 +1571,7 @@ impl<K, V: Debug> fmt::Debug for Values<'_, K, V> {
|
||||||
/// let iter = map.drain();
|
/// let iter = map.drain();
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "drain", since = "1.6.0")]
|
#[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> {
|
pub struct Drain<'a, K: 'a, V: 'a> {
|
||||||
base: base::Drain<'a, K, V>,
|
base: base::Drain<'a, K, V>,
|
||||||
}
|
}
|
||||||
|
@ -1624,6 +1629,7 @@ where
|
||||||
/// let iter_values = map.values_mut();
|
/// let iter_values = map.values_mut();
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "map_values_mut", since = "1.10.0")]
|
#[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> {
|
pub struct ValuesMut<'a, K: 'a, V: 'a> {
|
||||||
inner: IterMut<'a, K, V>,
|
inner: IterMut<'a, K, V>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1271,6 +1271,7 @@ where
|
||||||
/// let mut iter = a.iter();
|
/// let mut iter = a.iter();
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_iter_ty")]
|
||||||
pub struct Iter<'a, K: 'a> {
|
pub struct Iter<'a, K: 'a> {
|
||||||
base: base::Iter<'a, K>,
|
base: base::Iter<'a, K>,
|
||||||
}
|
}
|
||||||
|
@ -1313,6 +1314,7 @@ pub struct IntoIter<K> {
|
||||||
/// let mut drain = a.drain();
|
/// let mut drain = a.drain();
|
||||||
/// ```
|
/// ```
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_drain_ty")]
|
||||||
pub struct Drain<'a, K: 'a> {
|
pub struct Drain<'a, K: 'a> {
|
||||||
base: base::Drain<'a, K>,
|
base: base::Drain<'a, K>,
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,5 @@
|
||||||
use clippy_utils::diagnostics::span_lint;
|
use clippy_utils::diagnostics::span_lint;
|
||||||
use clippy_utils::higher::ForLoop;
|
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 clippy_utils::ty::is_type_diagnostic_item;
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::declare_lint_pass;
|
use rustc_session::declare_lint_pass;
|
||||||
|
@ -44,28 +39,23 @@ declare_lint_pass!(IterOverHashType => [ITER_OVER_HASH_TYPE]);
|
||||||
|
|
||||||
impl LateLintPass<'_> for IterOverHashType {
|
impl LateLintPass<'_> for IterOverHashType {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) {
|
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)
|
if let Some(for_loop) = ForLoop::hir(expr)
|
||||||
&& !for_loop.body.span.from_expansion()
|
&& !for_loop.body.span.from_expansion()
|
||||||
&& let ty = cx.typeck_results().expr_ty(for_loop.arg).peel_refs()
|
&& let ty = cx.typeck_results().expr_ty(for_loop.arg).peel_refs()
|
||||||
&& let Some(adt) = ty.ty_adt_def()
|
&& hash_iter_tys.into_iter().any(|sym| is_type_diagnostic_item(cx, ty, sym))
|
||||||
&& 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))
|
|
||||||
{
|
{
|
||||||
span_lint(
|
span_lint(
|
||||||
cx,
|
cx,
|
||||||
|
|
|
@ -19,14 +19,6 @@ pub const FILE_OPTIONS: [&str; 4] = ["std", "fs", "File", "options"];
|
||||||
pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"];
|
pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"];
|
||||||
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
|
#[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 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: [&str; 3] = ["rustc_span", "symbol", "Ident"];
|
||||||
pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"];
|
pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"];
|
||||||
pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"];
|
pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue