Skip to content

Commit 0c5bfc3

Browse files
committed
Auto merge of #10081 - matthiaskrgr:stack_overflow_fix, r=<try>
Stack overflow fix See #10068 (comment) for details -- original PR description of #10068 -- fixes #10041 Prevent recursion into Self when it's a generic parameter. Added regression test from example in #10041. - \[x] Followed [lint naming conventions][lint_naming] - **N/A** - \[x] Added passing UI tests (including committed `.stderr` file) - \[x] `cargo test` passes locally - \[x] Executed `cargo dev update_lints` - **N/A** - \[x] Added lint documentation - **N/A** - \[x] Run `cargo dev fmt` changelog: [`new-ret-no-self`] Fix segmentation fault caused when generic parameter defaults to `Self` and is unspecified. For example, `fn uh_oh(&self)>
2 parents f43e4f3 + eecec91 commit 0c5bfc3

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

clippy_utils/src/ty.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'
9090
.substs
9191
.types()
9292
.skip(1) // Skip the implicit `Self` generic parameter
93-
.any(|ty| contains_ty_adt_constructor_opaque(cx, ty, needle))
93+
.filter(|inner_ty| *inner_ty != ty) // Skip any other `Self` generic parameters
94+
.any(|ty| ty == needle || ty.ty_adt_def() == needle.ty_adt_def())
9495
{
9596
return true;
9697
}

tests/ui/new_ret_no_self.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(type_alias_impl_trait)]
12
#![warn(clippy::new_ret_no_self)]
23
#![allow(dead_code)]
34

@@ -400,3 +401,26 @@ mod issue7344 {
400401
}
401402
}
402403
}
404+
405+
mod issue10041 {
406+
struct Bomb;
407+
408+
impl Bomb {
409+
// Hidden <Rhs = Self> default generic paramter.
410+
pub fn explode(&self) -> impl PartialOrd {
411+
0i32
412+
}
413+
}
414+
}
415+
416+
mod issue10041_TAIT {
417+
type X = impl std::ops::Add<Output = X>;
418+
419+
struct Foo;
420+
421+
impl Foo {
422+
fn new() -> X {
423+
return 1;
424+
}
425+
}
426+
}

tests/ui/new_ret_no_self.stderr

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: methods called `new` usually return `Self`
2-
--> $DIR/new_ret_no_self.rs:49:5
2+
--> $DIR/new_ret_no_self.rs:50:5
33
|
44
LL | / pub fn new(_: String) -> impl R<Item = u32> {
55
LL | | S3
@@ -9,88 +9,86 @@ LL | | }
99
= note: `-D clippy::new-ret-no-self` implied by `-D warnings`
1010

1111
error: methods called `new` usually return `Self`
12-
--> $DIR/new_ret_no_self.rs:81:5
12+
--> $DIR/new_ret_no_self.rs:82:5
1313
|
1414
LL | / pub fn new() -> u32 {
1515
LL | | unimplemented!();
1616
LL | | }
1717
| |_____^
1818

1919
error: methods called `new` usually return `Self`
20-
--> $DIR/new_ret_no_self.rs:90:5
20+
--> $DIR/new_ret_no_self.rs:91:5
2121
|
2222
LL | / pub fn new(_: String) -> u32 {
2323
LL | | unimplemented!();
2424
LL | | }
2525
| |_____^
2626

2727
error: methods called `new` usually return `Self`
28-
--> $DIR/new_ret_no_self.rs:126:5
28+
--> $DIR/new_ret_no_self.rs:127:5
2929
|
3030
LL | / pub fn new() -> (u32, u32) {
3131
LL | | unimplemented!();
3232
LL | | }
3333
| |_____^
3434

3535
error: methods called `new` usually return `Self`
36-
--> $DIR/new_ret_no_self.rs:153:5
36+
--> $DIR/new_ret_no_self.rs:154:5
3737
|
3838
LL | / pub fn new() -> *mut V {
3939
LL | | unimplemented!();
4040
LL | | }
4141
| |_____^
4242

4343
error: methods called `new` usually return `Self`
44-
--> $DIR/new_ret_no_self.rs:171:5
44+
--> $DIR/new_ret_no_self.rs:172:5
4545
|
4646
LL | / pub fn new() -> Option<u32> {
4747
LL | | unimplemented!();
4848
LL | | }
4949
| |_____^
5050

5151
error: methods called `new` usually return `Self`
52-
--> $DIR/new_ret_no_self.rs:224:9
52+
--> $DIR/new_ret_no_self.rs:225:9
5353
|
5454
LL | fn new() -> String;
5555
| ^^^^^^^^^^^^^^^^^^^
5656

5757
error: methods called `new` usually return `Self`
58-
--> $DIR/new_ret_no_self.rs:236:9
58+
--> $DIR/new_ret_no_self.rs:237:9
5959
|
6060
LL | fn new(_: String) -> String;
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6262

6363
error: methods called `new` usually return `Self`
64-
--> $DIR/new_ret_no_self.rs:271:9
64+
--> $DIR/new_ret_no_self.rs:272:9
6565
|
6666
LL | / fn new() -> (u32, u32) {
6767
LL | | unimplemented!();
6868
LL | | }
6969
| |_________^
7070

7171
error: methods called `new` usually return `Self`
72-
--> $DIR/new_ret_no_self.rs:298:9
72+
--> $DIR/new_ret_no_self.rs:299:9
7373
|
7474
LL | / fn new() -> *mut V {
7575
LL | | unimplemented!();
7676
LL | | }
7777
| |_________^
7878

7979
error: methods called `new` usually return `Self`
80-
--> $DIR/new_ret_no_self.rs:368:9
80+
--> $DIR/new_ret_no_self.rs:369:9
8181
|
8282
LL | / fn new(t: T) -> impl Into<i32> {
8383
LL | | 1
8484
LL | | }
8585
| |_________^
8686

8787
error: methods called `new` usually return `Self`
88-
--> $DIR/new_ret_no_self.rs:389:9
88+
--> $DIR/new_ret_no_self.rs:390:9
8989
|
9090
LL | / fn new(t: T) -> impl Trait2<(), i32> {
9191
LL | | unimplemented!()
9292
LL | | }
9393
| |_________^
9494

95-
error: aborting due to 12 previous errors
96-

0 commit comments

Comments
 (0)