Skip to content

Commit 0b641e4

Browse files
committed
Check structs and enums for use_self
1 parent c4acbce commit 0b641e4

File tree

4 files changed

+350
-0
lines changed

4 files changed

+350
-0
lines changed

clippy_lints/src/use_self.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
117117
impl_id: item.owner_id.def_id,
118118
types_to_skip,
119119
}
120+
} else if let ItemKind::Struct(..) | ItemKind::Enum(..) = item.kind {
121+
StackItem::Check {
122+
impl_id: item.owner_id.def_id,
123+
types_to_skip: FxHashSet::default(),
124+
}
120125
} else {
121126
StackItem::NoCheck
122127
};

tests/ui/use_self_structs.fixed

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#![warn(clippy::use_self)]
2+
#![allow(clippy::type_complexity)]
3+
4+
fn main() {}
5+
6+
struct Basic {
7+
flag: Option<Box<Self>>,
8+
//~^ use_self
9+
}
10+
11+
struct BasicSelf {
12+
okay: Option<Box<Self>>,
13+
}
14+
15+
struct Generic<'q, T: From<u8>> {
16+
t: &'q T,
17+
flag: Option<Box<Self>>,
18+
//~^ use_self
19+
}
20+
21+
struct GenericSelf<'q, T: From<u8>> {
22+
t: &'q T,
23+
okay: Option<Box<Self>>,
24+
}
25+
26+
struct MixedLifetimes<'q, T: From<u8> + 'static> {
27+
t: &'q T,
28+
okay: Option<Box<MixedLifetimes<'static, T>>>,
29+
}
30+
31+
struct ConcreteType<'q, T: From<u8>> {
32+
t: &'q T,
33+
okay: Option<Box<ConcreteType<'q, u64>>>,
34+
}
35+
36+
struct ConcreteAndGeneric<'q, T: From<u8>> {
37+
t: &'q T,
38+
flag: Option<Box<Self>>,
39+
//~^ use_self
40+
okay: Option<Box<ConcreteAndGeneric<'q, u64>>>,
41+
}
42+
43+
struct ConcreteAndGenericSelf<'q, T: From<u8>> {
44+
t: &'q T,
45+
okay_1: Option<Box<Self>>,
46+
okay_2: Option<Box<ConcreteAndGeneric<'q, u64>>>,
47+
}
48+
49+
macro_rules! recursive_struct {
50+
($name:ident) => {
51+
struct $name {
52+
okay: Option<Box<$name>>,
53+
}
54+
};
55+
}
56+
57+
recursive_struct!(X);
58+
recursive_struct!(Y);
59+
recursive_struct!(Z);
60+
61+
struct Tree {
62+
left: Option<Box<Self>>,
63+
//~^ use_self
64+
right: Option<Box<Self>>,
65+
//~^ use_self
66+
}
67+
68+
struct TreeSelf {
69+
left: Option<Box<Self>>,
70+
right: Option<Box<Self>>,
71+
}
72+
73+
struct TreeMixed {
74+
left: Option<Box<Self>>,
75+
right: Option<Box<Self>>,
76+
//~^ use_self
77+
}
78+
79+
struct Nested {
80+
flag: Option<Box<Option<Box<Self>>>>,
81+
//~^ use_self
82+
}
83+
84+
struct NestedSelf {
85+
okay: Option<Box<Option<Box<Self>>>>,
86+
}
87+
88+
struct Tuple(Option<Box<Self>>);
89+
//~^ use_self
90+
91+
struct TupleSelf(Option<Box<Self>>);
92+
93+
use std::cell::RefCell;
94+
use std::rc::{Rc, Weak};
95+
96+
struct Containers {
97+
flag: Vec<Option<Rc<RefCell<Weak<Vec<Box<Self>>>>>>>,
98+
//~^ use_self
99+
}
100+
101+
struct ContainersSelf {
102+
okay: Vec<Option<Rc<RefCell<Weak<Vec<Box<Self>>>>>>>,
103+
}
104+
105+
type Wrappers<T> = Vec<Option<Rc<RefCell<Weak<Vec<Box<T>>>>>>>;
106+
107+
struct Alias {
108+
flag: Wrappers<Self>,
109+
//~^ use_self
110+
}
111+
112+
struct AliasSelf {
113+
okay: Wrappers<Self>,
114+
}
115+
116+
struct Array<const N: usize> {
117+
flag: [Option<Box<Self>>; N],
118+
//~^ use_self
119+
}
120+
121+
struct ArraySelf<const N: usize> {
122+
okay: [Option<Box<Self>>; N],
123+
}
124+
125+
enum Enum {
126+
Nil,
127+
Cons(Box<Self>),
128+
//~^ use_self
129+
}
130+
131+
enum EnumSelf {
132+
Nil,
133+
Cons(Box<Self>),
134+
}

tests/ui/use_self_structs.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#![warn(clippy::use_self)]
2+
#![allow(clippy::type_complexity)]
3+
4+
fn main() {}
5+
6+
struct Basic {
7+
flag: Option<Box<Basic>>,
8+
//~^ use_self
9+
}
10+
11+
struct BasicSelf {
12+
okay: Option<Box<Self>>,
13+
}
14+
15+
struct Generic<'q, T: From<u8>> {
16+
t: &'q T,
17+
flag: Option<Box<Generic<'q, T>>>,
18+
//~^ use_self
19+
}
20+
21+
struct GenericSelf<'q, T: From<u8>> {
22+
t: &'q T,
23+
okay: Option<Box<Self>>,
24+
}
25+
26+
struct MixedLifetimes<'q, T: From<u8> + 'static> {
27+
t: &'q T,
28+
okay: Option<Box<MixedLifetimes<'static, T>>>,
29+
}
30+
31+
struct ConcreteType<'q, T: From<u8>> {
32+
t: &'q T,
33+
okay: Option<Box<ConcreteType<'q, u64>>>,
34+
}
35+
36+
struct ConcreteAndGeneric<'q, T: From<u8>> {
37+
t: &'q T,
38+
flag: Option<Box<ConcreteAndGeneric<'q, T>>>,
39+
//~^ use_self
40+
okay: Option<Box<ConcreteAndGeneric<'q, u64>>>,
41+
}
42+
43+
struct ConcreteAndGenericSelf<'q, T: From<u8>> {
44+
t: &'q T,
45+
okay_1: Option<Box<Self>>,
46+
okay_2: Option<Box<ConcreteAndGeneric<'q, u64>>>,
47+
}
48+
49+
macro_rules! recursive_struct {
50+
($name:ident) => {
51+
struct $name {
52+
okay: Option<Box<$name>>,
53+
}
54+
};
55+
}
56+
57+
recursive_struct!(X);
58+
recursive_struct!(Y);
59+
recursive_struct!(Z);
60+
61+
struct Tree {
62+
left: Option<Box<Tree>>,
63+
//~^ use_self
64+
right: Option<Box<Tree>>,
65+
//~^ use_self
66+
}
67+
68+
struct TreeSelf {
69+
left: Option<Box<Self>>,
70+
right: Option<Box<Self>>,
71+
}
72+
73+
struct TreeMixed {
74+
left: Option<Box<Self>>,
75+
right: Option<Box<TreeMixed>>,
76+
//~^ use_self
77+
}
78+
79+
struct Nested {
80+
flag: Option<Box<Option<Box<Nested>>>>,
81+
//~^ use_self
82+
}
83+
84+
struct NestedSelf {
85+
okay: Option<Box<Option<Box<Self>>>>,
86+
}
87+
88+
struct Tuple(Option<Box<Tuple>>);
89+
//~^ use_self
90+
91+
struct TupleSelf(Option<Box<Self>>);
92+
93+
use std::cell::RefCell;
94+
use std::rc::{Rc, Weak};
95+
96+
struct Containers {
97+
flag: Vec<Option<Rc<RefCell<Weak<Vec<Box<Containers>>>>>>>,
98+
//~^ use_self
99+
}
100+
101+
struct ContainersSelf {
102+
okay: Vec<Option<Rc<RefCell<Weak<Vec<Box<Self>>>>>>>,
103+
}
104+
105+
type Wrappers<T> = Vec<Option<Rc<RefCell<Weak<Vec<Box<T>>>>>>>;
106+
107+
struct Alias {
108+
flag: Wrappers<Alias>,
109+
//~^ use_self
110+
}
111+
112+
struct AliasSelf {
113+
okay: Wrappers<Self>,
114+
}
115+
116+
struct Array<const N: usize> {
117+
flag: [Option<Box<Array<N>>>; N],
118+
//~^ use_self
119+
}
120+
121+
struct ArraySelf<const N: usize> {
122+
okay: [Option<Box<Self>>; N],
123+
}
124+
125+
enum Enum {
126+
Nil,
127+
Cons(Box<Enum>),
128+
//~^ use_self
129+
}
130+
131+
enum EnumSelf {
132+
Nil,
133+
Cons(Box<Self>),
134+
}

tests/ui/use_self_structs.stderr

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
error: unnecessary structure name repetition
2+
--> tests/ui/use_self_structs.rs:7:22
3+
|
4+
LL | flag: Option<Box<Basic>>,
5+
| ^^^^^ help: use the applicable keyword: `Self`
6+
|
7+
= note: `-D clippy::use-self` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::use_self)]`
9+
10+
error: unnecessary structure name repetition
11+
--> tests/ui/use_self_structs.rs:17:22
12+
|
13+
LL | flag: Option<Box<Generic<'q, T>>>,
14+
| ^^^^^^^^^^^^^^ help: use the applicable keyword: `Self`
15+
16+
error: unnecessary structure name repetition
17+
--> tests/ui/use_self_structs.rs:38:22
18+
|
19+
LL | flag: Option<Box<ConcreteAndGeneric<'q, T>>>,
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the applicable keyword: `Self`
21+
22+
error: unnecessary structure name repetition
23+
--> tests/ui/use_self_structs.rs:62:22
24+
|
25+
LL | left: Option<Box<Tree>>,
26+
| ^^^^ help: use the applicable keyword: `Self`
27+
28+
error: unnecessary structure name repetition
29+
--> tests/ui/use_self_structs.rs:64:23
30+
|
31+
LL | right: Option<Box<Tree>>,
32+
| ^^^^ help: use the applicable keyword: `Self`
33+
34+
error: unnecessary structure name repetition
35+
--> tests/ui/use_self_structs.rs:75:23
36+
|
37+
LL | right: Option<Box<TreeMixed>>,
38+
| ^^^^^^^^^ help: use the applicable keyword: `Self`
39+
40+
error: unnecessary structure name repetition
41+
--> tests/ui/use_self_structs.rs:80:33
42+
|
43+
LL | flag: Option<Box<Option<Box<Nested>>>>,
44+
| ^^^^^^ help: use the applicable keyword: `Self`
45+
46+
error: unnecessary structure name repetition
47+
--> tests/ui/use_self_structs.rs:88:25
48+
|
49+
LL | struct Tuple(Option<Box<Tuple>>);
50+
| ^^^^^ help: use the applicable keyword: `Self`
51+
52+
error: unnecessary structure name repetition
53+
--> tests/ui/use_self_structs.rs:97:46
54+
|
55+
LL | flag: Vec<Option<Rc<RefCell<Weak<Vec<Box<Containers>>>>>>>,
56+
| ^^^^^^^^^^ help: use the applicable keyword: `Self`
57+
58+
error: unnecessary structure name repetition
59+
--> tests/ui/use_self_structs.rs:108:20
60+
|
61+
LL | flag: Wrappers<Alias>,
62+
| ^^^^^ help: use the applicable keyword: `Self`
63+
64+
error: unnecessary structure name repetition
65+
--> tests/ui/use_self_structs.rs:117:23
66+
|
67+
LL | flag: [Option<Box<Array<N>>>; N],
68+
| ^^^^^^^^ help: use the applicable keyword: `Self`
69+
70+
error: unnecessary structure name repetition
71+
--> tests/ui/use_self_structs.rs:127:14
72+
|
73+
LL | Cons(Box<Enum>),
74+
| ^^^^ help: use the applicable keyword: `Self`
75+
76+
error: aborting due to 12 previous errors
77+

0 commit comments

Comments
 (0)