-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(use_self): don't early-return if the outer type has no lifetimes #15611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -513,25 +513,31 @@ pub fn walk_ptrs_ty_depth(ty: Ty<'_>) -> (Ty<'_>, usize) { | |
| inner(ty, 0) | ||
| } | ||
|
|
||
| /// Returns `true` if types `a` and `b` are same types having same `Const` generic args, | ||
| /// otherwise returns `false` | ||
| pub fn same_type_and_consts<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool { | ||
| /// Checks whether `a` and `b` are same types having same `Const` generic args, but ignores | ||
| /// lifetimes. | ||
| /// | ||
| /// For example, the function would return `true` for | ||
| /// - `u32` and `u32` | ||
| /// - `[u8; N]` and `[u8; M]`, if `N=M` | ||
| /// - `Option<T>` and `Option<U>`, if `same_type_modulo_regions(T, U)` holds | ||
| /// - `&'a str` and `&'b str` | ||
| /// | ||
| /// and `false` for: | ||
| /// - `Result<u32, String>` and `Result<usize, String>` | ||
| pub fn same_type_modulo_regions<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool { | ||
| match (&a.kind(), &b.kind()) { | ||
| (&ty::Adt(did_a, args_a), &ty::Adt(did_b, args_b)) => { | ||
| if did_a != did_b { | ||
| return false; | ||
| } | ||
|
|
||
| args_a | ||
| .iter() | ||
| .zip(args_b.iter()) | ||
| .all(|(arg_a, arg_b)| match (arg_a.kind(), arg_b.kind()) { | ||
| (GenericArgKind::Const(inner_a), GenericArgKind::Const(inner_b)) => inner_a == inner_b, | ||
| (GenericArgKind::Type(type_a), GenericArgKind::Type(type_b)) => { | ||
| same_type_and_consts(type_a, type_b) | ||
| }, | ||
| _ => true, | ||
| }) | ||
| iter::zip(*args_a, *args_b).all(|(arg_a, arg_b)| match (arg_a.kind(), arg_b.kind()) { | ||
| (GenericArgKind::Const(inner_a), GenericArgKind::Const(inner_b)) => inner_a == inner_b, | ||
| (GenericArgKind::Type(type_a), GenericArgKind::Type(type_b)) => { | ||
| same_type_modulo_regions(type_a, type_b) | ||
| }, | ||
| _ => true, | ||
| }) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this predates your patch, but shouldn't this function name contain I'd be curious to know whether this makes sense to ignore the lifetimes in all use cases of this function though, but at least this would make the intention clearer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
As #15610 just pointed out, a lot of things in Clippy tend to ignore lifetimes it seems.. I guess lifetimes being ignored is implied by contrast ("only takes into account the type and const generics, therefore ignores lifetimes"), but a more explicit name could not hurt. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a few "doctests" for some extra clarity (I often wish more util functions included these, to help understand what the function does at a glance) -- let me know what you think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean "examples", not "doctests", right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
EDIT: Ah, sorry, misread your comment. Yes, that's why put "doctests" in quotes -- they aren't actual doctests after all. The rest of my comment still holds though^^ |
||
| }, | ||
| _ => a == b, | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, not really --
NandMwould beConsts, notTys. I could add an example liketo illustrate that case though