about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCentri3 <114838443+Centri3@users.noreply.github.com>2023-06-06 18:05:15 -0500
committerCentri3 <114838443+Centri3@users.noreply.github.com>2023-06-06 18:05:15 -0500
commite97f190a9dab40aa1fe2e5ec72af5d52f5fcf1fd (patch)
tree9b1bf2853d91ece62491a1ec60fa58f6e4438c02
parentf68ee79864fb51232fb186fda6bf218be50c7e4a (diff)
downloadrust-e97f190a9dab40aa1fe2e5ec72af5d52f5fcf1fd.tar.gz
rust-e97f190a9dab40aa1fe2e5ec72af5d52f5fcf1fd.zip
`ty_search_pat`
-rw-r--r--clippy_utils/src/check_proc_macro.rs42
1 files changed, 40 insertions, 2 deletions
diff --git a/clippy_utils/src/check_proc_macro.rs b/clippy_utils/src/check_proc_macro.rs
index 7e954898460..7f79b8f87dd 100644
--- a/clippy_utils/src/check_proc_macro.rs
+++ b/clippy_utils/src/check_proc_macro.rs
@@ -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>;