Skip to content

Commit c01597a

Browse files
committed
init by copying ty_search_pat
1 parent 32a216e commit c01597a

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;
@@ -408,6 +409,47 @@ fn ty_search_pat(ty: &Ty<'_>) -> (Pat, Pat) {
408409
}
409410
}
410411

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

0 commit comments

Comments
 (0)