Skip to content

Commit e97f190

Browse files
committed
ty_search_pat
1 parent f68ee79 commit e97f190

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

clippy_utils/src/check_proc_macro.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use rustc_ast::{
1919
};
2020
use rustc_hir::{
2121
intravisit::FnKind, Block, BlockCheckMode, Body, Closure, Destination, Expr, ExprKind, FieldDef, FnHeader, HirId,
22-
Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, Node, QPath, TraitItem,
23-
TraitItemKind, UnOp, UnsafeSource, Unsafety, Variant, VariantData, YieldSource,
22+
Impl, ImplItem, ImplItemKind, IsAuto, Item, ItemKind, LoopSource, MatchSource, MutTy, Node, QPath, TraitItem,
23+
TraitItemKind, Ty, TyKind, UnOp, UnsafeSource, Unsafety, Variant, VariantData, YieldSource,
2424
};
2525
use rustc_lint::{LateContext, LintContext};
2626
use rustc_middle::ty::TyCtxt;
@@ -319,6 +319,43 @@ fn attr_search_pat(attr: &Attribute) -> (Pat, Pat) {
319319
}
320320
}
321321

322+
// TODO: Waiting on `ty_search_pat`.
323+
// fn where_pred_search_pat(where_pred: &WherePredicate<'_>) -> (Pat, Pat) {
324+
// match where_pred {
325+
// WherePredicate::BoundPredicate(bound) => {
326+
// todo!();
327+
// },
328+
// WherePredicate::RegionPredicate(region) => {
329+
//
330+
// },
331+
// WherePredicate::EqPredicate(..) => unimplemented!(),
332+
// }
333+
// }
334+
335+
fn ty_search_pat(ty: &Ty<'_>) -> (Pat, Pat) {
336+
match ty.kind {
337+
TyKind::Slice(..) | TyKind::Array(..) => (Pat::Str("["), Pat::Str("]")),
338+
TyKind::Ptr(MutTy { mutbl, ty }) => (
339+
if mutbl.is_mut() {
340+
Pat::Str("*const")
341+
} else {
342+
Pat::Str("*mut")
343+
},
344+
ty_search_pat(ty).1,
345+
),
346+
TyKind::Ref(_, MutTy { ty, .. }) => (Pat::Str("&"), ty_search_pat(ty).1),
347+
TyKind::BareFn(bare_fn) => (
348+
Pat::OwnedStr(format!("{}{} fn", bare_fn.unsafety.prefix_str(), bare_fn.abi.name())),
349+
ty_search_pat(ty).1,
350+
),
351+
TyKind::Never => (Pat::Str("!"), Pat::Str("")),
352+
TyKind::Tup(..) => (Pat::Str("("), Pat::Str(")")),
353+
TyKind::OpaqueDef(..) => (Pat::Str("impl"), Pat::Str("")),
354+
// NOTE: This is missing `TraitObject` and `Path` here. It always return true then.
355+
_ => (Pat::Str(""), Pat::Str("")),
356+
}
357+
}
358+
322359
pub trait WithSearchPat {
323360
type Context: LintContext;
324361
fn search_pat(&self, cx: &Self::Context) -> (Pat, Pat);
@@ -345,6 +382,7 @@ impl_with_search_pat!(LateContext: TraitItem with trait_item_search_pat);
345382
impl_with_search_pat!(LateContext: ImplItem with impl_item_search_pat);
346383
impl_with_search_pat!(LateContext: FieldDef with field_def_search_pat);
347384
impl_with_search_pat!(LateContext: Variant with variant_search_pat);
385+
impl_with_search_pat!(LateContext: Ty with ty_search_pat);
348386

349387
impl<'cx> WithSearchPat for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
350388
type Context = LateContext<'cx>;

0 commit comments

Comments
 (0)