1
Fork 0

Take a LocalDefId in hir::Visitor::visit_fn.

This commit is contained in:
Camille GILLOT 2023-01-22 18:00:33 +00:00
parent 252741673b
commit 3175d03d3b
52 changed files with 235 additions and 211 deletions

View file

@ -583,12 +583,13 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
// If the method is an impl for a trait, don't doc.
if method_context(cx, impl_item.hir_id()) == MethodLateContext::TraitImpl {
let context = method_context(cx, impl_item.owner_id.def_id);
if context == MethodLateContext::TraitImpl {
return;
}
// If the method is an impl for an item with docs_hidden, don't doc.
if method_context(cx, impl_item.hir_id()) == MethodLateContext::PlainImpl {
if context == MethodLateContext::PlainImpl {
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
let impl_ty = cx.tcx.type_of(parent);
let outerdef = match impl_ty.kind() {
@ -1296,19 +1297,18 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
_: &'tcx FnDecl<'_>,
_: &'tcx Body<'_>,
span: Span,
hir_id: HirId,
def_id: LocalDefId,
) {
if fn_kind.asyncness() == IsAsync::Async
&& !cx.tcx.features().closure_track_caller
&& let attrs = cx.tcx.hir().attrs(hir_id)
// Now, check if the function has the `#[track_caller]` attribute
&& let Some(attr) = attrs.iter().find(|attr| attr.has_name(sym::track_caller))
{
cx.emit_spanned_lint(UNGATED_ASYNC_FN_TRACK_CALLER, attr.span, BuiltinUngatedAsyncFnTrackCaller {
label: span,
parse_sess: &cx.tcx.sess.parse_sess,
});
}
&& let Some(attr) = cx.tcx.get_attr(def_id.to_def_id(), sym::track_caller)
{
cx.emit_spanned_lint(UNGATED_ASYNC_FN_TRACK_CALLER, attr.span, BuiltinUngatedAsyncFnTrackCaller {
label: span,
parse_sess: &cx.tcx.sess.parse_sess,
});
}
}
}

View file

@ -180,7 +180,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
decl: &'tcx hir::FnDecl<'tcx>,
body_id: hir::BodyId,
span: Span,
id: hir::HirId,
id: LocalDefId,
) {
// Wrap in typeck results here, not just in visit_nested_body,
// in order for `check_fn` to be able to use them.

View file

@ -10,8 +10,9 @@ use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::FnKind;
use rustc_hir::{GenericParamKind, PatKind};
use rustc_middle::ty;
use rustc_span::symbol::sym;
use rustc_span::{symbol::Ident, BytePos, Span};
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::{sym, Ident};
use rustc_span::{BytePos, Span};
use rustc_target::spec::abi::Abi;
#[derive(PartialEq)]
@ -21,8 +22,7 @@ pub enum MethodLateContext {
PlainImpl,
}
pub fn method_context(cx: &LateContext<'_>, id: hir::HirId) -> MethodLateContext {
let def_id = cx.tcx.hir().local_def_id(id);
pub fn method_context(cx: &LateContext<'_>, def_id: LocalDefId) -> MethodLateContext {
let item = cx.tcx.associated_item(def_id);
match item.container {
ty::TraitContainer => MethodLateContext::TraitAutoImpl,
@ -379,13 +379,13 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
_: &hir::FnDecl<'_>,
_: &hir::Body<'_>,
_: Span,
id: hir::HirId,
id: LocalDefId,
) {
let attrs = cx.tcx.hir().attrs(id);
match &fk {
FnKind::Method(ident, sig, ..) => match method_context(cx, id) {
MethodLateContext::PlainImpl => {
if sig.header.abi != Abi::Rust && cx.sess().contains_name(attrs, sym::no_mangle)
if sig.header.abi != Abi::Rust
&& cx.tcx.has_attr(id.to_def_id(), sym::no_mangle)
{
return;
}
@ -398,7 +398,7 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
},
FnKind::ItemFn(ident, _, header) => {
// Skip foreign-ABI #[no_mangle] functions (Issue #31924)
if header.abi != Abi::Rust && cx.sess().contains_name(attrs, sym::no_mangle) {
if header.abi != Abi::Rust && cx.tcx.has_attr(id.to_def_id(), sym::no_mangle) {
return;
}
self.check_snake_case(cx, "function", ident);

View file

@ -4,6 +4,7 @@ use rustc_ast as ast;
use rustc_hir as hir;
use rustc_session::lint::builtin::HardwiredLints;
use rustc_session::lint::LintPass;
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::Ident;
use rustc_span::Span;
@ -36,7 +37,7 @@ macro_rules! late_lint_methods {
b: &'tcx hir::FnDecl<'tcx>,
c: &'tcx hir::Body<'tcx>,
d: Span,
e: hir::HirId);
e: LocalDefId);
fn check_trait_item(a: &'tcx hir::TraitItem<'tcx>);
fn check_impl_item(a: &'tcx hir::ImplItem<'tcx>);
fn check_impl_item_post(a: &'tcx hir::ImplItem<'tcx>);

View file

@ -14,6 +14,7 @@ use rustc_hir::{is_range_literal, Expr, ExprKind, Node};
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, SizeSkeleton};
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map;
use rustc_span::symbol::sym;
use rustc_span::{Span, Symbol};
@ -1224,8 +1225,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
}
}
fn check_foreign_fn(&mut self, id: hir::HirId, decl: &hir::FnDecl<'_>) {
let def_id = self.cx.tcx.hir().local_def_id(id);
fn check_foreign_fn(&mut self, def_id: LocalDefId, decl: &hir::FnDecl<'_>) {
let sig = self.cx.tcx.fn_sig(def_id).subst_identity();
let sig = self.cx.tcx.erase_late_bound_regions(sig);
@ -1261,7 +1261,7 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDeclarations {
if !vis.is_internal_abi(abi) {
match it.kind {
hir::ForeignItemKind::Fn(ref decl, _, _) => {
vis.check_foreign_fn(it.hir_id(), decl);
vis.check_foreign_fn(it.owner_id.def_id, decl);
}
hir::ForeignItemKind::Static(ref ty, _) => {
vis.check_foreign_static(it.hir_id(), ty.span);
@ -1280,7 +1280,7 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
decl: &'tcx hir::FnDecl<'_>,
_: &'tcx hir::Body<'_>,
_: Span,
hir_id: hir::HirId,
id: LocalDefId,
) {
use hir::intravisit::FnKind;
@ -1292,7 +1292,7 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Definition };
if !vis.is_internal_abi(abi) {
vis.check_foreign_fn(hir_id, decl);
vis.check_foreign_fn(id, decl);
}
}
}