ty_search_pat

This commit is contained in:
Centri3 2023-06-06 18:05:15 -05:00
parent f68ee79864
commit e97f190a9d

View file

@ -19,8 +19,8 @@ use rustc_ast::{
};
use rustc_hir::{
intravisit::FnKind, Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, HirId,
Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, Node, QPath, TraitItem,
TraitItemKind, UnOp, UnsafeSource, Unsafety, Variant, VariantData, YieldSource,
Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, MutTy, Node, QPath, TraitItem,
TraitItemKind, Ty, TyKind, UnOp, UnsafeSource, Unsafety, Variant, VariantData, YieldSource,
};
use rustc_lint::{LateContext, LintContext};
use rustc_middle::ty::TyCtxt;
@ -319,6 +319,43 @@ fn attr_search_pat(attr: &Attribute) -> (Pat, Pat) {
}
}
// TODO: Waiting on `ty_search_pat`.
// fn where_pred_search_pat(where_pred: &WherePredicate<'_>) -> (Pat, Pat) {
// match where_pred {
// WherePredicate::BoundPredicate(bound) => {
// todo!();
// },
// WherePredicate::RegionPredicate(region) => {
//
// },
// WherePredicate::EqPredicate(..) => unimplemented!(),
// }
// }
fn ty_search_pat(ty: &Ty<'_>) -> (Pat, Pat) {
match ty.kind {
TyKind::Slice(..) | TyKind::Array(..) => (Pat::Str("["), Pat::Str("]")),
TyKind::Ptr(MutTy { mutbl, ty }) => (
if mutbl.is_mut() {
Pat::Str("*const")
} else {
Pat::Str("*mut")
},
ty_search_pat(ty).1,
),
TyKind::Ref(_, MutTy { ty, .. }) => (Pat::Str("&"), ty_search_pat(ty).1),
TyKind::BareFn(bare_fn) => (
Pat::OwnedStr(format!("{}{} fn", bare_fn.unsafety.prefix_str(), bare_fn.abi.name())),
ty_search_pat(ty).1,
),
TyKind::Never => (Pat::Str("!"), Pat::Str("")),
TyKind::Tup(..) => (Pat::Str("("), Pat::Str(")")),
TyKind::OpaqueDef(..) => (Pat::Str("impl"), Pat::Str("")),
// NOTE: This is missing `TraitObject` and `Path` here. It always return true then.
_ => (Pat::Str(""), Pat::Str("")),
}
}
pub trait WithSearchPat {
type Context: LintContext;
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
@ -345,6 +382,7 @@ impl_with_search_pat!(LateContext: TraitItem with trait_item_search_pat);
impl_with_search_pat!(LateContext: ImplItem with impl_item_search_pat);
impl_with_search_pat!(LateContext: FieldDef with field_def_search_pat);
impl_with_search_pat!(LateContext: Variant with variant_search_pat);
impl_with_search_pat!(LateContext: Ty with ty_search_pat);
impl<'cx> WithSearchPat for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
type Context = LateContext<'cx>;