Rename path_to_res to def_path_res

This commit is contained in:
Cameron Steffen 2022-01-05 20:36:22 -06:00
parent 8d5d9e0103
commit cc975929c5
5 changed files with 12 additions and 11 deletions

View file

@ -77,7 +77,7 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedMethods {
fn check_crate(&mut self, cx: &LateContext<'_>) { fn check_crate(&mut self, cx: &LateContext<'_>) {
for (index, conf) in self.conf_disallowed.iter().enumerate() { for (index, conf) in self.conf_disallowed.iter().enumerate() {
let segs: Vec<_> = conf.path().split("::").collect(); let segs: Vec<_> = conf.path().split("::").collect();
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &segs) { if let Res::Def(_, id) = clippy_utils::def_path_res(cx, &segs) {
self.disallowed.insert(id, index); self.disallowed.insert(id, index);
} }
} }

View file

@ -96,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for DisallowedTypes {
), ),
}; };
let segs: Vec<_> = path.split("::").collect(); let segs: Vec<_> = path.split("::").collect();
match clippy_utils::path_to_res(cx, &segs) { match clippy_utils::def_path_res(cx, &segs) {
Res::Def(_, id) => { Res::Def(_, id) => {
self.def_ids.insert(id, reason); self.def_ids.insert(id, reason);
}, },

View file

@ -58,7 +58,7 @@ impl_lint_pass!(ImportRename => [MISSING_ENFORCED_IMPORT_RENAMES]);
impl LateLintPass<'_> for ImportRename { impl LateLintPass<'_> for ImportRename {
fn check_crate(&mut self, cx: &LateContext<'_>) { fn check_crate(&mut self, cx: &LateContext<'_>) {
for Rename { path, rename } in &self.conf_renames { for Rename { path, rename } in &self.conf_renames {
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &path.split("::").collect::<Vec<_>>()) { if let Res::Def(_, id) = clippy_utils::def_path_res(cx, &path.split("::").collect::<Vec<_>>()) {
self.renames.insert(id, Symbol::intern(rename)); self.renames.insert(id, Symbol::intern(rename));
} }
} }

View file

@ -4,8 +4,8 @@ use clippy_utils::macros::root_macro_call_first_node;
use clippy_utils::source::snippet; use clippy_utils::source::snippet;
use clippy_utils::ty::match_type; use clippy_utils::ty::match_type;
use clippy_utils::{ use clippy_utils::{
higher, is_else_clause, is_expn_of, is_expr_path_def_path, is_lint_allowed, match_def_path, method_calls, def_path_res, higher, is_else_clause, is_expn_of, is_expr_path_def_path, is_lint_allowed, match_def_path,
path_to_res, paths, peel_blocks_with_stmt, SpanlessEq, method_calls, paths, peel_blocks_with_stmt, SpanlessEq,
}; };
use if_chain::if_chain; use if_chain::if_chain;
use rustc_ast as ast; use rustc_ast as ast;
@ -844,7 +844,7 @@ impl<'tcx> LateLintPass<'tcx> for MatchTypeOnDiagItem {
// Extract the path to the matched type // Extract the path to the matched type
if let Some(segments) = path_to_matched_type(cx, ty_path); if let Some(segments) = path_to_matched_type(cx, ty_path);
let segments: Vec<&str> = segments.iter().map(Symbol::as_str).collect(); let segments: Vec<&str> = segments.iter().map(Symbol::as_str).collect();
if let Some(ty_did) = path_to_res(cx, &segments[..]).opt_def_id(); if let Some(ty_did) = def_path_res(cx, &segments[..]).opt_def_id();
// Check if the matched type is a diagnostic item // Check if the matched type is a diagnostic item
if let Some(item_name) = cx.tcx.get_diagnostic_name(ty_did); if let Some(item_name) = cx.tcx.get_diagnostic_name(ty_did);
then { then {
@ -917,7 +917,7 @@ fn path_to_matched_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<Ve
// This is not a complete resolver for paths. It works on all the paths currently used in the paths // This is not a complete resolver for paths. It works on all the paths currently used in the paths
// module. That's all it does and all it needs to do. // module. That's all it does and all it needs to do.
pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool { pub fn check_path(cx: &LateContext<'_>, path: &[&str]) -> bool {
if path_to_res(cx, path) != Res::Err { if def_path_res(cx, path) != Res::Err {
return true; return true;
} }
@ -999,7 +999,7 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
} }
for &module in &[&paths::KW_MODULE, &paths::SYM_MODULE] { for &module in &[&paths::KW_MODULE, &paths::SYM_MODULE] {
if let Some(def_id) = path_to_res(cx, module).opt_def_id() { if let Some(def_id) = def_path_res(cx, module).opt_def_id() {
for item in cx.tcx.module_children(def_id).iter() { for item in cx.tcx.module_children(def_id).iter() {
if_chain! { if_chain! {
if let Res::Def(DefKind::Const, item_def_id) = item.res; if let Res::Def(DefKind::Const, item_def_id) = item.res;

View file

@ -497,8 +497,9 @@ pub fn path_to_local_id(expr: &Expr<'_>, id: HirId) -> bool {
path_to_local(expr) == Some(id) path_to_local(expr) == Some(id)
} }
/// Gets the definition associated to a path. /// Resolves a def path like `std::vec::Vec`.
pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Res { /// This function is expensive and should be used sparingly.
pub fn def_path_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
macro_rules! try_res { macro_rules! try_res {
($e:expr) => { ($e:expr) => {
match $e { match $e {
@ -574,7 +575,7 @@ pub fn path_to_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
/// Convenience function to get the `DefId` of a trait by path. /// Convenience function to get the `DefId` of a trait by path.
/// It could be a trait or trait alias. /// It could be a trait or trait alias.
pub fn get_trait_def_id(cx: &LateContext<'_>, path: &[&str]) -> Option<DefId> { pub fn get_trait_def_id(cx: &LateContext<'_>, path: &[&str]) -> Option<DefId> {
match path_to_res(cx, path) { match def_path_res(cx, path) {
Res::Def(DefKind::Trait | DefKind::TraitAlias, trait_id) => Some(trait_id), Res::Def(DefKind::Trait | DefKind::TraitAlias, trait_id) => Some(trait_id),
_ => None, _ => None,
} }