mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-29 10:13:54 +00:00
hir: Remove opt_local_def_id_to_hir_id
and opt_hir_node_by_def_id
Also replace a few `hir_node()` calls with `hir_node_by_def_id()`
This commit is contained in:
parent
30f74ff0dc
commit
89b536dbc8
@ -422,8 +422,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
|
|||||||
(None, &[][..], 0)
|
(None, &[][..], 0)
|
||||||
};
|
};
|
||||||
if let Some(def_id) = def_id
|
if let Some(def_id) = def_id
|
||||||
&& let node =
|
&& let node = self.infcx.tcx.hir_node_by_def_id(def_id)
|
||||||
self.infcx.tcx.hir_node(self.infcx.tcx.local_def_id_to_hir_id(def_id))
|
|
||||||
&& let Some(fn_sig) = node.fn_sig()
|
&& let Some(fn_sig) = node.fn_sig()
|
||||||
&& let Some(ident) = node.ident()
|
&& let Some(ident) = node.ident()
|
||||||
&& let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id)
|
&& let Some(pos) = args.iter().position(|arg| arg.hir_id == expr.hir_id)
|
||||||
|
@ -672,11 +672,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
|||||||
};
|
};
|
||||||
(
|
(
|
||||||
true,
|
true,
|
||||||
td.as_local().and_then(|tld| match self.infcx.tcx.opt_hir_node_by_def_id(tld) {
|
td.as_local().and_then(|tld| match self.infcx.tcx.hir_node_by_def_id(tld) {
|
||||||
Some(Node::Item(hir::Item {
|
Node::Item(hir::Item { kind: hir::ItemKind::Trait(_, _, _, _, items), .. }) => {
|
||||||
kind: hir::ItemKind::Trait(_, _, _, _, items),
|
|
||||||
..
|
|
||||||
})) => {
|
|
||||||
let mut f_in_trait_opt = None;
|
let mut f_in_trait_opt = None;
|
||||||
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
|
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
|
||||||
let hi = fi.hir_id();
|
let hi = fi.hir_id();
|
||||||
@ -1475,11 +1472,9 @@ fn get_mut_span_in_struct_field<'tcx>(
|
|||||||
if let ty::Ref(_, ty, _) = ty.kind()
|
if let ty::Ref(_, ty, _) = ty.kind()
|
||||||
&& let ty::Adt(def, _) = ty.kind()
|
&& let ty::Adt(def, _) = ty.kind()
|
||||||
&& let field = def.all_fields().nth(field.index())?
|
&& let field = def.all_fields().nth(field.index())?
|
||||||
// Use the HIR types to construct the diagnostic message.
|
|
||||||
&& let node = tcx.opt_hir_node_by_def_id(field.did.as_local()?)?
|
|
||||||
// Now we're dealing with the actual struct that we're going to suggest a change to,
|
// Now we're dealing with the actual struct that we're going to suggest a change to,
|
||||||
// we can expect a field that is an immutable reference to a type.
|
// we can expect a field that is an immutable reference to a type.
|
||||||
&& let hir::Node::Field(field) = node
|
&& let hir::Node::Field(field) = tcx.hir_node_by_def_id(field.did.as_local()?)
|
||||||
&& let hir::TyKind::Ref(lt, hir::MutTy { mutbl: hir::Mutability::Not, ty }) = field.ty.kind
|
&& let hir::TyKind::Ref(lt, hir::MutTy { mutbl: hir::Mutability::Not, ty }) = field.ty.kind
|
||||||
{
|
{
|
||||||
return Some(lt.ident.span.between(ty.span));
|
return Some(lt.ident.span.between(ty.span));
|
||||||
|
@ -42,8 +42,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
|
|||||||
if !def_id.is_local() {
|
if !def_id.is_local() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let hir_id = tcx.local_def_id_to_hir_id(def_id.expect_local());
|
match tcx.hir_node_by_def_id(def_id.expect_local()) {
|
||||||
match tcx.hir_node(hir_id) {
|
|
||||||
Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. }) => {
|
Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. }) => {
|
||||||
generics.params.is_empty().not().then_some(generics.span)
|
generics.params.is_empty().not().then_some(generics.span)
|
||||||
}
|
}
|
||||||
@ -57,8 +56,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
|
|||||||
if !def_id.is_local() {
|
if !def_id.is_local() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let hir_id = tcx.local_def_id_to_hir_id(def_id.expect_local());
|
match tcx.hir_node_by_def_id(def_id.expect_local()) {
|
||||||
match tcx.hir_node(hir_id) {
|
|
||||||
Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. }) => {
|
Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, generics, _), .. }) => {
|
||||||
Some(generics.where_clause_span)
|
Some(generics.where_clause_span)
|
||||||
}
|
}
|
||||||
@ -79,8 +77,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
|
|||||||
if !def_id.is_local() {
|
if !def_id.is_local() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let hir_id = tcx.local_def_id_to_hir_id(def_id.expect_local());
|
match tcx.hir_node_by_def_id(def_id.expect_local()) {
|
||||||
match tcx.hir_node(hir_id) {
|
|
||||||
Node::Item(hir::Item { kind: hir::ItemKind::Fn(fn_sig, _, _), .. }) => {
|
Node::Item(hir::Item { kind: hir::ItemKind::Fn(fn_sig, _, _), .. }) => {
|
||||||
Some(fn_sig.decl.output.span())
|
Some(fn_sig.decl.output.span())
|
||||||
}
|
}
|
||||||
|
@ -130,7 +130,7 @@ fn get_owner_return_paths(
|
|||||||
) -> Option<(LocalDefId, ReturnsVisitor<'_>)> {
|
) -> Option<(LocalDefId, ReturnsVisitor<'_>)> {
|
||||||
let hir_id = tcx.local_def_id_to_hir_id(def_id);
|
let hir_id = tcx.local_def_id_to_hir_id(def_id);
|
||||||
let parent_id = tcx.hir().get_parent_item(hir_id).def_id;
|
let parent_id = tcx.hir().get_parent_item(hir_id).def_id;
|
||||||
tcx.opt_hir_node_by_def_id(parent_id).and_then(|node| node.body_id()).map(|body_id| {
|
tcx.hir_node_by_def_id(parent_id).body_id().map(|body_id| {
|
||||||
let body = tcx.hir().body(body_id);
|
let body = tcx.hir().body(body_id);
|
||||||
let mut visitor = ReturnsVisitor::default();
|
let mut visitor = ReturnsVisitor::default();
|
||||||
visitor.visit_body(body);
|
visitor.visit_body(body);
|
||||||
|
@ -1969,13 +1969,10 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
|
|||||||
// Match the existing behavior.
|
// Match the existing behavior.
|
||||||
if pred.is_global() && !pred.has_type_flags(TypeFlags::HAS_BINDER_VARS) {
|
if pred.is_global() && !pred.has_type_flags(TypeFlags::HAS_BINDER_VARS) {
|
||||||
let pred = self.normalize(span, None, pred);
|
let pred = self.normalize(span, None, pred);
|
||||||
let hir_node = tcx.opt_hir_node_by_def_id(self.body_def_id);
|
|
||||||
|
|
||||||
// only use the span of the predicate clause (#90869)
|
// only use the span of the predicate clause (#90869)
|
||||||
|
let hir_node = tcx.hir_node_by_def_id(self.body_def_id);
|
||||||
if let Some(hir::Generics { predicates, .. }) =
|
if let Some(hir::Generics { predicates, .. }) = hir_node.generics() {
|
||||||
hir_node.and_then(|node| node.generics())
|
|
||||||
{
|
|
||||||
span = predicates
|
span = predicates
|
||||||
.iter()
|
.iter()
|
||||||
// There seems to be no better way to find out which predicate we are in
|
// There seems to be no better way to find out which predicate we are in
|
||||||
|
@ -609,10 +609,8 @@ pub(super) fn implied_predicates_with_filter(
|
|||||||
return tcx.super_predicates_of(trait_def_id);
|
return tcx.super_predicates_of(trait_def_id);
|
||||||
};
|
};
|
||||||
|
|
||||||
let trait_hir_id = tcx.local_def_id_to_hir_id(trait_def_id);
|
let Node::Item(item) = tcx.hir_node_by_def_id(trait_def_id) else {
|
||||||
|
bug!("trait_def_id {trait_def_id:?} is not an item");
|
||||||
let Node::Item(item) = tcx.hir_node(trait_hir_id) else {
|
|
||||||
bug!("trait_node_id {} is not an item", trait_hir_id);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let (generics, bounds) = match item.kind {
|
let (generics, bounds) = match item.kind {
|
||||||
|
@ -371,11 +371,10 @@ pub(super) fn find_opaque_ty_constraints_for_rpit<'tcx>(
|
|||||||
return mir_opaque_ty.ty;
|
return mir_opaque_ty.ty;
|
||||||
}
|
}
|
||||||
|
|
||||||
let scope = tcx.local_def_id_to_hir_id(owner_def_id);
|
debug!(?owner_def_id);
|
||||||
debug!(?scope);
|
|
||||||
let mut locator = RpitConstraintChecker { def_id, tcx, found: mir_opaque_ty };
|
let mut locator = RpitConstraintChecker { def_id, tcx, found: mir_opaque_ty };
|
||||||
|
|
||||||
match tcx.hir_node(scope) {
|
match tcx.hir_node_by_def_id(owner_def_id) {
|
||||||
Node::Item(it) => intravisit::walk_item(&mut locator, it),
|
Node::Item(it) => intravisit::walk_item(&mut locator, it),
|
||||||
Node::ImplItem(it) => intravisit::walk_impl_item(&mut locator, it),
|
Node::ImplItem(it) => intravisit::walk_impl_item(&mut locator, it),
|
||||||
Node::TraitItem(it) => intravisit::walk_trait_item(&mut locator, it),
|
Node::TraitItem(it) => intravisit::walk_trait_item(&mut locator, it),
|
||||||
|
@ -234,11 +234,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Next, make sure that we have no type expectation.
|
// Next, make sure that we have no type expectation.
|
||||||
let Some(ret) = self
|
let Some(ret) =
|
||||||
.tcx
|
self.tcx.hir_node_by_def_id(self.body_id).fn_decl().map(|decl| decl.output.span())
|
||||||
.opt_hir_node_by_def_id(self.body_id)
|
|
||||||
.and_then(|owner| owner.fn_decl())
|
|
||||||
.map(|decl| decl.output.span())
|
|
||||||
else {
|
else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
@ -890,21 +890,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
|
|
||||||
let encl_item_id = self.tcx.hir().get_parent_item(expr.hir_id);
|
let encl_item_id = self.tcx.hir().get_parent_item(expr.hir_id);
|
||||||
|
|
||||||
if let Some(hir::Node::Item(hir::Item {
|
if let hir::Node::Item(hir::Item {
|
||||||
kind: hir::ItemKind::Fn(..),
|
kind: hir::ItemKind::Fn(..), span: encl_fn_span, ..
|
||||||
span: encl_fn_span,
|
})
|
||||||
..
|
| hir::Node::TraitItem(hir::TraitItem {
|
||||||
}))
|
|
||||||
| Some(hir::Node::TraitItem(hir::TraitItem {
|
|
||||||
kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)),
|
kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)),
|
||||||
span: encl_fn_span,
|
span: encl_fn_span,
|
||||||
..
|
..
|
||||||
}))
|
})
|
||||||
| Some(hir::Node::ImplItem(hir::ImplItem {
|
| hir::Node::ImplItem(hir::ImplItem {
|
||||||
kind: hir::ImplItemKind::Fn(..),
|
kind: hir::ImplItemKind::Fn(..),
|
||||||
span: encl_fn_span,
|
span: encl_fn_span,
|
||||||
..
|
..
|
||||||
})) = self.tcx.opt_hir_node_by_def_id(encl_item_id.def_id)
|
}) = self.tcx.hir_node_by_def_id(encl_item_id.def_id)
|
||||||
{
|
{
|
||||||
// We are inside a function body, so reporting "return statement
|
// We are inside a function body, so reporting "return statement
|
||||||
// outside of function body" needs an explanation.
|
// outside of function body" needs an explanation.
|
||||||
|
@ -2172,16 +2172,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||||||
// Try to find earlier invocations of this closure to find if the type mismatch
|
// Try to find earlier invocations of this closure to find if the type mismatch
|
||||||
// is because of inference. If we find one, point at them.
|
// is because of inference. If we find one, point at them.
|
||||||
let mut call_finder = FindClosureArg { tcx: self.tcx, calls: vec![] };
|
let mut call_finder = FindClosureArg { tcx: self.tcx, calls: vec![] };
|
||||||
let node = self
|
let parent_def_id = self.tcx.hir().get_parent_item(call_expr.hir_id).def_id;
|
||||||
.tcx
|
match self.tcx.hir_node_by_def_id(parent_def_id) {
|
||||||
.opt_local_def_id_to_hir_id(
|
hir::Node::Item(item) => call_finder.visit_item(item),
|
||||||
self.tcx.hir().get_parent_item(call_expr.hir_id).def_id,
|
hir::Node::TraitItem(item) => call_finder.visit_trait_item(item),
|
||||||
)
|
hir::Node::ImplItem(item) => call_finder.visit_impl_item(item),
|
||||||
.map(|hir_id| self.tcx.hir_node(hir_id));
|
|
||||||
match node {
|
|
||||||
Some(hir::Node::Item(item)) => call_finder.visit_item(item),
|
|
||||||
Some(hir::Node::TraitItem(item)) => call_finder.visit_trait_item(item),
|
|
||||||
Some(hir::Node::ImplItem(item)) => call_finder.visit_impl_item(item),
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
let typeck = self.typeck_results.borrow();
|
let typeck = self.typeck_results.borrow();
|
||||||
|
@ -2126,8 +2126,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
let TypeError::FixedArraySize(sz) = terr else {
|
let TypeError::FixedArraySize(sz) = terr else {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
let tykind = match self.tcx.opt_hir_node_by_def_id(trace.cause.body_id) {
|
let tykind = match self.tcx.hir_node_by_def_id(trace.cause.body_id) {
|
||||||
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) => {
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. }) => {
|
||||||
let body = hir.body(*body_id);
|
let body = hir.body(*body_id);
|
||||||
struct LetVisitor {
|
struct LetVisitor {
|
||||||
span: Span,
|
span: Span,
|
||||||
@ -2156,7 +2156,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
}
|
}
|
||||||
LetVisitor { span }.visit_body(body).break_value()
|
LetVisitor { span }.visit_body(body).break_value()
|
||||||
}
|
}
|
||||||
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _, _), .. })) => {
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _, _), .. }) => {
|
||||||
Some(&ty.peel_refs().kind)
|
Some(&ty.peel_refs().kind)
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
@ -2527,15 +2527,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
.filter(|p| matches!(p.kind, ty::GenericParamDefKind::Lifetime))
|
.filter(|p| matches!(p.kind, ty::GenericParamDefKind::Lifetime))
|
||||||
.map(|p| p.name)
|
.map(|p| p.name)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
if let Some(hir_id) = self.tcx.opt_local_def_id_to_hir_id(lifetime_scope) {
|
let hir_id = self.tcx.local_def_id_to_hir_id(lifetime_scope);
|
||||||
// consider late-bound lifetimes ...
|
// consider late-bound lifetimes ...
|
||||||
used_names.extend(self.tcx.late_bound_vars(hir_id).into_iter().filter_map(|p| {
|
used_names.extend(self.tcx.late_bound_vars(hir_id).into_iter().filter_map(
|
||||||
match p {
|
|p| match p {
|
||||||
ty::BoundVariableKind::Region(lt) => lt.get_name(),
|
ty::BoundVariableKind::Region(lt) => lt.get_name(),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
},
|
||||||
}))
|
));
|
||||||
}
|
|
||||||
(b'a'..=b'z')
|
(b'a'..=b'z')
|
||||||
.map(|c| format!("'{}", c as char))
|
.map(|c| format!("'{}", c as char))
|
||||||
.find(|candidate| !used_names.iter().any(|e| e.as_str() == candidate))
|
.find(|candidate| !used_names.iter().any(|e| e.as_str() == candidate))
|
||||||
|
@ -459,7 +459,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
|||||||
tcx.hir().trait_impls(trait_did).iter().find_map(|&impl_did| {
|
tcx.hir().trait_impls(trait_did).iter().find_map(|&impl_did| {
|
||||||
if let Node::Item(Item {
|
if let Node::Item(Item {
|
||||||
kind: ItemKind::Impl(hir::Impl { self_ty, .. }), ..
|
kind: ItemKind::Impl(hir::Impl { self_ty, .. }), ..
|
||||||
}) = tcx.opt_hir_node_by_def_id(impl_did)?
|
}) = tcx.hir_node_by_def_id(impl_did)
|
||||||
&& trait_objects.iter().all(|did| {
|
&& trait_objects.iter().all(|did| {
|
||||||
// FIXME: we should check `self_ty` against the receiver
|
// FIXME: we should check `self_ty` against the receiver
|
||||||
// type in the `UnifyReceiver` context, but for now, use
|
// type in the `UnifyReceiver` context, but for now, use
|
||||||
|
@ -804,23 +804,22 @@ fn foo(&self) -> Self::T { String::new() }
|
|||||||
) -> bool {
|
) -> bool {
|
||||||
let tcx = self.tcx;
|
let tcx = self.tcx;
|
||||||
|
|
||||||
let Some(hir_id) = body_owner_def_id.as_local() else {
|
let Some(def_id) = body_owner_def_id.as_local() else {
|
||||||
return false;
|
|
||||||
};
|
|
||||||
let Some(hir_id) = tcx.opt_local_def_id_to_hir_id(hir_id) else {
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
// When `body_owner` is an `impl` or `trait` item, look in its associated types for
|
// When `body_owner` is an `impl` or `trait` item, look in its associated types for
|
||||||
// `expected` and point at it.
|
// `expected` and point at it.
|
||||||
|
let hir_id = tcx.local_def_id_to_hir_id(def_id);
|
||||||
let parent_id = tcx.hir().get_parent_item(hir_id);
|
let parent_id = tcx.hir().get_parent_item(hir_id);
|
||||||
let item = tcx.opt_hir_node_by_def_id(parent_id.def_id);
|
let item = tcx.hir_node_by_def_id(parent_id.def_id);
|
||||||
|
|
||||||
debug!("expected_projection parent item {:?}", item);
|
debug!("expected_projection parent item {:?}", item);
|
||||||
|
|
||||||
let param_env = tcx.param_env(body_owner_def_id);
|
let param_env = tcx.param_env(body_owner_def_id);
|
||||||
|
|
||||||
match item {
|
match item {
|
||||||
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Trait(.., items), .. })) => {
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Trait(.., items), .. }) => {
|
||||||
// FIXME: account for `#![feature(specialization)]`
|
// FIXME: account for `#![feature(specialization)]`
|
||||||
for item in &items[..] {
|
for item in &items[..] {
|
||||||
match item.kind {
|
match item.kind {
|
||||||
@ -845,10 +844,10 @@ fn foo(&self) -> Self::T { String::new() }
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(hir::Node::Item(hir::Item {
|
hir::Node::Item(hir::Item {
|
||||||
kind: hir::ItemKind::Impl(hir::Impl { items, .. }),
|
kind: hir::ItemKind::Impl(hir::Impl { items, .. }),
|
||||||
..
|
..
|
||||||
})) => {
|
}) => {
|
||||||
for item in &items[..] {
|
for item in &items[..] {
|
||||||
if let hir::AssocItemKind::Type = item.kind {
|
if let hir::AssocItemKind::Type = item.kind {
|
||||||
let assoc_ty = tcx.type_of(item.id.owner_id).instantiate_identity();
|
let assoc_ty = tcx.type_of(item.id.owner_id).instantiate_identity();
|
||||||
|
@ -1339,8 +1339,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
|||||||
is_doc_hidden: false,
|
is_doc_hidden: false,
|
||||||
};
|
};
|
||||||
let attr_iter = tcx
|
let attr_iter = tcx
|
||||||
.opt_local_def_id_to_hir_id(def_id)
|
.hir()
|
||||||
.map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id))
|
.attrs(tcx.local_def_id_to_hir_id(def_id))
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|attr| analyze_attr(attr, &mut state));
|
.filter(|attr| analyze_attr(attr, &mut state));
|
||||||
|
|
||||||
|
@ -158,12 +158,6 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||||||
self.hir_owner_nodes(owner_id).node()
|
self.hir_owner_nodes(owner_id).node()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieves the `hir::Node` corresponding to `id`, returning `None` if cannot be found.
|
|
||||||
#[inline]
|
|
||||||
pub fn opt_hir_node_by_def_id(self, id: LocalDefId) -> Option<Node<'tcx>> {
|
|
||||||
Some(self.hir_node_by_def_id(id))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Retrieves the `hir::Node` corresponding to `id`.
|
/// Retrieves the `hir::Node` corresponding to `id`.
|
||||||
pub fn hir_node(self, id: HirId) -> Node<'tcx> {
|
pub fn hir_node(self, id: HirId) -> Node<'tcx> {
|
||||||
self.hir_owner_nodes(id.owner).nodes[id.local_id].node
|
self.hir_owner_nodes(id.owner).nodes[id.local_id].node
|
||||||
@ -239,8 +233,7 @@ impl<'hir> Map<'hir> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_if_local(self, id: DefId) -> Option<Node<'hir>> {
|
pub fn get_if_local(self, id: DefId) -> Option<Node<'hir>> {
|
||||||
id.as_local()
|
id.as_local().map(|id| self.tcx.hir_node_by_def_id(id))
|
||||||
.and_then(|id| Some(self.tcx.hir_node(self.tcx.opt_local_def_id_to_hir_id(id)?)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_generics(self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
|
pub fn get_generics(self, id: LocalDefId) -> Option<&'hir Generics<'hir>> {
|
||||||
@ -304,7 +297,7 @@ impl<'hir> Map<'hir> {
|
|||||||
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
|
/// Given a `LocalDefId`, returns the `BodyId` associated with it,
|
||||||
/// if the node is a body owner, otherwise returns `None`.
|
/// if the node is a body owner, otherwise returns `None`.
|
||||||
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
|
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
|
||||||
let node = self.tcx.opt_hir_node_by_def_id(id)?;
|
let node = self.tcx.hir_node_by_def_id(id);
|
||||||
let (_, body_id) = associated_body(node)?;
|
let (_, body_id) = associated_body(node)?;
|
||||||
Some(body_id)
|
Some(body_id)
|
||||||
}
|
}
|
||||||
|
@ -1265,11 +1265,9 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||||||
break (scope, ty::BrNamed(def_id.into(), self.item_name(def_id.into())));
|
break (scope, ty::BrNamed(def_id.into(), self.item_name(def_id.into())));
|
||||||
};
|
};
|
||||||
|
|
||||||
let is_impl_item = match self.opt_hir_node_by_def_id(suitable_region_binding_scope) {
|
let is_impl_item = match self.hir_node_by_def_id(suitable_region_binding_scope) {
|
||||||
Some(Node::Item(..) | Node::TraitItem(..)) => false,
|
Node::Item(..) | Node::TraitItem(..) => false,
|
||||||
Some(Node::ImplItem(..)) => {
|
Node::ImplItem(..) => self.is_bound_region_in_impl_item(suitable_region_binding_scope),
|
||||||
self.is_bound_region_in_impl_item(suitable_region_binding_scope)
|
|
||||||
}
|
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -2355,10 +2353,6 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||||||
self.intrinsic_raw(def_id)
|
self.intrinsic_raw(def_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn opt_local_def_id_to_hir_id(self, local_def_id: LocalDefId) -> Option<HirId> {
|
|
||||||
Some(self.local_def_id_to_hir_id(local_def_id))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn next_trait_solver_globally(self) -> bool {
|
pub fn next_trait_solver_globally(self) -> bool {
|
||||||
self.sess.opts.unstable_opts.next_solver.map_or(false, |c| c.globally)
|
self.sess.opts.unstable_opts.next_solver.map_or(false, |c| c.globally)
|
||||||
}
|
}
|
||||||
|
@ -33,15 +33,13 @@ use crate::errors::{
|
|||||||
// may need to be marked as live.
|
// may need to be marked as live.
|
||||||
fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
|
fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
tcx.opt_hir_node_by_def_id(def_id),
|
tcx.hir_node_by_def_id(def_id),
|
||||||
Some(
|
Node::Item(..)
|
||||||
Node::Item(..)
|
| Node::ImplItem(..)
|
||||||
| Node::ImplItem(..)
|
| Node::ForeignItem(..)
|
||||||
| Node::ForeignItem(..)
|
| Node::TraitItem(..)
|
||||||
| Node::TraitItem(..)
|
| Node::Variant(..)
|
||||||
| Node::Variant(..)
|
| Node::AnonConst(..)
|
||||||
| Node::AnonConst(..)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,33 +314,31 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
|
|||||||
// tuple struct constructor function
|
// tuple struct constructor function
|
||||||
let id = self.struct_constructors.get(&id).copied().unwrap_or(id);
|
let id = self.struct_constructors.get(&id).copied().unwrap_or(id);
|
||||||
|
|
||||||
if let Some(node) = self.tcx.opt_hir_node_by_def_id(id) {
|
// When using `#[allow]` or `#[expect]` of `dead_code`, we do a QOL improvement
|
||||||
// When using `#[allow]` or `#[expect]` of `dead_code`, we do a QOL improvement
|
// by declaring fn calls, statics, ... within said items as live, as well as
|
||||||
// by declaring fn calls, statics, ... within said items as live, as well as
|
// the item itself, although technically this is not the case.
|
||||||
// the item itself, although technically this is not the case.
|
//
|
||||||
//
|
// This means that the lint for said items will never be fired.
|
||||||
// This means that the lint for said items will never be fired.
|
//
|
||||||
//
|
// This doesn't make any difference for the item declared with `#[allow]`, as
|
||||||
// This doesn't make any difference for the item declared with `#[allow]`, as
|
// the lint firing will be a nop, as it will be silenced by the `#[allow]` of
|
||||||
// the lint firing will be a nop, as it will be silenced by the `#[allow]` of
|
// the item.
|
||||||
// the item.
|
//
|
||||||
//
|
// However, for `#[expect]`, the presence or absence of the lint is relevant,
|
||||||
// However, for `#[expect]`, the presence or absence of the lint is relevant,
|
// so we don't add it to the list of live symbols when it comes from a
|
||||||
// so we don't add it to the list of live symbols when it comes from a
|
// `#[expect]`. This means that we will correctly report an item as live or not
|
||||||
// `#[expect]`. This means that we will correctly report an item as live or not
|
// for the `#[expect]` case.
|
||||||
// for the `#[expect]` case.
|
//
|
||||||
//
|
// Note that an item can and will be duplicated on the worklist with different
|
||||||
// Note that an item can and will be duplicated on the worklist with different
|
// `ComesFromAllowExpect`, particularly if it was added from the
|
||||||
// `ComesFromAllowExpect`, particularly if it was added from the
|
// `effective_visibilities` query or from the `#[allow]`/`#[expect]` checks,
|
||||||
// `effective_visibilities` query or from the `#[allow]`/`#[expect]` checks,
|
// this "duplication" is essential as otherwise a function with `#[expect]`
|
||||||
// this "duplication" is essential as otherwise a function with `#[expect]`
|
// called from a `pub fn` may be falsely reported as not live, falsely
|
||||||
// called from a `pub fn` may be falsely reported as not live, falsely
|
// triggering the `unfulfilled_lint_expectations` lint.
|
||||||
// triggering the `unfulfilled_lint_expectations` lint.
|
if comes_from_allow_expect != ComesFromAllowExpect::Yes {
|
||||||
if comes_from_allow_expect != ComesFromAllowExpect::Yes {
|
self.live_symbols.insert(id);
|
||||||
self.live_symbols.insert(id);
|
|
||||||
}
|
|
||||||
self.visit_node(node);
|
|
||||||
}
|
}
|
||||||
|
self.visit_node(self.tcx.hir_node_by_def_id(id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -739,8 +735,8 @@ fn check_item<'tcx>(
|
|||||||
for local_def_id in local_def_ids {
|
for local_def_id in local_def_ids {
|
||||||
// check the function may construct Self
|
// check the function may construct Self
|
||||||
let mut may_construct_self = true;
|
let mut may_construct_self = true;
|
||||||
if let Some(hir_id) = tcx.opt_local_def_id_to_hir_id(local_def_id)
|
if let Some(fn_sig) =
|
||||||
&& let Some(fn_sig) = tcx.hir().fn_sig_by_hir_id(hir_id)
|
tcx.hir().fn_sig_by_hir_id(tcx.local_def_id_to_hir_id(local_def_id))
|
||||||
{
|
{
|
||||||
may_construct_self =
|
may_construct_self =
|
||||||
matches!(fn_sig.decl.implicit_self, hir::ImplicitSelfKind::None);
|
matches!(fn_sig.decl.implicit_self, hir::ImplicitSelfKind::None);
|
||||||
|
@ -127,7 +127,7 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) -> Option<(DefId,
|
|||||||
{
|
{
|
||||||
// non-local main imports are handled below
|
// non-local main imports are handled below
|
||||||
if let Some(def_id) = def_id.as_local()
|
if let Some(def_id) = def_id.as_local()
|
||||||
&& matches!(tcx.opt_hir_node_by_def_id(def_id), Some(Node::ForeignItem(_)))
|
&& matches!(tcx.hir_node_by_def_id(def_id), Node::ForeignItem(_))
|
||||||
{
|
{
|
||||||
tcx.dcx().emit_err(ExternMain { span: tcx.def_span(def_id) });
|
tcx.dcx().emit_err(ExternMain { span: tcx.def_span(def_id) });
|
||||||
return None;
|
return None;
|
||||||
|
@ -116,26 +116,25 @@ impl<'tcx> ReachableContext<'tcx> {
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.tcx.opt_hir_node_by_def_id(def_id) {
|
match self.tcx.hir_node_by_def_id(def_id) {
|
||||||
Some(Node::Item(item)) => match item.kind {
|
Node::Item(item) => match item.kind {
|
||||||
hir::ItemKind::Fn(..) => item_might_be_inlined(self.tcx, def_id.into()),
|
hir::ItemKind::Fn(..) => item_might_be_inlined(self.tcx, def_id.into()),
|
||||||
_ => false,
|
_ => false,
|
||||||
},
|
},
|
||||||
Some(Node::TraitItem(trait_method)) => match trait_method.kind {
|
Node::TraitItem(trait_method) => match trait_method.kind {
|
||||||
hir::TraitItemKind::Const(_, ref default) => default.is_some(),
|
hir::TraitItemKind::Const(_, ref default) => default.is_some(),
|
||||||
hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => true,
|
hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)) => true,
|
||||||
hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_))
|
hir::TraitItemKind::Fn(_, hir::TraitFn::Required(_))
|
||||||
| hir::TraitItemKind::Type(..) => false,
|
| hir::TraitItemKind::Type(..) => false,
|
||||||
},
|
},
|
||||||
Some(Node::ImplItem(impl_item)) => match impl_item.kind {
|
Node::ImplItem(impl_item) => match impl_item.kind {
|
||||||
hir::ImplItemKind::Const(..) => true,
|
hir::ImplItemKind::Const(..) => true,
|
||||||
hir::ImplItemKind::Fn(..) => {
|
hir::ImplItemKind::Fn(..) => {
|
||||||
item_might_be_inlined(self.tcx, impl_item.hir_id().owner.to_def_id())
|
item_might_be_inlined(self.tcx, impl_item.hir_id().owner.to_def_id())
|
||||||
}
|
}
|
||||||
hir::ImplItemKind::Type(_) => false,
|
hir::ImplItemKind::Type(_) => false,
|
||||||
},
|
},
|
||||||
Some(_) => false,
|
_ => false,
|
||||||
None => false, // This will happen for default methods.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -147,9 +146,7 @@ impl<'tcx> ReachableContext<'tcx> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref item) = self.tcx.opt_hir_node_by_def_id(search_item) {
|
self.propagate_node(&self.tcx.hir_node_by_def_id(search_item), search_item);
|
||||||
self.propagate_node(item, search_item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
/// Used to set on_unimplemented's `ItemContext`
|
/// Used to set on_unimplemented's `ItemContext`
|
||||||
/// to be the enclosing (async) block/function/closure
|
/// to be the enclosing (async) block/function/closure
|
||||||
fn describe_enclosure(&self, def_id: LocalDefId) -> Option<&'static str> {
|
fn describe_enclosure(&self, def_id: LocalDefId) -> Option<&'static str> {
|
||||||
match self.tcx.opt_hir_node_by_def_id(def_id)? {
|
match self.tcx.hir_node_by_def_id(def_id) {
|
||||||
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. }) => Some("a function"),
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. }) => Some("a function"),
|
||||||
hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. }) => {
|
hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. }) => {
|
||||||
Some("a trait method")
|
Some("a trait method")
|
||||||
|
@ -261,7 +261,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
|
|
||||||
// FIXME: Add check for trait bound that is already present, particularly `?Sized` so we
|
// FIXME: Add check for trait bound that is already present, particularly `?Sized` so we
|
||||||
// don't suggest `T: Sized + ?Sized`.
|
// don't suggest `T: Sized + ?Sized`.
|
||||||
while let Some(node) = self.tcx.opt_hir_node_by_def_id(body_id) {
|
loop {
|
||||||
|
let node = self.tcx.hir_node_by_def_id(body_id);
|
||||||
match node {
|
match node {
|
||||||
hir::Node::Item(hir::Item {
|
hir::Node::Item(hir::Item {
|
||||||
ident,
|
ident,
|
||||||
@ -1685,8 +1686,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
trait_pred: ty::PolyTraitPredicate<'tcx>,
|
trait_pred: ty::PolyTraitPredicate<'tcx>,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
let hir = self.tcx.hir();
|
let hir = self.tcx.hir();
|
||||||
let node = self.tcx.opt_hir_node_by_def_id(obligation.cause.body_id);
|
let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
|
||||||
if let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. })) = node
|
if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. }) = node
|
||||||
&& let hir::ExprKind::Block(blk, _) = &hir.body(*body_id).value.kind
|
&& let hir::ExprKind::Block(blk, _) = &hir.body(*body_id).value.kind
|
||||||
&& sig.decl.output.span().overlaps(span)
|
&& sig.decl.output.span().overlaps(span)
|
||||||
&& blk.expr.is_none()
|
&& blk.expr.is_none()
|
||||||
@ -1720,8 +1721,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
|
fn return_type_span(&self, obligation: &PredicateObligation<'tcx>) -> Option<Span> {
|
||||||
let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })) =
|
let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. }) =
|
||||||
self.tcx.opt_hir_node_by_def_id(obligation.cause.body_id)
|
self.tcx.hir_node_by_def_id(obligation.cause.body_id)
|
||||||
else {
|
else {
|
||||||
return None;
|
return None;
|
||||||
};
|
};
|
||||||
@ -1813,10 +1814,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let hir = self.tcx.hir();
|
let hir = self.tcx.hir();
|
||||||
let node = self.tcx.opt_hir_node_by_def_id(obligation.cause.body_id);
|
let node = self.tcx.hir_node_by_def_id(obligation.cause.body_id);
|
||||||
if let Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. })) =
|
if let hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. }) = node {
|
||||||
node
|
|
||||||
{
|
|
||||||
let body = hir.body(*body_id);
|
let body = hir.body(*body_id);
|
||||||
// Point at all the `return`s in the function as they have failed trait bounds.
|
// Point at all the `return`s in the function as they have failed trait bounds.
|
||||||
let mut visitor = ReturnsVisitor::default();
|
let mut visitor = ReturnsVisitor::default();
|
||||||
@ -4450,7 +4449,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let Some(hir::Node::TraitItem(item)) = self.tcx.opt_hir_node_by_def_id(fn_def_id) else {
|
let hir::Node::TraitItem(item) = self.tcx.hir_node_by_def_id(fn_def_id) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2497,11 +2497,11 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
|||||||
err.code(E0790);
|
err.code(E0790);
|
||||||
|
|
||||||
if let Some(local_def_id) = data.trait_ref.def_id.as_local()
|
if let Some(local_def_id) = data.trait_ref.def_id.as_local()
|
||||||
&& let Some(hir::Node::Item(hir::Item {
|
&& let hir::Node::Item(hir::Item {
|
||||||
ident: trait_name,
|
ident: trait_name,
|
||||||
kind: hir::ItemKind::Trait(_, _, _, _, trait_item_refs),
|
kind: hir::ItemKind::Trait(_, _, _, _, trait_item_refs),
|
||||||
..
|
..
|
||||||
})) = self.tcx.opt_hir_node_by_def_id(local_def_id)
|
}) = self.tcx.hir_node_by_def_id(local_def_id)
|
||||||
&& let Some(method_ref) = trait_item_refs
|
&& let Some(method_ref) = trait_item_refs
|
||||||
.iter()
|
.iter()
|
||||||
.find(|item_ref| item_ref.ident == *assoc_item_name)
|
.find(|item_ref| item_ref.ident == *assoc_item_name)
|
||||||
|
@ -319,11 +319,7 @@ fn associated_type_for_impl_trait_in_impl(
|
|||||||
) -> LocalDefId {
|
) -> LocalDefId {
|
||||||
let impl_local_def_id = tcx.local_parent(impl_fn_def_id);
|
let impl_local_def_id = tcx.local_parent(impl_fn_def_id);
|
||||||
|
|
||||||
let decl = tcx
|
let decl = tcx.hir_node_by_def_id(impl_fn_def_id).fn_decl().expect("expected decl");
|
||||||
.opt_hir_node_by_def_id(impl_fn_def_id)
|
|
||||||
.expect("expected item")
|
|
||||||
.fn_decl()
|
|
||||||
.expect("expected decl");
|
|
||||||
let span = match decl.output {
|
let span = match decl.output {
|
||||||
hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn_def_id),
|
hir::FnRetTy::DefaultReturn(_) => tcx.def_span(impl_fn_def_id),
|
||||||
hir::FnRetTy::Return(ty) => ty.span,
|
hir::FnRetTy::Return(ty) => ty.span,
|
||||||
|
@ -149,8 +149,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn is_glob_import(tcx: TyCtxt<'_>, import_id: LocalDefId) -> bool {
|
fn is_glob_import(tcx: TyCtxt<'_>, import_id: LocalDefId) -> bool {
|
||||||
if let Some(node) = tcx.opt_hir_node_by_def_id(import_id)
|
if let hir::Node::Item(item) = tcx.hir_node_by_def_id(import_id)
|
||||||
&& let hir::Node::Item(item) = node
|
|
||||||
&& let hir::ItemKind::Use(_, use_kind) = item.kind
|
&& let hir::ItemKind::Use(_, use_kind) = item.kind
|
||||||
{
|
{
|
||||||
use_kind == hir::UseKind::Glob
|
use_kind == hir::UseKind::Glob
|
||||||
@ -1612,8 +1611,7 @@ fn first_non_private<'tcx>(
|
|||||||
'reexps: for reexp in child.reexport_chain.iter() {
|
'reexps: for reexp in child.reexport_chain.iter() {
|
||||||
if let Some(use_def_id) = reexp.id()
|
if let Some(use_def_id) = reexp.id()
|
||||||
&& let Some(local_use_def_id) = use_def_id.as_local()
|
&& let Some(local_use_def_id) = use_def_id.as_local()
|
||||||
&& let Some(hir::Node::Item(item)) =
|
&& let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(local_use_def_id)
|
||||||
cx.tcx.opt_hir_node_by_def_id(local_use_def_id)
|
|
||||||
&& !item.ident.name.is_empty()
|
&& !item.ident.name.is_empty()
|
||||||
&& let hir::ItemKind::Use(path, _) = item.kind
|
&& let hir::ItemKind::Use(path, _) = item.kind
|
||||||
{
|
{
|
||||||
|
@ -664,9 +664,10 @@ pub(crate) fn inherits_doc_hidden(
|
|||||||
def_id = id;
|
def_id = id;
|
||||||
if tcx.is_doc_hidden(def_id.to_def_id()) {
|
if tcx.is_doc_hidden(def_id.to_def_id()) {
|
||||||
return true;
|
return true;
|
||||||
} else if let Some(node) = tcx.opt_hir_node_by_def_id(def_id)
|
} else if matches!(
|
||||||
&& matches!(node, hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(_), .. }),)
|
tcx.hir_node_by_def_id(def_id),
|
||||||
{
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Impl(_), .. })
|
||||||
|
) {
|
||||||
// `impl` blocks stand a bit on their own: unless they have `#[doc(hidden)]` directly
|
// `impl` blocks stand a bit on their own: unless they have `#[doc(hidden)]` directly
|
||||||
// on them, they don't inherit it from the parent context.
|
// on them, they don't inherit it from the parent context.
|
||||||
return false;
|
return false;
|
||||||
|
@ -80,9 +80,8 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
|
|||||||
|
|
||||||
// check if parent is trait impl
|
// check if parent is trait impl
|
||||||
if let Some(parent_def_id) = cx.tcx.opt_local_parent(def_id)
|
if let Some(parent_def_id) = cx.tcx.opt_local_parent(def_id)
|
||||||
&& let Some(parent_node) = cx.tcx.opt_hir_node_by_def_id(parent_def_id)
|
|
||||||
&& matches!(
|
&& matches!(
|
||||||
parent_node,
|
cx.tcx.hir_node_by_def_id(parent_def_id),
|
||||||
hir::Node::Item(hir::Item {
|
hir::Node::Item(hir::Item {
|
||||||
kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }),
|
kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }),
|
||||||
..
|
..
|
||||||
|
@ -76,10 +76,9 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
|
|||||||
.hir()
|
.hir()
|
||||||
.get_parent_item(cx.tcx.local_def_id_to_hir_id(fn_def_id))
|
.get_parent_item(cx.tcx.local_def_id_to_hir_id(fn_def_id))
|
||||||
.def_id;
|
.def_id;
|
||||||
let parent_node = cx.tcx.opt_hir_node_by_def_id(parent_id);
|
|
||||||
|
|
||||||
let mut trait_self_ty = None;
|
let mut trait_self_ty = None;
|
||||||
if let Some(Node::Item(item)) = parent_node {
|
if let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent_id) {
|
||||||
// If the method is an impl for a trait, don't warn.
|
// If the method is an impl for a trait, don't warn.
|
||||||
if let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = item.kind {
|
if let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = item.kind {
|
||||||
return;
|
return;
|
||||||
|
@ -46,7 +46,7 @@ impl<'tcx> LateLintPass<'tcx> for Exit {
|
|||||||
&& let Some(def_id) = cx.qpath_res(path, path_expr.hir_id).opt_def_id()
|
&& let Some(def_id) = cx.qpath_res(path, path_expr.hir_id).opt_def_id()
|
||||||
&& cx.tcx.is_diagnostic_item(sym::process_exit, def_id)
|
&& cx.tcx.is_diagnostic_item(sym::process_exit, def_id)
|
||||||
&& let parent = cx.tcx.hir().get_parent_item(e.hir_id).def_id
|
&& let parent = cx.tcx.hir().get_parent_item(e.hir_id).def_id
|
||||||
&& let Some(Node::Item(Item{kind: ItemKind::Fn(..), ..})) = cx.tcx.opt_hir_node_by_def_id(parent)
|
&& let Node::Item(Item{kind: ItemKind::Fn(..), ..}) = cx.tcx.hir_node_by_def_id(parent)
|
||||||
// If the next item up is a function we check if it is an entry point
|
// If the next item up is a function we check if it is an entry point
|
||||||
// and only then emit a linter warning
|
// and only then emit a linter warning
|
||||||
&& !is_entrypoint_fn(cx, parent.to_def_id())
|
&& !is_entrypoint_fn(cx, parent.to_def_id())
|
||||||
|
@ -92,7 +92,7 @@ fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty
|
|||||||
.expect("already checked this is adt")
|
.expect("already checked this is adt")
|
||||||
.did()
|
.did()
|
||||||
.as_local()
|
.as_local()
|
||||||
&& let Some(hir::Node::Item(item)) = cx.tcx.opt_hir_node_by_def_id(local_def_id)
|
&& let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(local_def_id)
|
||||||
&& let hir::ItemKind::Enum(ref def, _) = item.kind
|
&& let hir::ItemKind::Enum(ref def, _) = item.kind
|
||||||
{
|
{
|
||||||
let variants_size = AdtVariantInfo::new(cx, *adt, subst);
|
let variants_size = AdtVariantInfo::new(cx, *adt, subst);
|
||||||
|
@ -225,10 +225,9 @@ impl<'tcx> LateLintPass<'tcx> for LintPass {
|
|||||||
if let Some(adt_def) = cx.typeck_results().expr_ty_adjusted(recv).ty_adt_def()
|
if let Some(adt_def) = cx.typeck_results().expr_ty_adjusted(recv).ty_adt_def()
|
||||||
&& let Some(field) = adt_def.all_fields().find(|field| field.name == ident.name)
|
&& let Some(field) = adt_def.all_fields().find(|field| field.name == ident.name)
|
||||||
&& let Some(local_did) = field.did.as_local()
|
&& let Some(local_did) = field.did.as_local()
|
||||||
&& let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(local_did)
|
|
||||||
&& !cx.tcx.type_of(field.did).skip_binder().is_phantom_data()
|
&& !cx.tcx.type_of(field.did).skip_binder().is_phantom_data()
|
||||||
{
|
{
|
||||||
(hir_id, ident)
|
(cx.tcx.local_def_id_to_hir_id(local_did), ident)
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -220,7 +220,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingFieldsInDebug {
|
|||||||
&& let self_ty = cx.tcx.type_of(self_path_did).skip_binder().peel_refs()
|
&& let self_ty = cx.tcx.type_of(self_path_did).skip_binder().peel_refs()
|
||||||
&& let Some(self_adt) = self_ty.ty_adt_def()
|
&& let Some(self_adt) = self_ty.ty_adt_def()
|
||||||
&& let Some(self_def_id) = self_adt.did().as_local()
|
&& let Some(self_def_id) = self_adt.did().as_local()
|
||||||
&& let Some(Node::Item(self_item)) = cx.tcx.opt_hir_node_by_def_id(self_def_id)
|
&& let Node::Item(self_item) = cx.tcx.hir_node_by_def_id(self_def_id)
|
||||||
// NB: can't call cx.typeck_results() as we are not in a body
|
// NB: can't call cx.typeck_results() as we are not in a body
|
||||||
&& let typeck_results = cx.tcx.typeck_body(*body_id)
|
&& let typeck_results = cx.tcx.typeck_body(*body_id)
|
||||||
&& should_lint(cx, typeck_results, block)
|
&& should_lint(cx, typeck_results, block)
|
||||||
|
@ -112,10 +112,7 @@ fn check_closures<'tcx>(
|
|||||||
}
|
}
|
||||||
ctx.prev_bind = None;
|
ctx.prev_bind = None;
|
||||||
ctx.prev_move_to_closure.clear();
|
ctx.prev_move_to_closure.clear();
|
||||||
if let Some(body) = cx
|
if let Some(body) = associated_body(cx.tcx.hir_node_by_def_id(closure))
|
||||||
.tcx
|
|
||||||
.opt_hir_node_by_def_id(closure)
|
|
||||||
.and_then(associated_body)
|
|
||||||
.map(|(_, body_id)| hir.body(body_id))
|
.map(|(_, body_id)| hir.body(body_id))
|
||||||
{
|
{
|
||||||
euv::ExprUseVisitor::new(ctx, infcx, closure, cx.param_env, cx.typeck_results()).consume_body(body);
|
euv::ExprUseVisitor::new(ctx, infcx, closure, cx.param_env, cx.typeck_results()).consume_body(body);
|
||||||
|
@ -72,8 +72,7 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors {
|
|||||||
|
|
||||||
if let Some(self_def) = self_ty.ty_adt_def()
|
if let Some(self_def) = self_ty.ty_adt_def()
|
||||||
&& let Some(self_local_did) = self_def.did().as_local()
|
&& let Some(self_local_did) = self_def.did().as_local()
|
||||||
&& let self_id = cx.tcx.local_def_id_to_hir_id(self_local_did)
|
&& let Node::Item(x) = cx.tcx.hir_node_by_def_id(self_local_did)
|
||||||
&& let Node::Item(x) = cx.tcx.hir_node(self_id)
|
|
||||||
&& let type_name = x.ident.name.as_str().to_lowercase()
|
&& let type_name = x.ident.name.as_str().to_lowercase()
|
||||||
&& (impl_item.ident.name.as_str() == type_name
|
&& (impl_item.ident.name.as_str() == type_name
|
||||||
|| impl_item.ident.name.as_str().replace('_', "") == type_name)
|
|| impl_item.ident.name.as_str().replace('_', "") == type_name)
|
||||||
|
@ -95,7 +95,7 @@ impl SingleCallFn {
|
|||||||
/// to be considered.
|
/// to be considered.
|
||||||
fn is_valid_item_kind(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
|
fn is_valid_item_kind(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
|
||||||
matches!(
|
matches!(
|
||||||
cx.tcx.hir_node(cx.tcx.local_def_id_to_hir_id(def_id)),
|
cx.tcx.hir_node_by_def_id(def_id),
|
||||||
Node::Item(_) | Node::ImplItem(_) | Node::TraitItem(_)
|
Node::Item(_) | Node::ImplItem(_) | Node::TraitItem(_)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -321,7 +321,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
|
|||||||
_: Span,
|
_: Span,
|
||||||
def_id: LocalDefId,
|
def_id: LocalDefId,
|
||||||
) {
|
) {
|
||||||
let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx.tcx.opt_hir_node_by_def_id(
|
let is_in_trait_impl = if let hir::Node::Item(item) = cx.tcx.hir_node_by_def_id(
|
||||||
cx.tcx
|
cx.tcx
|
||||||
.hir()
|
.hir()
|
||||||
.get_parent_item(cx.tcx.local_def_id_to_hir_id(def_id))
|
.get_parent_item(cx.tcx.local_def_id_to_hir_id(def_id))
|
||||||
@ -366,9 +366,9 @@ impl<'tcx> LateLintPass<'tcx> for Types {
|
|||||||
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) {
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) {
|
||||||
match item.kind {
|
match item.kind {
|
||||||
ImplItemKind::Const(ty, _) => {
|
ImplItemKind::Const(ty, _) => {
|
||||||
let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx
|
let is_in_trait_impl = if let hir::Node::Item(item) = cx
|
||||||
.tcx
|
.tcx
|
||||||
.opt_hir_node_by_def_id(cx.tcx.hir().get_parent_item(item.hir_id()).def_id)
|
.hir_node_by_def_id(cx.tcx.hir().get_parent_item(item.hir_id()).def_id)
|
||||||
{
|
{
|
||||||
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
|
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
|
||||||
} else {
|
} else {
|
||||||
|
@ -74,7 +74,7 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
|
|||||||
fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool {
|
fn in_trait_impl(cx: &LateContext<'_>, hir_id: HirId) -> bool {
|
||||||
let parent_id = cx.tcx.hir().get_parent_item(hir_id);
|
let parent_id = cx.tcx.hir().get_parent_item(hir_id);
|
||||||
let second_parent_id = cx.tcx.hir().get_parent_item(parent_id.into()).def_id;
|
let second_parent_id = cx.tcx.hir().get_parent_item(parent_id.into()).def_id;
|
||||||
if let Some(Node::Item(item)) = cx.tcx.opt_hir_node_by_def_id(second_parent_id) {
|
if let Node::Item(item) = cx.tcx.hir_node_by_def_id(second_parent_id) {
|
||||||
if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind {
|
if let ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = item.kind {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -330,8 +330,7 @@ pub fn is_trait_method(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol)
|
|||||||
|
|
||||||
/// Checks if the `def_id` belongs to a function that is part of a trait impl.
|
/// Checks if the `def_id` belongs to a function that is part of a trait impl.
|
||||||
pub fn is_def_id_trait_method(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
|
pub fn is_def_id_trait_method(cx: &LateContext<'_>, def_id: LocalDefId) -> bool {
|
||||||
if let Some(hir_id) = cx.tcx.opt_local_def_id_to_hir_id(def_id)
|
if let Node::Item(item) = cx.tcx.parent_hir_node(cx.tcx.local_def_id_to_hir_id(def_id))
|
||||||
&& let Node::Item(item) = cx.tcx.parent_hir_node(hir_id)
|
|
||||||
&& let ItemKind::Impl(imp) = item.kind
|
&& let ItemKind::Impl(imp) = item.kind
|
||||||
{
|
{
|
||||||
imp.of_trait.is_some()
|
imp.of_trait.is_some()
|
||||||
@ -574,12 +573,12 @@ fn local_item_children_by_name(tcx: TyCtxt<'_>, local_id: LocalDefId, name: Symb
|
|||||||
let hir = tcx.hir();
|
let hir = tcx.hir();
|
||||||
|
|
||||||
let root_mod;
|
let root_mod;
|
||||||
let item_kind = match tcx.opt_hir_node_by_def_id(local_id) {
|
let item_kind = match tcx.hir_node_by_def_id(local_id) {
|
||||||
Some(Node::Crate(r#mod)) => {
|
Node::Crate(r#mod) => {
|
||||||
root_mod = ItemKind::Mod(r#mod);
|
root_mod = ItemKind::Mod(r#mod);
|
||||||
&root_mod
|
&root_mod
|
||||||
},
|
},
|
||||||
Some(Node::Item(item)) => &item.kind,
|
Node::Item(item) => &item.kind,
|
||||||
_ => return Vec::new(),
|
_ => return Vec::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1254,12 +1253,10 @@ pub fn is_in_panic_handler(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
|
|||||||
/// Gets the name of the item the expression is in, if available.
|
/// Gets the name of the item the expression is in, if available.
|
||||||
pub fn get_item_name(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Symbol> {
|
pub fn get_item_name(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Symbol> {
|
||||||
let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id).def_id;
|
let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id).def_id;
|
||||||
match cx.tcx.opt_hir_node_by_def_id(parent_id) {
|
match cx.tcx.hir_node_by_def_id(parent_id) {
|
||||||
Some(
|
Node::Item(Item { ident, .. })
|
||||||
Node::Item(Item { ident, .. })
|
| Node::TraitItem(TraitItem { ident, .. })
|
||||||
| Node::TraitItem(TraitItem { ident, .. })
|
| Node::ImplItem(ImplItem { ident, .. }) => Some(ident.name),
|
||||||
| Node::ImplItem(ImplItem { ident, .. }),
|
|
||||||
) => Some(ident.name),
|
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2667,11 +2664,10 @@ impl<'tcx> ExprUseNode<'tcx> {
|
|||||||
.and(Binder::dummy(cx.tcx.type_of(id).instantiate_identity())),
|
.and(Binder::dummy(cx.tcx.type_of(id).instantiate_identity())),
|
||||||
)),
|
)),
|
||||||
Self::Return(id) => {
|
Self::Return(id) => {
|
||||||
let hir_id = cx.tcx.local_def_id_to_hir_id(id.def_id);
|
|
||||||
if let Node::Expr(Expr {
|
if let Node::Expr(Expr {
|
||||||
kind: ExprKind::Closure(c),
|
kind: ExprKind::Closure(c),
|
||||||
..
|
..
|
||||||
}) = cx.tcx.hir_node(hir_id)
|
}) = cx.tcx.hir_node_by_def_id(id.def_id)
|
||||||
{
|
{
|
||||||
match c.fn_decl.output {
|
match c.fn_decl.output {
|
||||||
FnRetTy::DefaultReturn(_) => None,
|
FnRetTy::DefaultReturn(_) => None,
|
||||||
|
Loading…
Reference in New Issue
Block a user