Use diagnostic items in into_iter_collections

This commit is contained in:
Cameron Steffen 2021-02-26 21:17:29 -06:00
parent 2b3a731e1c
commit 7984e60d9e
3 changed files with 22 additions and 24 deletions

View file

@ -24,7 +24,7 @@ use rustc_middle::ty::{self, TraitRef, Ty, TyS};
use rustc_semver::RustcVersion; use rustc_semver::RustcVersion;
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::symbol::{sym, SymbolStr}; use rustc_span::symbol::{sym, Symbol, SymbolStr};
use rustc_typeck::hir_ty_to_ty; use rustc_typeck::hir_ty_to_ty;
use crate::consts::{constant, Constant}; use crate::consts::{constant, Constant};
@ -3619,7 +3619,7 @@ fn lint_asref(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_re
} }
} }
fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(&'static str, &'static str)> { fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(Symbol, &'static str)> {
has_iter_method(cx, self_ref_ty).map(|ty_name| { has_iter_method(cx, self_ref_ty).map(|ty_name| {
let mutbl = match self_ref_ty.kind() { let mutbl = match self_ref_ty.kind() {
ty::Ref(_, _, mutbl) => mutbl, ty::Ref(_, _, mutbl) => mutbl,

View file

@ -1295,24 +1295,24 @@ pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_>, node: HirId) -> bool
} }
/// Returns true if ty has `iter` or `iter_mut` methods /// Returns true if ty has `iter` or `iter_mut` methods
pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<&'static str> { pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<Symbol> {
// FIXME: instead of this hard-coded list, we should check if `<adt>::iter` // FIXME: instead of this hard-coded list, we should check if `<adt>::iter`
// exists and has the desired signature. Unfortunately FnCtxt is not exported // exists and has the desired signature. Unfortunately FnCtxt is not exported
// so we can't use its `lookup_method` method. // so we can't use its `lookup_method` method.
let into_iter_collections: [&[&str]; 13] = [ let into_iter_collections: &[Symbol] = &[
&paths::VEC, sym::vec_type,
&paths::OPTION, sym::option_type,
&paths::RESULT, sym::result_type,
&paths::BTREESET, sym::BTreeMap,
&paths::BTREEMAP, sym::BTreeSet,
&paths::VEC_DEQUE, sym::vecdeque_type,
&paths::LINKED_LIST, sym::LinkedList,
&paths::BINARY_HEAP, sym::BinaryHeap,
&paths::HASHSET, sym::hashset_type,
&paths::HASHMAP, sym::hashmap_type,
&paths::PATH_BUF, sym::PathBuf,
&paths::PATH, sym::Path,
&paths::RECEIVER, sym::Receiver,
]; ];
let ty_to_check = match probably_ref_ty.kind() { 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<
}; };
let def_id = match ty_to_check.kind() { let def_id = match ty_to_check.kind() {
ty::Array(..) => return Some("array"), ty::Array(..) => return Some(sym::array),
ty::Slice(..) => return Some("slice"), ty::Slice(..) => return Some(sym::slice),
ty::Adt(adt, _) => adt.did, ty::Adt(adt, _) => adt.did,
_ => return None, _ => return None,
}; };
for path in &into_iter_collections { for &name in into_iter_collections {
if match_def_path(cx, def_id, path) { if cx.tcx.is_diagnostic_item(name, def_id) {
return Some(*path.last().unwrap()); return Some(cx.tcx.item_name(def_id));
} }
} }
None None

View file

@ -99,7 +99,6 @@ pub(super) const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"];
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"]; pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"]; pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"]; pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];
pub const PATH: [&str; 3] = ["std", "path", "Path"];
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"]; pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"]; pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"]; 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"];
pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"]; pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"];
pub const RC: [&str; 3] = ["alloc", "rc", "Rc"]; pub const RC: [&str; 3] = ["alloc", "rc", "Rc"];
pub const RC_PTR_EQ: [&str; 4] = ["alloc", "rc", "Rc", "ptr_eq"]; pub const RC_PTR_EQ: [&str; 4] = ["alloc", "rc", "Rc", "ptr_eq"];
pub const RECEIVER: [&str; 4] = ["std", "sync", "mpsc", "Receiver"];
pub const REFCELL_REF: [&str; 3] = ["core", "cell", "Ref"]; pub const REFCELL_REF: [&str; 3] = ["core", "cell", "Ref"];
pub const REFCELL_REFMUT: [&str; 3] = ["core", "cell", "RefMut"]; pub const REFCELL_REFMUT: [&str; 3] = ["core", "cell", "RefMut"];
pub const REGEX_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "unicode", "RegexBuilder", "new"]; pub const REGEX_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "unicode", "RegexBuilder", "new"];