Add more types to is_from_proc_macro

This commit is contained in:
Jason Newcomb 2024-06-16 20:15:09 -04:00
parent 9f5d60f160
commit e18b310874
3 changed files with 44 additions and 44 deletions

View file

@ -57,7 +57,7 @@ impl LateLintPass<'_> for AllowAttribute {
&& let AttrStyle::Outer = attr.style && let AttrStyle::Outer = attr.style
&& let Some(ident) = attr.ident() && let Some(ident) = attr.ident()
&& ident.name == rustc_span::symbol::sym::allow && ident.name == rustc_span::symbol::sym::allow
&& !is_from_proc_macro(cx, &attr) && !is_from_proc_macro(cx, attr)
{ {
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,

View file

@ -22,7 +22,7 @@ pub(super) fn check<'cx>(cx: &LateContext<'cx>, name: Symbol, items: &[NestedMet
} }
// Check if the attribute is in an external macro and therefore out of the developer's control // Check if the attribute is in an external macro and therefore out of the developer's control
if in_external_macro(cx.sess(), attr.span) || is_from_proc_macro(cx, &attr) { if in_external_macro(cx.sess(), attr.span) || is_from_proc_macro(cx, attr) {
return; return;
} }

View file

@ -18,8 +18,8 @@ use rustc_ast::AttrStyle;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{ use rustc_hir::{
Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, FnRetTy, HirId, Impl, Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, FnRetTy, HirId, Impl,
ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, MutTy, Node, QPath, Safety, TraitItem, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, Lit, LoopSource, MatchSource, MutTy, Node, Path, QPath, Safety,
TraitItemKind, Ty, TyKind, UnOp, UnsafeSource, Variant, VariantData, YieldSource, TraitItem, TraitItemKind, Ty, TyKind, UnOp, UnsafeSource, Variant, VariantData, YieldSource,
}; };
use rustc_lint::{LateContext, LintContext}; use rustc_lint::{LateContext, LintContext};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
@ -121,6 +121,26 @@ fn qpath_search_pat(path: &QPath<'_>) -> (Pat, Pat) {
} }
} }
fn path_search_pat(path: &Path<'_>) -> (Pat, Pat) {
let (head, tail) = match path.segments {
[head, .., tail] => (head, tail),
[p] => (p, p),
[] => return (Pat::Str(""), Pat::Str("")),
};
(
if head.ident.name == kw::PathRoot {
Pat::Str("::")
} else {
Pat::Sym(head.ident.name)
},
if tail.args.is_some() {
Pat::Str(">")
} else {
Pat::Sym(tail.ident.name)
},
)
}
/// Get the search patterns to use for the given expression /// Get the search patterns to use for the given expression
fn expr_search_pat(tcx: TyCtxt<'_>, e: &Expr<'_>) -> (Pat, Pat) { fn expr_search_pat(tcx: TyCtxt<'_>, e: &Expr<'_>) -> (Pat, Pat) {
match e.kind { match e.kind {
@ -355,19 +375,21 @@ fn ty_search_pat(ty: &Ty<'_>) -> (Pat, Pat) {
} }
} }
fn ident_search_pat(ident: Ident) -> (Pat, Pat) {
(Pat::Sym(ident.name), Pat::Sym(ident.name))
}
pub trait WithSearchPat<'cx> { pub trait WithSearchPat<'cx> {
type Context: LintContext; type Context: LintContext;
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat); fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
fn span(&self) -> Span; fn span(&self) -> Span;
} }
macro_rules! impl_with_search_pat { macro_rules! impl_with_search_pat {
($cx:ident: $ty:ident with $fn:ident $(($tcx:ident))?) => { (($cx_ident:ident: $cx_ty:ident<$cx_lt:lifetime>, $self:tt: $ty:ty) => $fn:ident($($args:tt)*)) => {
impl<'cx> WithSearchPat<'cx> for $ty<'cx> { impl<$cx_lt> WithSearchPat<$cx_lt> for $ty {
type Context = $cx<'cx>; type Context = $cx_ty<$cx_lt>;
#[allow(unused_variables)] fn search_pat(&$self, $cx_ident: &Self::Context) -> (Pat, Pat) {
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat) { $fn($($args)*)
$(let $tcx = cx.tcx;)?
$fn($($tcx,)? self)
} }
fn span(&self) -> Span { fn span(&self) -> Span {
self.span self.span
@ -375,13 +397,17 @@ macro_rules! impl_with_search_pat {
} }
}; };
} }
impl_with_search_pat!(LateContext: Expr with expr_search_pat(tcx)); impl_with_search_pat!((cx: LateContext<'tcx>, self: Expr<'tcx>) => expr_search_pat(cx.tcx, self));
impl_with_search_pat!(LateContext: Item with item_search_pat); impl_with_search_pat!((_cx: LateContext<'tcx>, self: Item<'_>) => item_search_pat(self));
impl_with_search_pat!(LateContext: TraitItem with trait_item_search_pat); impl_with_search_pat!((_cx: LateContext<'tcx>, self: TraitItem<'_>) => trait_item_search_pat(self));
impl_with_search_pat!(LateContext: ImplItem with impl_item_search_pat); impl_with_search_pat!((_cx: LateContext<'tcx>, self: ImplItem<'_>) => impl_item_search_pat(self));
impl_with_search_pat!(LateContext: FieldDef with field_def_search_pat); impl_with_search_pat!((_cx: LateContext<'tcx>, self: FieldDef<'_>) => field_def_search_pat(self));
impl_with_search_pat!(LateContext: Variant with variant_search_pat); impl_with_search_pat!((_cx: LateContext<'tcx>, self: Variant<'_>) => variant_search_pat(self));
impl_with_search_pat!(LateContext: Ty with ty_search_pat); impl_with_search_pat!((_cx: LateContext<'tcx>, self: Ty<'_>) => ty_search_pat(self));
impl_with_search_pat!((_cx: LateContext<'tcx>, self: Attribute) => attr_search_pat(self));
impl_with_search_pat!((_cx: LateContext<'tcx>, self: Ident) => ident_search_pat(*self));
impl_with_search_pat!((_cx: LateContext<'tcx>, self: Lit) => lit_search_pat(&self.node));
impl_with_search_pat!((_cx: LateContext<'tcx>, self: Path<'_>) => path_search_pat(self));
impl<'cx> WithSearchPat<'cx> for (&FnKind<'cx>, &Body<'cx>, HirId, Span) { impl<'cx> WithSearchPat<'cx> for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
type Context = LateContext<'cx>; type Context = LateContext<'cx>;
@ -395,32 +421,6 @@ impl<'cx> WithSearchPat<'cx> for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
} }
} }
// `Attribute` does not have the `hir` associated lifetime, so we cannot use the macro
impl<'cx> WithSearchPat<'cx> for &'cx Attribute {
type Context = LateContext<'cx>;
fn search_pat(&self, _cx: &Self::Context) -> (Pat, Pat) {
attr_search_pat(self)
}
fn span(&self) -> Span {
self.span
}
}
// `Ident` does not have the `hir` associated lifetime, so we cannot use the macro
impl<'cx> WithSearchPat<'cx> for Ident {
type Context = LateContext<'cx>;
fn search_pat(&self, _cx: &Self::Context) -> (Pat, Pat) {
(Pat::Sym(self.name), Pat::Sym(self.name))
}
fn span(&self) -> Span {
self.span
}
}
/// Checks if the item likely came from a proc-macro. /// Checks if the item likely came from a proc-macro.
/// ///
/// This should be called after `in_external_macro` and the initial pattern matching of the ast as /// This should be called after `in_external_macro` and the initial pattern matching of the ast as