|
1 | 1 | use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then}; |
2 | 2 | use clippy_utils::source::snippet_with_applicability; |
3 | | -use clippy_utils::{def_path_res, is_lint_allowed, match_any_def_paths, peel_hir_expr_refs}; |
| 3 | +use clippy_utils::{def_path_def_ids, is_lint_allowed, match_any_def_paths, peel_hir_expr_refs}; |
4 | 4 | use if_chain::if_chain; |
5 | 5 | use rustc_ast::ast::LitKind; |
6 | 6 | use rustc_data_structures::fx::FxHashSet; |
7 | 7 | use rustc_errors::Applicability; |
8 | 8 | use rustc_hir as hir; |
9 | | -use rustc_hir::def::{DefKind, Namespace, Res}; |
| 9 | +use rustc_hir::def::{DefKind, Res}; |
10 | 10 | use rustc_hir::def_id::DefId; |
11 | 11 | use rustc_hir::{Expr, ExprKind, Local, Mutability, Node}; |
12 | 12 | use rustc_lint::{LateContext, LateLintPass}; |
13 | 13 | use rustc_middle::mir::interpret::{Allocation, ConstValue, GlobalAlloc}; |
14 | | -use rustc_middle::ty::{self, AssocKind, DefIdTree, Ty}; |
| 14 | +use rustc_middle::ty::{self, DefIdTree, Ty}; |
15 | 15 | use rustc_session::{declare_tool_lint, impl_lint_pass}; |
16 | | -use rustc_span::symbol::{Ident, Symbol}; |
| 16 | +use rustc_span::symbol::Symbol; |
17 | 17 | use rustc_span::Span; |
18 | 18 |
|
19 | 19 | use std::str; |
@@ -110,7 +110,7 @@ impl UnnecessaryDefPath { |
110 | 110 | // Extract the path to the matched type |
111 | 111 | if let Some(segments) = path_to_matched_type(cx, item_arg); |
112 | 112 | let segments: Vec<&str> = segments.iter().map(|sym| &**sym).collect(); |
113 | | - if let Some(def_id) = inherent_def_path_res(cx, &segments[..]); |
| 113 | + if let Some(def_id) = def_path_def_ids(cx, &segments[..]).next(); |
114 | 114 | then { |
115 | 115 | // Check if the target item is a diagnostic item or LangItem. |
116 | 116 | #[rustfmt::skip] |
@@ -209,7 +209,7 @@ impl UnnecessaryDefPath { |
209 | 209 | fn check_array(&mut self, cx: &LateContext<'_>, elements: &[Expr<'_>], span: Span) { |
210 | 210 | let Some(path) = path_from_array(elements) else { return }; |
211 | 211 |
|
212 | | - if let Some(def_id) = inherent_def_path_res(cx, &path.iter().map(AsRef::as_ref).collect::<Vec<_>>()) { |
| 212 | + for def_id in def_path_def_ids(cx, &path.iter().map(AsRef::as_ref).collect::<Vec<_>>()) { |
213 | 213 | self.array_def_ids.insert((def_id, span)); |
214 | 214 | } |
215 | 215 | } |
@@ -293,41 +293,11 @@ fn path_from_array(exprs: &[Expr<'_>]) -> Option<Vec<String>> { |
293 | 293 | .collect() |
294 | 294 | } |
295 | 295 |
|
296 | | -// def_path_res will match field names before anything else, but for this we want to match |
297 | | -// inherent functions first. |
298 | | -fn inherent_def_path_res(cx: &LateContext<'_>, segments: &[&str]) -> Option<DefId> { |
299 | | - def_path_res(cx, segments, None).opt_def_id().map(|def_id| { |
300 | | - if cx.tcx.def_kind(def_id) == DefKind::Field { |
301 | | - let method_name = *segments.last().unwrap(); |
302 | | - cx.tcx |
303 | | - .def_key(def_id) |
304 | | - .parent |
305 | | - .and_then(|parent_idx| { |
306 | | - cx.tcx |
307 | | - .inherent_impls(DefId { |
308 | | - index: parent_idx, |
309 | | - krate: def_id.krate, |
310 | | - }) |
311 | | - .iter() |
312 | | - .find_map(|impl_id| { |
313 | | - cx.tcx.associated_items(*impl_id).find_by_name_and_kind( |
314 | | - cx.tcx, |
315 | | - Ident::from_str(method_name), |
316 | | - AssocKind::Fn, |
317 | | - *impl_id, |
318 | | - ) |
319 | | - }) |
320 | | - }) |
321 | | - .map_or(def_id, |item| item.def_id) |
322 | | - } else { |
323 | | - def_id |
324 | | - } |
325 | | - }) |
326 | | -} |
327 | | - |
328 | 296 | fn get_lang_item_name(cx: &LateContext<'_>, def_id: DefId) -> Option<Symbol> { |
329 | 297 | if let Some(lang_item) = cx.tcx.lang_items().items().iter().position(|id| *id == Some(def_id)) { |
330 | | - let lang_items = def_path_res(cx, &["rustc_hir", "lang_items", "LangItem"], Some(Namespace::TypeNS)).def_id(); |
| 298 | + let lang_items = def_path_def_ids(cx, &["rustc_hir", "lang_items", "LangItem"]) |
| 299 | + .next() |
| 300 | + .unwrap(); |
331 | 301 | let item_name = cx |
332 | 302 | .tcx |
333 | 303 | .adt_def(lang_items) |
|
0 commit comments