Skip to content

Commit cd2d2a0

Browse files
committed
init by copying ty_search_pat
1 parent 8779679 commit cd2d2a0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

clippy_utils/src/check_proc_macro.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! if the span is not from a `macro_rules` based macro.
1414
1515
use rustc_abi::ExternAbi;
16+
use rustc_ast as ast;
1617
use rustc_ast::AttrStyle;
1718
use rustc_ast::ast::{AttrKind, Attribute, IntTy, LitIntType, LitKind, StrStyle, TraitObjectSyntax, UintTy};
1819
use rustc_ast::token::CommentKind;
@@ -411,6 +412,47 @@ fn ty_search_pat(ty: &Ty<'_>) -> (Pat, Pat) {
411412
}
412413
}
413414

415+
fn ast_ty_search_pat(ty: &ast::Ty) -> (Pat, Pat) {
416+
match ty.kind {
417+
TyKind::Slice(..) | TyKind::Array(..) => (Pat::Str("["), Pat::Str("]")),
418+
TyKind::Ptr(MutTy { ty, .. }) => (Pat::Str("*"), ty_search_pat(ty).1),
419+
TyKind::Ref(_, MutTy { ty, .. }) => (Pat::Str("&"), ty_search_pat(ty).1),
420+
TyKind::FnPtr(fn_ptr) => (
421+
if fn_ptr.safety.is_unsafe() {
422+
Pat::Str("unsafe")
423+
} else if fn_ptr.abi != ExternAbi::Rust {
424+
Pat::Str("extern")
425+
} else {
426+
Pat::MultiStr(&["fn", "extern"])
427+
},
428+
match fn_ptr.decl.output {
429+
FnRetTy::DefaultReturn(_) => {
430+
if let [.., ty] = fn_ptr.decl.inputs {
431+
ty_search_pat(ty).1
432+
} else {
433+
Pat::Str("(")
434+
}
435+
},
436+
FnRetTy::Return(ty) => ty_search_pat(ty).1,
437+
},
438+
),
439+
TyKind::Never => (Pat::Str("!"), Pat::Str("!")),
440+
// Parenthesis are trimmed from the text before the search patterns are matched.
441+
// See: `span_matches_pat`
442+
TyKind::Tup([]) => (Pat::Str(")"), Pat::Str("(")),
443+
TyKind::Tup([ty]) => ty_search_pat(ty),
444+
TyKind::Tup([head, .., tail]) => (ty_search_pat(head).0, ty_search_pat(tail).1),
445+
TyKind::OpaqueDef(..) => (Pat::Str("impl"), Pat::Str("")),
446+
TyKind::Path(qpath) => qpath_search_pat(&qpath),
447+
TyKind::Infer(()) => (Pat::Str("_"), Pat::Str("_")),
448+
TyKind::TraitObject(_, tagged_ptr) if let TraitObjectSyntax::Dyn = tagged_ptr.tag() => {
449+
(Pat::Str("dyn"), Pat::Str(""))
450+
},
451+
// NOTE: `TraitObject` is incomplete. It will always return true then.
452+
_ => (Pat::Str(""), Pat::Str("")),
453+
}
454+
}
455+
414456
fn ident_search_pat(ident: Ident) -> (Pat, Pat) {
415457
(Pat::Sym(ident.name), Pat::Sym(ident.name))
416458
}

0 commit comments

Comments
 (0)