Skip to content

Commit c53d2b8

Browse files
committed
Auto merge of #4525 - mikerite:use-self-constructor, r=phansch
Extend `use_self` to check constructor Rust did not allow this before. changelog: Extend `use_self` to check constructor
2 parents 09ed605 + 31fbff2 commit c53d2b8

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

clippy_lints/src/use_self.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use if_chain::if_chain;
22
use rustc::hir;
3-
use rustc::hir::def::{CtorKind, DefKind, Res};
3+
use rustc::hir::def::{DefKind, Res};
44
use rustc::hir::intravisit::{walk_item, walk_path, walk_ty, NestedVisitorMap, Visitor};
55
use rustc::hir::*;
66
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
@@ -239,7 +239,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
239239
if path.segments.last().expect(SEGMENTS_MSG).ident.name != kw::SelfUpper {
240240
if self.item_path.res == path.res {
241241
span_use_self_lint(self.cx, path, None);
242-
} else if let Res::Def(DefKind::Ctor(def::CtorOf::Struct, CtorKind::Fn), ctor_def_id) = path.res {
242+
} else if let Res::Def(DefKind::Ctor(def::CtorOf::Struct, _), ctor_def_id) = path.res {
243243
if self.item_path.res.opt_def_id() == self.cx.tcx.parent(ctor_def_id) {
244244
span_use_self_lint(self.cx, path, None);
245245
}

tests/ui/use_self.fixed

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ mod traits {
112112
}
113113
}
114114

115+
impl Clone for Bad {
116+
fn clone(&self) -> Self {
117+
Self
118+
}
119+
}
120+
115121
#[derive(Default)]
116122
struct Good;
117123

@@ -171,15 +177,6 @@ mod traits {
171177
Self::default()
172178
}
173179
}
174-
175-
// Check that self arg isn't linted
176-
impl Clone for Good {
177-
fn clone(&self) -> Self {
178-
// Note: Not linted and it wouldn't be valid
179-
// because "can't use `Self` as a constructor`"
180-
Good
181-
}
182-
}
183180
}
184181

185182
mod issue2894 {

tests/ui/use_self.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ mod traits {
112112
}
113113
}
114114

115+
impl Clone for Bad {
116+
fn clone(&self) -> Self {
117+
Bad
118+
}
119+
}
120+
115121
#[derive(Default)]
116122
struct Good;
117123

@@ -171,15 +177,6 @@ mod traits {
171177
Self::default()
172178
}
173179
}
174-
175-
// Check that self arg isn't linted
176-
impl Clone for Good {
177-
fn clone(&self) -> Self {
178-
// Note: Not linted and it wouldn't be valid
179-
// because "can't use `Self` as a constructor`"
180-
Good
181-
}
182-
}
183180
}
184181

185182
mod issue2894 {

tests/ui/use_self.stderr

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,25 @@ LL | fn mul(self, rhs: Bad) -> Bad {
121121
| ^^^ help: use the applicable keyword: `Self`
122122

123123
error: unnecessary structure name repetition
124-
--> $DIR/use_self.rs:202:56
124+
--> $DIR/use_self.rs:117:13
125+
|
126+
LL | Bad
127+
| ^^^ help: use the applicable keyword: `Self`
128+
129+
error: unnecessary structure name repetition
130+
--> $DIR/use_self.rs:199:56
125131
|
126132
LL | fn bad(foos: &[Self]) -> impl Iterator<Item = &Foo> {
127133
| ^^^ help: use the applicable keyword: `Self`
128134

129135
error: unnecessary structure name repetition
130-
--> $DIR/use_self.rs:217:13
136+
--> $DIR/use_self.rs:214:13
131137
|
132138
LL | TS(0)
133139
| ^^ help: use the applicable keyword: `Self`
134140

135141
error: unnecessary structure name repetition
136-
--> $DIR/use_self.rs:225:25
142+
--> $DIR/use_self.rs:222:25
137143
|
138144
LL | fn new() -> Foo {
139145
| ^^^ help: use the applicable keyword: `Self`
@@ -142,7 +148,7 @@ LL | use_self_expand!(); // Should lint in local macros
142148
| ------------------- in this macro invocation
143149

144150
error: unnecessary structure name repetition
145-
--> $DIR/use_self.rs:226:17
151+
--> $DIR/use_self.rs:223:17
146152
|
147153
LL | Foo {}
148154
| ^^^ help: use the applicable keyword: `Self`
@@ -151,64 +157,64 @@ LL | use_self_expand!(); // Should lint in local macros
151157
| ------------------- in this macro invocation
152158

153159
error: unnecessary structure name repetition
154-
--> $DIR/use_self.rs:261:21
160+
--> $DIR/use_self.rs:258:21
155161
|
156162
LL | fn baz() -> Foo {
157163
| ^^^ help: use the applicable keyword: `Self`
158164

159165
error: unnecessary structure name repetition
160-
--> $DIR/use_self.rs:262:13
166+
--> $DIR/use_self.rs:259:13
161167
|
162168
LL | Foo {}
163169
| ^^^ help: use the applicable keyword: `Self`
164170

165171
error: unnecessary structure name repetition
166-
--> $DIR/use_self.rs:249:29
172+
--> $DIR/use_self.rs:246:29
167173
|
168174
LL | fn bar() -> Bar {
169175
| ^^^ help: use the applicable keyword: `Self`
170176

171177
error: unnecessary structure name repetition
172-
--> $DIR/use_self.rs:250:21
178+
--> $DIR/use_self.rs:247:21
173179
|
174180
LL | Bar { foo: Foo {} }
175181
| ^^^ help: use the applicable keyword: `Self`
176182

177183
error: unnecessary structure name repetition
178-
--> $DIR/use_self.rs:279:21
184+
--> $DIR/use_self.rs:276:21
179185
|
180186
LL | let _ = Enum::B(42);
181187
| ^^^^ help: use the applicable keyword: `Self`
182188

183189
error: unnecessary structure name repetition
184-
--> $DIR/use_self.rs:280:21
190+
--> $DIR/use_self.rs:277:21
185191
|
186192
LL | let _ = Enum::C { field: true };
187193
| ^^^^ help: use the applicable keyword: `Self`
188194

189195
error: unnecessary structure name repetition
190-
--> $DIR/use_self.rs:281:21
196+
--> $DIR/use_self.rs:278:21
191197
|
192198
LL | let _ = Enum::A;
193199
| ^^^^ help: use the applicable keyword: `Self`
194200

195201
error: unnecessary structure name repetition
196-
--> $DIR/use_self.rs:312:13
202+
--> $DIR/use_self.rs:309:13
197203
|
198204
LL | nested::A::fun_1();
199205
| ^^^^^^^^^ help: use the applicable keyword: `Self`
200206

201207
error: unnecessary structure name repetition
202-
--> $DIR/use_self.rs:313:13
208+
--> $DIR/use_self.rs:310:13
203209
|
204210
LL | nested::A::A;
205211
| ^^^^^^^^^ help: use the applicable keyword: `Self`
206212

207213
error: unnecessary structure name repetition
208-
--> $DIR/use_self.rs:315:13
214+
--> $DIR/use_self.rs:312:13
209215
|
210216
LL | nested::A {};
211217
| ^^^^^^^^^ help: use the applicable keyword: `Self`
212218

213-
error: aborting due to 34 previous errors
219+
error: aborting due to 35 previous errors
214220

0 commit comments

Comments
 (0)