Skip to content

Commit d7e4b50

Browse files
committed
Auto merge of #4365 - lukas-code:async_new_ret_no_self, r=flip1995
new_ret_no_self: allow Self in inner type for impl Trait return types Check the inner types of associated types of a trait when checking for Self in the return type of a `new` method. This means that the following will no longer warn: ```rust trait Trait { type Inner; } struct S; impl S { fn new() -> impl Trait<Inner = Option<Self>> { struct TraitImpl; impl Trait for TraitImpl { type Inner = Option<S>; } TraitImpl } } ``` ```rust #![feature(async_await)] struct Connection; impl Connection { async fn new() -> Result<Self, ()> { Ok(S) } } ``` closes #4359
2 parents 72da101 + d553158 commit d7e4b50

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

clippy_lints/src/methods/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1070,11 +1070,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
10701070
(Predicate::Projection(poly_projection_predicate), _) => {
10711071
let binder = poly_projection_predicate.ty();
10721072
let associated_type = binder.skip_binder();
1073-
let associated_type_is_self_type = same_tys(cx, ty, associated_type);
10741073

1075-
// if the associated type is self, early return and do not trigger lint
1076-
if associated_type_is_self_type {
1077-
return;
1074+
// walk the associated type and check for Self
1075+
for inner_type in associated_type.walk() {
1076+
if same_tys(cx, ty, inner_type) {
1077+
return;
1078+
}
10781079
}
10791080
},
10801081
(_, _) => {},

tests/ui/methods.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// aux-build:option_helpers.rs
2+
// compile-flags: --edition 2018
23

4+
#![feature(async_await)]
35
#![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
46
#![allow(
57
clippy::blacklisted_name,
@@ -11,7 +13,6 @@
1113
clippy::needless_pass_by_value,
1214
clippy::default_trait_access,
1315
clippy::use_self,
14-
clippy::new_ret_no_self,
1516
clippy::useless_format,
1617
clippy::wrong_self_convention
1718
)]
@@ -138,6 +139,22 @@ impl<T> V<T> {
138139
}
139140
}
140141

142+
struct AsyncNew;
143+
144+
impl AsyncNew {
145+
async fn new() -> Option<Self> {
146+
None
147+
}
148+
}
149+
150+
struct BadNew;
151+
152+
impl BadNew {
153+
fn new() -> i32 {
154+
0
155+
}
156+
}
157+
141158
impl Mul<T> for T {
142159
type Output = T;
143160
// No error, obviously.

tests/ui/methods.stderr

+31-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: defining a method called `add` on this type; consider implementing the `std::ops::Add` trait or choosing a less ambiguous name
2-
--> $DIR/methods.rs:36:5
2+
--> $DIR/methods.rs:37:5
33
|
44
LL | / pub fn add(self, other: T) -> T {
55
LL | | self
@@ -8,8 +8,18 @@ LL | | }
88
|
99
= note: `-D clippy::should-implement-trait` implied by `-D warnings`
1010

11+
error: methods called `new` usually return `Self`
12+
--> $DIR/methods.rs:153:5
13+
|
14+
LL | / fn new() -> i32 {
15+
LL | | 0
16+
LL | | }
17+
| |_____^
18+
|
19+
= note: `-D clippy::new-ret-no-self` implied by `-D warnings`
20+
1121
error: called `map(f).unwrap_or(a)` on an Option value. This can be done more directly by calling `map_or(a, f)` instead
12-
--> $DIR/methods.rs:158:13
22+
--> $DIR/methods.rs:175:13
1323
|
1424
LL | let _ = opt.map(|x| x + 1)
1525
| _____________^
@@ -21,7 +31,7 @@ LL | | .unwrap_or(0);
2131
= note: replace `map(|x| x + 1).unwrap_or(0)` with `map_or(0, |x| x + 1)`
2232

2333
error: called `map(f).unwrap_or(a)` on an Option value. This can be done more directly by calling `map_or(a, f)` instead
24-
--> $DIR/methods.rs:162:13
34+
--> $DIR/methods.rs:179:13
2535
|
2636
LL | let _ = opt.map(|x| {
2737
| _____________^
@@ -31,7 +41,7 @@ LL | | ).unwrap_or(0);
3141
| |____________________________^
3242

3343
error: called `map(f).unwrap_or(a)` on an Option value. This can be done more directly by calling `map_or(a, f)` instead
34-
--> $DIR/methods.rs:166:13
44+
--> $DIR/methods.rs:183:13
3545
|
3646
LL | let _ = opt.map(|x| x + 1)
3747
| _____________^
@@ -41,15 +51,15 @@ LL | | });
4151
| |__________________^
4252

4353
error: called `map(f).unwrap_or(None)` on an Option value. This can be done more directly by calling `and_then(f)` instead
44-
--> $DIR/methods.rs:171:13
54+
--> $DIR/methods.rs:188:13
4555
|
4656
LL | let _ = opt.map(|x| Some(x + 1)).unwrap_or(None);
4757
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4858
|
4959
= note: replace `map(|x| Some(x + 1)).unwrap_or(None)` with `and_then(|x| Some(x + 1))`
5060

5161
error: called `map(f).unwrap_or(None)` on an Option value. This can be done more directly by calling `and_then(f)` instead
52-
--> $DIR/methods.rs:173:13
62+
--> $DIR/methods.rs:190:13
5363
|
5464
LL | let _ = opt.map(|x| {
5565
| _____________^
@@ -59,7 +69,7 @@ LL | | ).unwrap_or(None);
5969
| |_____________________^
6070

6171
error: called `map(f).unwrap_or(None)` on an Option value. This can be done more directly by calling `and_then(f)` instead
62-
--> $DIR/methods.rs:177:13
72+
--> $DIR/methods.rs:194:13
6373
|
6474
LL | let _ = opt
6575
| _____________^
@@ -70,15 +80,15 @@ LL | | .unwrap_or(None);
7080
= note: replace `map(|x| Some(x + 1)).unwrap_or(None)` with `and_then(|x| Some(x + 1))`
7181

7282
error: called `map(f).unwrap_or(a)` on an Option value. This can be done more directly by calling `map_or(a, f)` instead
73-
--> $DIR/methods.rs:188:13
83+
--> $DIR/methods.rs:205:13
7484
|
7585
LL | let _ = Some("prefix").map(|p| format!("{}.", p)).unwrap_or(id);
7686
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7787
|
7888
= note: replace `map(|p| format!("{}.", p)).unwrap_or(id)` with `map_or(id, |p| format!("{}.", p))`
7989

8090
error: called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling `map_or_else(g, f)` instead
81-
--> $DIR/methods.rs:192:13
91+
--> $DIR/methods.rs:209:13
8292
|
8393
LL | let _ = opt.map(|x| x + 1)
8494
| _____________^
@@ -90,7 +100,7 @@ LL | | .unwrap_or_else(|| 0);
90100
= note: replace `map(|x| x + 1).unwrap_or_else(|| 0)` with `map_or_else(|| 0, |x| x + 1)`
91101

92102
error: called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling `map_or_else(g, f)` instead
93-
--> $DIR/methods.rs:196:13
103+
--> $DIR/methods.rs:213:13
94104
|
95105
LL | let _ = opt.map(|x| {
96106
| _____________^
@@ -100,7 +110,7 @@ LL | | ).unwrap_or_else(|| 0);
100110
| |____________________________________^
101111

102112
error: called `map(f).unwrap_or_else(g)` on an Option value. This can be done more directly by calling `map_or_else(g, f)` instead
103-
--> $DIR/methods.rs:200:13
113+
--> $DIR/methods.rs:217:13
104114
|
105115
LL | let _ = opt.map(|x| x + 1)
106116
| _____________^
@@ -110,7 +120,7 @@ LL | | );
110120
| |_________________^
111121

112122
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
113-
--> $DIR/methods.rs:230:13
123+
--> $DIR/methods.rs:247:13
114124
|
115125
LL | let _ = v.iter().filter(|&x| *x < 0).next();
116126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -119,7 +129,7 @@ LL | let _ = v.iter().filter(|&x| *x < 0).next();
119129
= note: replace `filter(|&x| *x < 0).next()` with `find(|&x| *x < 0)`
120130

121131
error: called `filter(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(p)` instead.
122-
--> $DIR/methods.rs:233:13
132+
--> $DIR/methods.rs:250:13
123133
|
124134
LL | let _ = v.iter().filter(|&x| {
125135
| _____________^
@@ -129,7 +139,7 @@ LL | | ).next();
129139
| |___________________________^
130140

131141
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
132-
--> $DIR/methods.rs:249:13
142+
--> $DIR/methods.rs:266:13
133143
|
134144
LL | let _ = v.iter().find(|&x| *x < 0).is_some();
135145
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -138,7 +148,7 @@ LL | let _ = v.iter().find(|&x| *x < 0).is_some();
138148
= note: replace `find(|&x| *x < 0).is_some()` with `any(|x| *x < 0)`
139149

140150
error: called `is_some()` after searching an `Iterator` with find. This is more succinctly expressed by calling `any()`.
141-
--> $DIR/methods.rs:252:13
151+
--> $DIR/methods.rs:269:13
142152
|
143153
LL | let _ = v.iter().find(|&x| {
144154
| _____________^
@@ -148,15 +158,15 @@ LL | | ).is_some();
148158
| |______________________________^
149159

150160
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
151-
--> $DIR/methods.rs:258:13
161+
--> $DIR/methods.rs:275:13
152162
|
153163
LL | let _ = v.iter().position(|&x| x < 0).is_some();
154164
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
155165
|
156166
= note: replace `position(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
157167

158168
error: called `is_some()` after searching an `Iterator` with position. This is more succinctly expressed by calling `any()`.
159-
--> $DIR/methods.rs:261:13
169+
--> $DIR/methods.rs:278:13
160170
|
161171
LL | let _ = v.iter().position(|&x| {
162172
| _____________^
@@ -166,15 +176,15 @@ LL | | ).is_some();
166176
| |______________________________^
167177

168178
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
169-
--> $DIR/methods.rs:267:13
179+
--> $DIR/methods.rs:284:13
170180
|
171181
LL | let _ = v.iter().rposition(|&x| x < 0).is_some();
172182
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
173183
|
174184
= note: replace `rposition(|&x| x < 0).is_some()` with `any(|&x| x < 0)`
175185

176186
error: called `is_some()` after searching an `Iterator` with rposition. This is more succinctly expressed by calling `any()`.
177-
--> $DIR/methods.rs:270:13
187+
--> $DIR/methods.rs:287:13
178188
|
179189
LL | let _ = v.iter().rposition(|&x| {
180190
| _____________^
@@ -184,12 +194,12 @@ LL | | ).is_some();
184194
| |______________________________^
185195

186196
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
187-
--> $DIR/methods.rs:285:13
197+
--> $DIR/methods.rs:302:13
188198
|
189199
LL | let _ = opt.unwrap();
190200
| ^^^^^^^^^^^^
191201
|
192202
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
193203

194-
error: aborting due to 20 previous errors
204+
error: aborting due to 21 previous errors
195205

0 commit comments

Comments
 (0)