Skip to content

Commit cdadb5a

Browse files
committed
rework use_self impl based on ty::Ty comparison rust-lang#3410
1 parent f2486b3 commit cdadb5a

File tree

8 files changed

+294
-269
lines changed

8 files changed

+294
-269
lines changed

clippy_lints/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
1313
#![feature(crate_visibility_modifier)]
1414
#![feature(concat_idents)]
15+
#![feature(bindings_after_at)]
1516

1617
// FIXME: switch to something more ergonomic here, once available.
1718
// (Currently there is no way to opt into sysroot crates without `extern crate`.)

clippy_lints/src/use_self.rs

+172-156
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,13 @@
1-
error: this could be a `const fn`
2-
--> $DIR/could_be_const.rs:13:5
3-
|
4-
LL | / pub fn new() -> Self {
5-
LL | | Self { guess: 42 }
6-
LL | | }
7-
| |_____^
8-
|
9-
= note: `-D clippy::missing-const-for-fn` implied by `-D warnings`
1+
error: internal compiler error: src/librustc_middle/ty/context.rs:516: node_type: no type for node `expr N (hir_id=HirId { owner: DefId(0:8 ~ could_be_const[317d]::{{impl}}[0]::const_generic_params[0]), local_id: 22 })`
102

11-
error: this could be a `const fn`
12-
--> $DIR/could_be_const.rs:17:5
13-
|
14-
LL | / fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] {
15-
LL | | b
16-
LL | | }
17-
| |_____^
3+
thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:904:9
4+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
185

19-
error: this could be a `const fn`
20-
--> $DIR/could_be_const.rs:23:1
21-
|
22-
LL | / fn one() -> i32 {
23-
LL | | 1
24-
LL | | }
25-
| |_^
6+
note: the compiler unexpectedly panicked. this is a bug.
267

27-
error: this could be a `const fn`
28-
--> $DIR/could_be_const.rs:28:1
29-
|
30-
LL | / fn two() -> i32 {
31-
LL | | let abc = 2;
32-
LL | | abc
33-
LL | | }
34-
| |_^
8+
note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new
359

36-
error: this could be a `const fn`
37-
--> $DIR/could_be_const.rs:34:1
38-
|
39-
LL | / fn string() -> String {
40-
LL | | String::new()
41-
LL | | }
42-
| |_^
10+
note: Clippy version: clippy 0.0.212 (517d0ff3 2020-04-23)
4311

44-
error: this could be a `const fn`
45-
--> $DIR/could_be_const.rs:39:1
46-
|
47-
LL | / unsafe fn four() -> i32 {
48-
LL | | 4
49-
LL | | }
50-
| |_^
51-
52-
error: this could be a `const fn`
53-
--> $DIR/could_be_const.rs:44:1
54-
|
55-
LL | / fn generic<T>(t: T) -> T {
56-
LL | | t
57-
LL | | }
58-
| |_^
59-
60-
error: this could be a `const fn`
61-
--> $DIR/could_be_const.rs:48:1
62-
|
63-
LL | / fn sub(x: u32) -> usize {
64-
LL | | unsafe { transmute(&x) }
65-
LL | | }
66-
| |_^
67-
68-
error: this could be a `const fn`
69-
--> $DIR/could_be_const.rs:67:9
70-
|
71-
LL | / pub fn b(self, a: &A) -> B {
72-
LL | | B
73-
LL | | }
74-
| |_________^
75-
76-
error: aborting due to 9 previous errors
12+
error: aborting due to previous error
7713

tests/ui/use_self.fixed

+40-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ mod existential {
8686
struct Foo;
8787

8888
impl Foo {
89-
fn bad(foos: &[Self]) -> impl Iterator<Item = &Self> {
89+
// TyKind::Def (used for `impl Trait` types) does not include type parameters yet.
90+
// See documentation in rustc_hir::hir::TyKind.
91+
// The hir tree walk stops at `impl Iterator` level and does not inspect &Foo.
92+
fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
9093
foos.iter()
9194
}
9295

@@ -176,11 +179,23 @@ mod issue3410 {
176179
struct B;
177180

178181
trait Trait<T> {
179-
fn a(v: T);
182+
fn a(v: T) -> Self;
180183
}
181184

182185
impl Trait<Vec<A>> for Vec<B> {
183-
fn a(_: Vec<A>) {}
186+
fn a(_: Vec<A>) -> Self {
187+
unimplemented!()
188+
}
189+
}
190+
191+
impl<T> Trait<Vec<A>> for Vec<T>
192+
where
193+
T: Trait<B>,
194+
{
195+
fn a(v: Vec<A>) -> Self {
196+
// fix false positive, not applicable here
197+
<Vec<B>>::a(v).into_iter().map(Trait::a).collect()
198+
}
184199
}
185200
}
186201

@@ -232,7 +247,9 @@ mod paths_created_by_lowering {
232247
const A: usize = 0;
233248
const B: usize = 1;
234249

235-
async fn g() -> Self {
250+
// TODO
251+
// applicable here
252+
async fn g() -> S {
236253
Self {}
237254
}
238255

@@ -251,3 +268,22 @@ mod paths_created_by_lowering {
251268
}
252269
}
253270
}
271+
272+
// reused from #1997
273+
mod generics {
274+
struct Foo<T> {
275+
value: T,
276+
}
277+
278+
impl<T> Foo<T> {
279+
// `Self` is applicable here
280+
fn foo(value: T) -> Self {
281+
Self { value }
282+
}
283+
284+
// `Cannot` use `Self` as a return type as the generic types are different
285+
fn bar(value: i32) -> Foo<i32> {
286+
Foo { value }
287+
}
288+
}
289+
}

tests/ui/use_self.rs

+39-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ mod existential {
8686
struct Foo;
8787

8888
impl Foo {
89-
fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
89+
// TyKind::Def (used for `impl Trait` types) does not include type parameters yet.
90+
// See documentation in rustc_hir::hir::TyKind.
91+
// The hir tree walk stops at `impl Iterator` level and does not inspect &Foo.
92+
fn bad(foos: &[Foo]) -> impl Iterator<Item = &Foo> {
9093
foos.iter()
9194
}
9295

@@ -176,11 +179,23 @@ mod issue3410 {
176179
struct B;
177180

178181
trait Trait<T> {
179-
fn a(v: T);
182+
fn a(v: T) -> Self;
180183
}
181184

182185
impl Trait<Vec<A>> for Vec<B> {
183-
fn a(_: Vec<A>) {}
186+
fn a(_: Vec<A>) -> Self {
187+
unimplemented!()
188+
}
189+
}
190+
191+
impl<T> Trait<Vec<A>> for Vec<T>
192+
where
193+
T: Trait<B>,
194+
{
195+
fn a(v: Vec<A>) -> Self {
196+
// fix false positive, not applicable here
197+
<Vec<B>>::a(v).into_iter().map(Trait::a).collect()
198+
}
184199
}
185200
}
186201

@@ -232,6 +247,8 @@ mod paths_created_by_lowering {
232247
const A: usize = 0;
233248
const B: usize = 1;
234249

250+
// TODO
251+
// applicable here
235252
async fn g() -> S {
236253
S {}
237254
}
@@ -251,3 +268,22 @@ mod paths_created_by_lowering {
251268
}
252269
}
253270
}
271+
272+
// reused from #1997
273+
mod generics {
274+
struct Foo<T> {
275+
value: T,
276+
}
277+
278+
impl<T> Foo<T> {
279+
// `Self` is applicable here
280+
fn foo(value: T) -> Foo<T> {
281+
Foo { value }
282+
}
283+
284+
// `Cannot` use `Self` as a return type as the generic types are different
285+
fn bar(value: i32) -> Foo<i32> {
286+
Foo { value }
287+
}
288+
}
289+
}

tests/ui/use_self.stderr

+33-27
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ LL | Foo::new()
3737
| ^^^ help: use the applicable keyword: `Self`
3838

3939
error: unnecessary structure name repetition
40-
--> $DIR/use_self.rs:89:56
40+
--> $DIR/use_self.rs:92:24
4141
|
42-
LL | fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
43-
| ^^^ help: use the applicable keyword: `Self`
42+
LL | fn bad(foos: &[Foo]) -> impl Iterator<Item = &Foo> {
43+
| ^^^ help: use the applicable keyword: `Self`
4444

4545
error: unnecessary structure name repetition
46-
--> $DIR/use_self.rs:104:13
46+
--> $DIR/use_self.rs:107:13
4747
|
4848
LL | TS(0)
4949
| ^^ help: use the applicable keyword: `Self`
5050

5151
error: unnecessary structure name repetition
52-
--> $DIR/use_self.rs:112:25
52+
--> $DIR/use_self.rs:115:25
5353
|
5454
LL | fn new() -> Foo {
5555
| ^^^ help: use the applicable keyword: `Self`
@@ -60,7 +60,7 @@ LL | use_self_expand!(); // Should lint in local macros
6060
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
6161

6262
error: unnecessary structure name repetition
63-
--> $DIR/use_self.rs:113:17
63+
--> $DIR/use_self.rs:116:17
6464
|
6565
LL | Foo {}
6666
| ^^^ help: use the applicable keyword: `Self`
@@ -71,94 +71,100 @@ LL | use_self_expand!(); // Should lint in local macros
7171
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
7272

7373
error: unnecessary structure name repetition
74-
--> $DIR/use_self.rs:148:21
74+
--> $DIR/use_self.rs:151:21
7575
|
7676
LL | fn baz() -> Foo {
7777
| ^^^ help: use the applicable keyword: `Self`
7878

7979
error: unnecessary structure name repetition
80-
--> $DIR/use_self.rs:149:13
80+
--> $DIR/use_self.rs:152:13
8181
|
8282
LL | Foo {}
8383
| ^^^ help: use the applicable keyword: `Self`
8484

8585
error: unnecessary structure name repetition
86-
--> $DIR/use_self.rs:136:29
86+
--> $DIR/use_self.rs:139:29
8787
|
8888
LL | fn bar() -> Bar {
8989
| ^^^ help: use the applicable keyword: `Self`
9090

9191
error: unnecessary structure name repetition
92-
--> $DIR/use_self.rs:137:21
92+
--> $DIR/use_self.rs:140:21
9393
|
9494
LL | Bar { foo: Foo {} }
9595
| ^^^ help: use the applicable keyword: `Self`
9696

9797
error: unnecessary structure name repetition
98-
--> $DIR/use_self.rs:166:21
98+
--> $DIR/use_self.rs:169:21
9999
|
100100
LL | let _ = Enum::B(42);
101101
| ^^^^ help: use the applicable keyword: `Self`
102102

103103
error: unnecessary structure name repetition
104-
--> $DIR/use_self.rs:167:21
104+
--> $DIR/use_self.rs:170:21
105105
|
106106
LL | let _ = Enum::C { field: true };
107107
| ^^^^ help: use the applicable keyword: `Self`
108108

109109
error: unnecessary structure name repetition
110-
--> $DIR/use_self.rs:168:21
110+
--> $DIR/use_self.rs:171:21
111111
|
112112
LL | let _ = Enum::A;
113113
| ^^^^ help: use the applicable keyword: `Self`
114114

115115
error: unnecessary structure name repetition
116-
--> $DIR/use_self.rs:199:13
116+
--> $DIR/use_self.rs:214:13
117117
|
118118
LL | nested::A::fun_1();
119119
| ^^^^^^^^^ help: use the applicable keyword: `Self`
120120

121121
error: unnecessary structure name repetition
122-
--> $DIR/use_self.rs:200:13
122+
--> $DIR/use_self.rs:215:13
123123
|
124124
LL | nested::A::A;
125125
| ^^^^^^^^^ help: use the applicable keyword: `Self`
126126

127127
error: unnecessary structure name repetition
128-
--> $DIR/use_self.rs:202:13
128+
--> $DIR/use_self.rs:217:13
129129
|
130130
LL | nested::A {};
131131
| ^^^^^^^^^ help: use the applicable keyword: `Self`
132132

133133
error: unnecessary structure name repetition
134-
--> $DIR/use_self.rs:221:13
134+
--> $DIR/use_self.rs:236:13
135135
|
136136
LL | TestStruct::from_something()
137137
| ^^^^^^^^^^ help: use the applicable keyword: `Self`
138138

139139
error: unnecessary structure name repetition
140-
--> $DIR/use_self.rs:235:25
141-
|
142-
LL | async fn g() -> S {
143-
| ^ help: use the applicable keyword: `Self`
144-
145-
error: unnecessary structure name repetition
146-
--> $DIR/use_self.rs:236:13
140+
--> $DIR/use_self.rs:253:13
147141
|
148142
LL | S {}
149143
| ^ help: use the applicable keyword: `Self`
150144

151145
error: unnecessary structure name repetition
152-
--> $DIR/use_self.rs:240:16
146+
--> $DIR/use_self.rs:257:16
153147
|
154148
LL | &p[S::A..S::B]
155149
| ^ help: use the applicable keyword: `Self`
156150

157151
error: unnecessary structure name repetition
158-
--> $DIR/use_self.rs:240:22
152+
--> $DIR/use_self.rs:257:22
159153
|
160154
LL | &p[S::A..S::B]
161155
| ^ help: use the applicable keyword: `Self`
162156

163-
error: aborting due to 25 previous errors
157+
error: unnecessary structure name repetition
158+
--> $DIR/use_self.rs:280:29
159+
|
160+
LL | fn foo(value: T) -> Foo<T> {
161+
| ^^^^^^ help: use the applicable keyword: `Self`
162+
163+
error: unnecessary structure name repetition
164+
--> $DIR/use_self.rs:281:13
165+
|
166+
LL | Foo { value }
167+
| ^^^ help: use the applicable keyword: `Self`
168+
169+
error: aborting due to 26 previous errors
164170

tests/ui/use_self_trait.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Mul for Bad {
4747

4848
impl Clone for Bad {
4949
fn clone(&self) -> Self {
50-
Self
50+
Bad
5151
}
5252
}
5353

0 commit comments

Comments
 (0)