Skip to content

Commit 6812aa3

Browse files
committed
fix(use_self): descend into type's children when looking for lifetimes
This unfortunately breaks another test case -- it had only worked because when we checked `!has_lifetime` on `S2<S<'a>>`, we only looked for generic lifetime params on `S2`, forgetting to descend into `S`. One thing that makes this a bit less sad is that this particular test case doesn't come from a reported issue, but rather was added as a miscellaneous check during a kind of unrelated PR.
1 parent 4ae4f67 commit 6812aa3

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

clippy_lints/src/use_self.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,12 @@ fn same_lifetimes<'tcx>(a: MiddleTy<'tcx>, b: MiddleTy<'tcx>) -> bool {
323323
fn has_lifetime(ty: MiddleTy<'_>) -> bool {
324324
use rustc_middle::ty::{Adt, GenericArgKind};
325325
match ty.kind() {
326-
Adt(_, args) => args
327-
.iter()
326+
Adt(_, args) => args.iter().any(|arg| match arg.kind() {
328327
// TODO: Handle inferred lifetimes
329-
.any(|arg| matches!(arg.kind(), GenericArgKind::Lifetime(..))),
328+
GenericArgKind::Lifetime(..) => true,
329+
GenericArgKind::Type(ty) => has_lifetime(ty),
330+
_ => false,
331+
}),
330332
_ => false,
331333
}
332334
}

tests/ui/use_self.fixed

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,8 @@ mod issue7206 {
530530

531531
impl<'a> S2<S<'a>> {
532532
fn new_again() -> Self {
533-
Self::new()
534-
//~^ use_self
533+
S2::new()
534+
// FIXME: ^Broken by PR #15611
535535
}
536536
}
537537
}
@@ -755,3 +755,17 @@ mod crash_check_13128 {
755755
}
756756
}
757757
}
758+
759+
mod issue_13277 {
760+
trait Foo {
761+
type Item<'foo>;
762+
}
763+
struct Bar<'b> {
764+
content: &'b str,
765+
}
766+
impl<'b> Foo for Option<Bar<'b>> {
767+
// when checking whether `Option<Bar<'foo>>` has a lifetime, check not only the outer
768+
// `Option<T>`, but also the inner `Bar<'foo>`
769+
type Item<'foo> = Option<Bar<'foo>>;
770+
}
771+
}

tests/ui/use_self.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ mod issue7206 {
531531
impl<'a> S2<S<'a>> {
532532
fn new_again() -> Self {
533533
S2::new()
534-
//~^ use_self
534+
// FIXME: ^Broken by PR #15611
535535
}
536536
}
537537
}
@@ -755,3 +755,17 @@ mod crash_check_13128 {
755755
}
756756
}
757757
}
758+
759+
mod issue_13277 {
760+
trait Foo {
761+
type Item<'foo>;
762+
}
763+
struct Bar<'b> {
764+
content: &'b str,
765+
}
766+
impl<'b> Foo for Option<Bar<'b>> {
767+
// when checking whether `Option<Bar<'foo>>` has a lifetime, check not only the outer
768+
// `Option<T>`, but also the inner `Bar<'foo>`
769+
type Item<'foo> = Option<Bar<'foo>>;
770+
}
771+
}

tests/ui/use_self.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,6 @@ error: unnecessary structure name repetition
169169
LL | A::new::<submod::B>(submod::B {})
170170
| ^ help: use the applicable keyword: `Self`
171171

172-
error: unnecessary structure name repetition
173-
--> tests/ui/use_self.rs:533:13
174-
|
175-
LL | S2::new()
176-
| ^^ help: use the applicable keyword: `Self`
177-
178172
error: unnecessary structure name repetition
179173
--> tests/ui/use_self.rs:571:17
180174
|
@@ -259,5 +253,5 @@ error: unnecessary structure name repetition
259253
LL | E::A => {},
260254
| ^ help: use the applicable keyword: `Self`
261255

262-
error: aborting due to 43 previous errors
256+
error: aborting due to 42 previous errors
263257

0 commit comments

Comments
 (0)