Skip to content

Commit e107f76

Browse files
committed
Don't trigger use_self in macros
1 parent a260c68 commit e107f76

File tree

5 files changed

+63
-60
lines changed

5 files changed

+63
-60
lines changed

clippy_lints/src/use_self.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{meets_msrv, qpath_res, snippet_opt, span_lint_and_sugg};
1+
use crate::utils::{in_macro, meets_msrv, qpath_res, snippet_opt, span_lint_and_sugg};
22
use if_chain::if_chain;
33

44
use rustc_errors::Applicability;
@@ -13,7 +13,6 @@ use rustc_hir::{
1313
};
1414
use rustc_lint::{LateContext, LateLintPass, LintContext};
1515
use rustc_middle::hir::map::Map;
16-
use rustc_middle::lint::in_external_macro;
1716
use rustc_middle::ty::{AssocKind, Ty};
1817
use rustc_semver::RustcVersion;
1918
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -232,7 +231,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
232231
}
233232

234233
fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) {
235-
if in_external_macro(cx.sess(), hir_ty.span)
234+
if in_macro(hir_ty.span)
236235
| in_impl(cx, hir_ty)
237236
| self.types_to_skip.contains(&hir_ty.hir_id)
238237
| !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV)
@@ -274,7 +273,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
274273
}
275274
}
276275

277-
if in_external_macro(cx.sess(), expr.span) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) {
276+
if in_macro(expr.span) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) {
278277
return;
279278
}
280279

tests/ui/auxiliary/proc_macro_derive.rs

+12
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,15 @@ pub fn derive_foo(_input: TokenStream) -> TokenStream {
4141
}
4242
}
4343
}
44+
45+
#[proc_macro_derive(StructAUseSelf)]
46+
pub fn derive_use_self(_input: TokenStream) -> proc_macro::TokenStream {
47+
quote! {
48+
struct A;
49+
impl A {
50+
fn new() -> A {
51+
A
52+
}
53+
}
54+
}
55+
}

tests/ui/use_self.fixed

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// run-rustfix
22
// edition:2018
3+
// aux-build:proc_macro_derive.rs
34

45
#![warn(clippy::use_self)]
56
#![allow(dead_code)]
67
#![allow(clippy::should_implement_trait, clippy::from_over_into)]
78

9+
#[macro_use]
10+
extern crate proc_macro_derive;
11+
812
fn main() {}
913

1014
mod use_self {
@@ -109,17 +113,20 @@ mod tuple_structs {
109113
mod macros {
110114
macro_rules! use_self_expand {
111115
() => {
112-
fn new() -> Self {
113-
Self {}
116+
fn new() -> Foo {
117+
Foo {}
114118
}
115119
};
116120
}
117121

118122
struct Foo {}
119123

120124
impl Foo {
121-
use_self_expand!(); // Should lint in local macros
125+
use_self_expand!(); // Should not lint in local macros
122126
}
127+
128+
#[derive(StructAUseSelf)] // Should not lint in derives
129+
struct A;
123130
}
124131

125132
mod nesting {

tests/ui/use_self.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
// run-rustfix
22
// edition:2018
3+
// aux-build:proc_macro_derive.rs
34

45
#![warn(clippy::use_self)]
56
#![allow(dead_code)]
67
#![allow(clippy::should_implement_trait, clippy::from_over_into)]
78

9+
#[macro_use]
10+
extern crate proc_macro_derive;
11+
812
fn main() {}
913

1014
mod use_self {
@@ -118,8 +122,11 @@ mod macros {
118122
struct Foo {}
119123

120124
impl Foo {
121-
use_self_expand!(); // Should lint in local macros
125+
use_self_expand!(); // Should not lint in local macros
122126
}
127+
128+
#[derive(StructAUseSelf)] // Should not lint in derives
129+
struct A;
123130
}
124131

125132
mod nesting {

tests/ui/use_self.stderr

+30-52
Original file line numberDiff line numberDiff line change
@@ -1,200 +1,178 @@
11
error: unnecessary structure name repetition
2-
--> $DIR/use_self.rs:14:21
2+
--> $DIR/use_self.rs:18:21
33
|
44
LL | fn new() -> Foo {
55
| ^^^ help: use the applicable keyword: `Self`
66
|
77
= note: `-D clippy::use-self` implied by `-D warnings`
88

99
error: unnecessary structure name repetition
10-
--> $DIR/use_self.rs:15:13
10+
--> $DIR/use_self.rs:19:13
1111
|
1212
LL | Foo {}
1313
| ^^^ help: use the applicable keyword: `Self`
1414

1515
error: unnecessary structure name repetition
16-
--> $DIR/use_self.rs:17:22
16+
--> $DIR/use_self.rs:21:22
1717
|
1818
LL | fn test() -> Foo {
1919
| ^^^ help: use the applicable keyword: `Self`
2020

2121
error: unnecessary structure name repetition
22-
--> $DIR/use_self.rs:18:13
22+
--> $DIR/use_self.rs:22:13
2323
|
2424
LL | Foo::new()
2525
| ^^^ help: use the applicable keyword: `Self`
2626

2727
error: unnecessary structure name repetition
28-
--> $DIR/use_self.rs:23:25
28+
--> $DIR/use_self.rs:27:25
2929
|
3030
LL | fn default() -> Foo {
3131
| ^^^ help: use the applicable keyword: `Self`
3232

3333
error: unnecessary structure name repetition
34-
--> $DIR/use_self.rs:24:13
34+
--> $DIR/use_self.rs:28:13
3535
|
3636
LL | Foo::new()
3737
| ^^^ help: use the applicable keyword: `Self`
3838

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

4545
error: unnecessary structure name repetition
46-
--> $DIR/use_self.rs:89:55
46+
--> $DIR/use_self.rs:93:55
4747
|
4848
LL | fn bad(foos: &[Foo]) -> impl Iterator<Item = &Foo> {
4949
| ^^^ help: use the applicable keyword: `Self`
5050

5151
error: unnecessary structure name repetition
52-
--> $DIR/use_self.rs:104:13
52+
--> $DIR/use_self.rs:108:13
5353
|
5454
LL | TS(0)
5555
| ^^ help: use the applicable keyword: `Self`
5656

5757
error: unnecessary structure name repetition
58-
--> $DIR/use_self.rs:112:25
59-
|
60-
LL | fn new() -> Foo {
61-
| ^^^ help: use the applicable keyword: `Self`
62-
...
63-
LL | use_self_expand!(); // Should lint in local macros
64-
| ------------------- in this macro invocation
65-
|
66-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
67-
68-
error: unnecessary structure name repetition
69-
--> $DIR/use_self.rs:113:17
70-
|
71-
LL | Foo {}
72-
| ^^^ help: use the applicable keyword: `Self`
73-
...
74-
LL | use_self_expand!(); // Should lint in local macros
75-
| ------------------- in this macro invocation
76-
|
77-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
78-
79-
error: unnecessary structure name repetition
80-
--> $DIR/use_self.rs:136:29
58+
--> $DIR/use_self.rs:143:29
8159
|
8260
LL | fn bar() -> Bar {
8361
| ^^^ help: use the applicable keyword: `Self`
8462

8563
error: unnecessary structure name repetition
86-
--> $DIR/use_self.rs:137:21
64+
--> $DIR/use_self.rs:144:21
8765
|
8866
LL | Bar { foo: Foo {} }
8967
| ^^^ help: use the applicable keyword: `Self`
9068

9169
error: unnecessary structure name repetition
92-
--> $DIR/use_self.rs:148:21
70+
--> $DIR/use_self.rs:155:21
9371
|
9472
LL | fn baz() -> Foo {
9573
| ^^^ help: use the applicable keyword: `Self`
9674

9775
error: unnecessary structure name repetition
98-
--> $DIR/use_self.rs:149:13
76+
--> $DIR/use_self.rs:156:13
9977
|
10078
LL | Foo {}
10179
| ^^^ help: use the applicable keyword: `Self`
10280

10381
error: unnecessary structure name repetition
104-
--> $DIR/use_self.rs:166:21
82+
--> $DIR/use_self.rs:173:21
10583
|
10684
LL | let _ = Enum::B(42);
10785
| ^^^^ help: use the applicable keyword: `Self`
10886

10987
error: unnecessary structure name repetition
110-
--> $DIR/use_self.rs:167:21
88+
--> $DIR/use_self.rs:174:21
11189
|
11290
LL | let _ = Enum::C { field: true };
11391
| ^^^^ help: use the applicable keyword: `Self`
11492

11593
error: unnecessary structure name repetition
116-
--> $DIR/use_self.rs:168:21
94+
--> $DIR/use_self.rs:175:21
11795
|
11896
LL | let _ = Enum::A;
11997
| ^^^^ help: use the applicable keyword: `Self`
12098

12199
error: unnecessary structure name repetition
122-
--> $DIR/use_self.rs:210:13
100+
--> $DIR/use_self.rs:217:13
123101
|
124102
LL | nested::A::fun_1();
125103
| ^^^^^^^^^ help: use the applicable keyword: `Self`
126104

127105
error: unnecessary structure name repetition
128-
--> $DIR/use_self.rs:211:13
106+
--> $DIR/use_self.rs:218:13
129107
|
130108
LL | nested::A::A;
131109
| ^^^^^^^^^ help: use the applicable keyword: `Self`
132110

133111
error: unnecessary structure name repetition
134-
--> $DIR/use_self.rs:213:13
112+
--> $DIR/use_self.rs:220:13
135113
|
136114
LL | nested::A {};
137115
| ^^^^^^^^^ help: use the applicable keyword: `Self`
138116

139117
error: unnecessary structure name repetition
140-
--> $DIR/use_self.rs:232:13
118+
--> $DIR/use_self.rs:239:13
141119
|
142120
LL | TestStruct::from_something()
143121
| ^^^^^^^^^^ help: use the applicable keyword: `Self`
144122

145123
error: unnecessary structure name repetition
146-
--> $DIR/use_self.rs:246:25
124+
--> $DIR/use_self.rs:253:25
147125
|
148126
LL | async fn g() -> S {
149127
| ^ help: use the applicable keyword: `Self`
150128

151129
error: unnecessary structure name repetition
152-
--> $DIR/use_self.rs:247:13
130+
--> $DIR/use_self.rs:254:13
153131
|
154132
LL | S {}
155133
| ^ help: use the applicable keyword: `Self`
156134

157135
error: unnecessary structure name repetition
158-
--> $DIR/use_self.rs:251:16
136+
--> $DIR/use_self.rs:258:16
159137
|
160138
LL | &p[S::A..S::B]
161139
| ^ help: use the applicable keyword: `Self`
162140

163141
error: unnecessary structure name repetition
164-
--> $DIR/use_self.rs:251:22
142+
--> $DIR/use_self.rs:258:22
165143
|
166144
LL | &p[S::A..S::B]
167145
| ^ help: use the applicable keyword: `Self`
168146

169147
error: unnecessary structure name repetition
170-
--> $DIR/use_self.rs:274:29
148+
--> $DIR/use_self.rs:281:29
171149
|
172150
LL | fn foo(value: T) -> Foo<T> {
173151
| ^^^^^^ help: use the applicable keyword: `Self`
174152

175153
error: unnecessary structure name repetition
176-
--> $DIR/use_self.rs:275:13
154+
--> $DIR/use_self.rs:282:13
177155
|
178156
LL | Foo { value }
179157
| ^^^ help: use the applicable keyword: `Self`
180158

181159
error: unnecessary structure name repetition
182-
--> $DIR/use_self.rs:312:21
160+
--> $DIR/use_self.rs:319:21
183161
|
184162
LL | type From = T::From;
185163
| ^^^^^^^ help: use the applicable keyword: `Self`
186164

187165
error: unnecessary structure name repetition
188-
--> $DIR/use_self.rs:313:19
166+
--> $DIR/use_self.rs:320:19
189167
|
190168
LL | type To = T::To;
191169
| ^^^^^ help: use the applicable keyword: `Self`
192170

193171
error: unnecessary structure name repetition
194-
--> $DIR/use_self.rs:450:13
172+
--> $DIR/use_self.rs:457:13
195173
|
196174
LL | A::new::<submod::B>(submod::B {})
197175
| ^ help: use the applicable keyword: `Self`
198176

199-
error: aborting due to 31 previous errors
177+
error: aborting due to 29 previous errors
200178

0 commit comments

Comments
 (0)