Skip to content

Commit 73d04ba

Browse files
committed
fix Path
1 parent 2de81f5 commit 73d04ba

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

clippy_utils/src/check_proc_macro.rs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use rustc_abi::ExternAbi;
1616
use rustc_ast as ast;
1717
use rustc_ast::AttrStyle;
18-
use rustc_ast::ast::{AttrKind, Attribute, IntTy, LitIntType, LitKind, StrStyle, TraitObjectSyntax, UintTy};
18+
use rustc_ast::ast::{
19+
AttrKind, Attribute, GenericArgs, IntTy, LitIntType, LitKind, StrStyle, TraitObjectSyntax, UintTy,
20+
};
1921
use rustc_ast::token::CommentKind;
2022
use rustc_hir::intravisit::FnKind;
2123
use rustc_hir::{
@@ -445,7 +447,52 @@ fn ast_ty_search_pat(ty: &ast::Ty) -> (Pat, Pat) {
445447
TyKind::Tup([ty]) => ast_ty_search_pat(ty),
446448
TyKind::Tup([head, .., tail]) => (ast_ty_search_pat(head).0, ast_ty_search_pat(tail).1),
447449
TyKind::OpaqueDef(..) => (Pat::Str("impl"), Pat::Str("")),
448-
TyKind::Path(qpath) => qpath_search_pat(&qpath),
450+
TyKind::Path(qself_path, path) => {
451+
let start = if qself_path.is_some() {
452+
Pat::Str("<")
453+
} else if let Some(first) = path.segments.first() {
454+
ident_search_pat(first.ident).0
455+
} else {
456+
// this shouldn't be possible, but sure
457+
Pat::Str("")
458+
};
459+
let end = if let Some(last) = path.segments.last() {
460+
match last.args.as_deref() {
461+
// last `>` in `std::foo::Bar<T>`
462+
Some(GenericArgs::AngleBracketed(_)) => Pat::Str(">"),
463+
Some(GenericArgs::Parenthesized(par_args)) => match &par_args.output {
464+
FnRetTy::Default(_) => {
465+
if let Some(last) = par_args.inputs.last() {
466+
// `B` in `(A, B)` -- `)` gets stripped
467+
ast_ty_search_pat(last).1
468+
} else {
469+
// `(` in `()` -- `)` gets stripped
470+
Pat::Str("(")
471+
}
472+
},
473+
// `C` in `(A, B) -> C`
474+
FnRetTy::Ty(ty) => ast_ty_search_pat(ty).1,
475+
},
476+
// last `..` in `(..)` -- `)` gets stripped
477+
Some(GenericArgs::ParenthesizedElided(_)) => Pat::Str(".."),
478+
// `bar` in `std::foo::bar`
479+
None => ident_search_pat(last.ident).1,
480+
}
481+
} else {
482+
// this shouldn't be possible, but sure
483+
#[allow(
484+
clippy::collapsible_else_if,
485+
reason = "we want to keep these cases together, since they are both impossible"
486+
)]
487+
if qself_path.is_some() {
488+
// last `>` in `<Vec as IntoIterator>`
489+
Pat::Str(">")
490+
} else {
491+
Pat::Str("")
492+
}
493+
};
494+
(start, end)
495+
},
449496
TyKind::Infer => (Pat::Str("_"), Pat::Str("_")),
450497
TyKind::TraitObject(_, tagged_ptr) if let TraitObjectSyntax::Dyn = tagged_ptr.tag() => {
451498
(Pat::Str("dyn"), Pat::Str(""))

0 commit comments

Comments
 (0)