Avoid using id followed by local_def_id if we can just call def_id instead

This commit is contained in:
Oli Scherer
2026-05-05 06:36:36 +02:00
parent 0afe083fd1
commit 191cda55e4
3 changed files with 15 additions and 11 deletions
@@ -45,10 +45,7 @@ fn nearest_normal_mod(&self, def_id: LocalDefId) -> LocalDefId {
fn private_vis_import(&self, decl: Decl<'_>) -> Visibility {
let DeclKind::Import { import, .. } = decl.kind else { unreachable!() };
Visibility::Restricted(
import
.id()
.map(|id| self.nearest_normal_mod(self.local_def_id(id)))
.unwrap_or(CRATE_DEF_ID),
import.def_id().map(|id| self.nearest_normal_mod(id)).unwrap_or(CRATE_DEF_ID),
)
}
@@ -96,8 +93,8 @@ pub(crate) fn compute_effective_visibilities<'c>(
// is the maximum value among visibilities of declarations corresponding to that def id.
for (decl, eff_vis) in visitor.import_effective_visibilities.iter() {
let DeclKind::Import { import, .. } = decl.kind else { unreachable!() };
if let Some(node_id) = import.id() {
r.effective_visibilities.update_eff_vis(r.local_def_id(node_id), eff_vis, r.tcx)
if let Some(def_id) = import.def_id() {
r.effective_visibilities.update_eff_vis(def_id, eff_vis, r.tcx)
}
if decl.ambiguity.get().is_some() && eff_vis.is_public_at_level(Level::Reexported) {
exported_ambiguities.insert(*decl);
+11 -3
View File
@@ -268,6 +268,15 @@ pub(crate) fn id(&self) -> Option<NodeId> {
}
}
pub(crate) fn def_id(&self) -> Option<LocalDefId> {
match self.kind {
ImportKind::Single { def_id, .. }
| ImportKind::Glob { def_id, .. }
| ImportKind::ExternCrate { def_id, .. } => Some(def_id),
ImportKind::MacroUse { .. } | ImportKind::MacroExport => None,
}
}
pub(crate) fn simplify(&self) -> Reexport {
match self.kind {
ImportKind::Single { def_id, .. } => Reexport::Single(def_id.to_def_id()),
@@ -852,8 +861,7 @@ pub(crate) fn lint_reexports(&mut self, exported_ambiguities: FxHashSet<Decl<'ra
if binding.res() != Res::Err
&& glob_decl.res() != Res::Err
&& let DeclKind::Import { import: glob_import, .. } = glob_decl.kind
&& let Some(glob_import_id) = glob_import.id()
&& let glob_import_def_id = self.local_def_id(glob_import_id)
&& let Some(glob_import_def_id) = glob_import.def_id()
&& self.effective_visibilities.is_exported(glob_import_def_id)
&& glob_decl.vis().is_public()
&& !binding.vis().is_public()
@@ -882,7 +890,7 @@ pub(crate) fn lint_reexports(&mut self, exported_ambiguities: FxHashSet<Decl<'ra
if let DeclKind::Import { import, .. } = binding.kind
&& let Some(binding_id) = import.id()
&& let import_def_id = self.local_def_id(binding_id)
&& let import_def_id = import.def_id().unwrap()
&& self.effective_visibilities.is_exported(import_def_id)
&& let Res::Def(reexported_kind, reexported_def_id) = binding.res()
&& !matches!(reexported_kind, DefKind::Ctor(..))
+1 -2
View File
@@ -2155,8 +2155,7 @@ fn find_transitive_imports(
) -> &'tcx [LocalDefId] {
let mut import_ids: SmallVec<[LocalDefId; 1]> = smallvec![];
while let DeclKind::Import { import, source_decl, .. } = kind {
if let Some(node_id) = import.id() {
let def_id = self.local_def_id(node_id);
if let Some(def_id) = import.def_id() {
self.maybe_unused_trait_imports.insert(def_id);
import_ids.push(def_id);
}