Skip to content

Commit 8fc0a73

Browse files
committed
Auto merge of #3869 - taiki-e:use_self, r=flip1995
Fix `use_self` false positive on nested functions Related to #3640 The current `use_self` warns the following code. ```rust #![warn(clippy::use_self)] struct Foo {} impl Foo { fn bar() { fn baz() -> Foo { //^ warning: unnecessary structure name repetition Foo {} //^ warning: unnecessary structure name repetition } } } ```
2 parents 6e2b8fa + 187ce4c commit 8fc0a73

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

clippy_lints/src/use_self.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> {
248248
| ItemKind::Enum(..)
249249
| ItemKind::Struct(..)
250250
| ItemKind::Union(..)
251-
| ItemKind::Impl(..) => {
251+
| ItemKind::Impl(..)
252+
| ItemKind::Fn(..) => {
252253
// Don't check statements that shadow `Self` or where `Self` can't be used
253254
},
254255
_ => walk_item(self, item),

tests/ui/use_self.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ mod nesting {
249249
Self { foo: Foo {} }
250250
}
251251
}
252+
253+
// Can't use Self here
254+
fn baz() -> Foo {
255+
Foo {}
256+
}
257+
}
258+
259+
// Should lint here
260+
fn baz() -> Self {
261+
Self {}
252262
}
253263
}
254264

tests/ui/use_self.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ mod nesting {
249249
Bar { foo: Foo {} }
250250
}
251251
}
252+
253+
// Can't use Self here
254+
fn baz() -> Foo {
255+
Foo {}
256+
}
257+
}
258+
259+
// Should lint here
260+
fn baz() -> Foo {
261+
Foo {}
252262
}
253263
}
254264

tests/ui/use_self.stderr

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ LL | Foo {}
150150
LL | use_self_expand!(); // Should lint in local macros
151151
| ------------------- in this macro invocation
152152

153+
error: unnecessary structure name repetition
154+
--> $DIR/use_self.rs:260:21
155+
|
156+
LL | fn baz() -> Foo {
157+
| ^^^ help: use the applicable keyword: `Self`
158+
159+
error: unnecessary structure name repetition
160+
--> $DIR/use_self.rs:261:13
161+
|
162+
LL | Foo {}
163+
| ^^^ help: use the applicable keyword: `Self`
164+
153165
error: unnecessary structure name repetition
154166
--> $DIR/use_self.rs:248:29
155167
|
@@ -163,22 +175,22 @@ LL | Bar { foo: Foo {} }
163175
| ^^^ help: use the applicable keyword: `Self`
164176

165177
error: unnecessary structure name repetition
166-
--> $DIR/use_self.rs:293:13
178+
--> $DIR/use_self.rs:303:13
167179
|
168180
LL | nested::A::fun_1();
169181
| ^^^^^^^^^ help: use the applicable keyword: `Self`
170182

171183
error: unnecessary structure name repetition
172-
--> $DIR/use_self.rs:294:13
184+
--> $DIR/use_self.rs:304:13
173185
|
174186
LL | nested::A::A;
175187
| ^^^^^^^^^ help: use the applicable keyword: `Self`
176188

177189
error: unnecessary structure name repetition
178-
--> $DIR/use_self.rs:296:13
190+
--> $DIR/use_self.rs:306:13
179191
|
180192
LL | nested::A {};
181193
| ^^^^^^^^^ help: use the applicable keyword: `Self`
182194

183-
error: aborting due to 29 previous errors
195+
error: aborting due to 31 previous errors
184196

0 commit comments

Comments
 (0)