Skip to content

Commit 7984e60

Browse files
committed
Use diagnostic items in into_iter_collections
1 parent 2b3a731 commit 7984e60

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed

clippy_lints/src/methods/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_middle::ty::{self, TraitRef, Ty, TyS};
2424
use rustc_semver::RustcVersion;
2525
use rustc_session::{declare_tool_lint, impl_lint_pass};
2626
use rustc_span::source_map::Span;
27-
use rustc_span::symbol::{sym, SymbolStr};
27+
use rustc_span::symbol::{sym, Symbol, SymbolStr};
2828
use rustc_typeck::hir_ty_to_ty;
2929

3030
use crate::consts::{constant, Constant};
@@ -3619,7 +3619,7 @@ fn lint_asref(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_re
36193619
}
36203620
}
36213621

3622-
fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(&'static str, &'static str)> {
3622+
fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(Symbol, &'static str)> {
36233623
has_iter_method(cx, self_ref_ty).map(|ty_name| {
36243624
let mutbl = match self_ref_ty.kind() {
36253625
ty::Ref(_, _, mutbl) => mutbl,

clippy_utils/src/lib.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -1295,24 +1295,24 @@ pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_>, node: HirId) -> bool
12951295
}
12961296

12971297
/// Returns true if ty has `iter` or `iter_mut` methods
1298-
pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<&'static str> {
1298+
pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<Symbol> {
12991299
// FIXME: instead of this hard-coded list, we should check if `<adt>::iter`
13001300
// exists and has the desired signature. Unfortunately FnCtxt is not exported
13011301
// so we can't use its `lookup_method` method.
1302-
let into_iter_collections: [&[&str]; 13] = [
1303-
&paths::VEC,
1304-
&paths::OPTION,
1305-
&paths::RESULT,
1306-
&paths::BTREESET,
1307-
&paths::BTREEMAP,
1308-
&paths::VEC_DEQUE,
1309-
&paths::LINKED_LIST,
1310-
&paths::BINARY_HEAP,
1311-
&paths::HASHSET,
1312-
&paths::HASHMAP,
1313-
&paths::PATH_BUF,
1314-
&paths::PATH,
1315-
&paths::RECEIVER,
1302+
let into_iter_collections: &[Symbol] = &[
1303+
sym::vec_type,
1304+
sym::option_type,
1305+
sym::result_type,
1306+
sym::BTreeMap,
1307+
sym::BTreeSet,
1308+
sym::vecdeque_type,
1309+
sym::LinkedList,
1310+
sym::BinaryHeap,
1311+
sym::hashset_type,
1312+
sym::hashmap_type,
1313+
sym::PathBuf,
1314+
sym::Path,
1315+
sym::Receiver,
13161316
];
13171317

13181318
let ty_to_check = match probably_ref_ty.kind() {
@@ -1321,15 +1321,15 @@ pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<
13211321
};
13221322

13231323
let def_id = match ty_to_check.kind() {
1324-
ty::Array(..) => return Some("array"),
1325-
ty::Slice(..) => return Some("slice"),
1324+
ty::Array(..) => return Some(sym::array),
1325+
ty::Slice(..) => return Some(sym::slice),
13261326
ty::Adt(adt, _) => adt.did,
13271327
_ => return None,
13281328
};
13291329

1330-
for path in &into_iter_collections {
1331-
if match_def_path(cx, def_id, path) {
1332-
return Some(*path.last().unwrap());
1330+
for &name in into_iter_collections {
1331+
if cx.tcx.is_diagnostic_item(name, def_id) {
1332+
return Some(cx.tcx.item_name(def_id));
13331333
}
13341334
}
13351335
None

clippy_utils/src/paths.rs

-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ pub(super) const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"];
9999
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
100100
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
101101
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];
102-
pub const PATH: [&str; 3] = ["std", "path", "Path"];
103102
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
104103
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
105104
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];
@@ -116,7 +115,6 @@ pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"];
116115
pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"];
117116
pub const RC: [&str; 3] = ["alloc", "rc", "Rc"];
118117
pub const RC_PTR_EQ: [&str; 4] = ["alloc", "rc", "Rc", "ptr_eq"];
119-
pub const RECEIVER: [&str; 4] = ["std", "sync", "mpsc", "Receiver"];
120118
pub const REFCELL_REF: [&str; 3] = ["core", "cell", "Ref"];
121119
pub const REFCELL_REFMUT: [&str; 3] = ["core", "cell", "RefMut"];
122120
pub const REGEX_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "unicode", "RegexBuilder", "new"];

0 commit comments

Comments
 (0)