Skip to content

Commit c26a704

Browse files
committed
wrong_self_conversion: Emit lint msg with help msg
1 parent 25ac230 commit c26a704

File tree

6 files changed

+126
-64
lines changed

6 files changed

+126
-64
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,19 +1985,21 @@ fn lint_wrong_self_convention<'tcx>(
19851985
}
19861986
};
19871987

1988-
span_lint(
1988+
span_lint_and_help(
19891989
cx,
19901990
lint,
19911991
first_arg_span,
19921992
&format!(
1993-
"{} usually take {}; consider choosing a less ambiguous name",
1993+
"{} usually take {}",
19941994
suggestion,
19951995
&self_kinds
19961996
.iter()
19971997
.map(|k| k.description())
19981998
.collect::<Vec<_>>()
19991999
.join(" or ")
20002000
),
2001+
None,
2002+
"consider choosing a less ambiguous name",
20012003
);
20022004
}
20032005
}

tests/ui/def_id_nocore.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
error: methods called `as_*` usually take self by reference or self by mutable reference; consider choosing a less ambiguous name
1+
error: methods called `as_*` usually take self by reference or self by mutable reference
22
--> $DIR/def_id_nocore.rs:26:19
33
|
44
LL | pub fn as_ref(self) -> &'static str {
55
| ^^^^
66
|
77
= note: `-D clippy::wrong-self-convention` implied by `-D warnings`
8+
= help: consider choosing a less ambiguous name
89

910
error: aborting due to previous error
1011

tests/ui/wrong_self_convention.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,3 @@ mod issue6307 {
163163
fn to_mut(&mut self);
164164
}
165165
}
166-
167-
mod issue6758 {
168-
pub enum Test<T> {
169-
One(T),
170-
Many(Vec<T>),
171-
}
172-
173-
impl<T> Test<T> {
174-
// If a method starts with `to_` and not ends with `_mut` it should expect `&self`
175-
pub fn to_many(&mut self) -> Option<&mut [T]> {
176-
match self {
177-
Self::Many(data) => Some(data),
178-
_ => None,
179-
}
180-
}
181-
182-
// If a method starts with `to_` and ends with `_mut` it should expect `&mut self`
183-
pub fn to_many_mut(&self) -> Option<&[T]> {
184-
match self {
185-
Self::Many(data) => Some(data),
186-
_ => None,
187-
}
188-
}
189-
}
190-
}
Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,195 @@
1-
error: methods called `from_*` usually take no self; consider choosing a less ambiguous name
1+
error: methods called `from_*` usually take no self
22
--> $DIR/wrong_self_convention.rs:18:17
33
|
44
LL | fn from_i32(self) {}
55
| ^^^^
66
|
77
= note: `-D clippy::wrong-self-convention` implied by `-D warnings`
8+
= help: consider choosing a less ambiguous name
89

9-
error: methods called `from_*` usually take no self; consider choosing a less ambiguous name
10+
error: methods called `from_*` usually take no self
1011
--> $DIR/wrong_self_convention.rs:24:21
1112
|
1213
LL | pub fn from_i64(self) {}
1314
| ^^^^
15+
|
16+
= help: consider choosing a less ambiguous name
1417

15-
error: methods called `as_*` usually take self by reference or self by mutable reference; consider choosing a less ambiguous name
18+
error: methods called `as_*` usually take self by reference or self by mutable reference
1619
--> $DIR/wrong_self_convention.rs:36:15
1720
|
1821
LL | fn as_i32(self) {}
1922
| ^^^^
23+
|
24+
= help: consider choosing a less ambiguous name
2025

21-
error: methods called `into_*` usually take self by value; consider choosing a less ambiguous name
26+
error: methods called `into_*` usually take self by value
2227
--> $DIR/wrong_self_convention.rs:38:17
2328
|
2429
LL | fn into_i32(&self) {}
2530
| ^^^^^
31+
|
32+
= help: consider choosing a less ambiguous name
2633

27-
error: methods called `is_*` usually take self by reference or no self; consider choosing a less ambiguous name
34+
error: methods called `is_*` usually take self by reference or no self
2835
--> $DIR/wrong_self_convention.rs:40:15
2936
|
3037
LL | fn is_i32(self) {}
3138
| ^^^^
39+
|
40+
= help: consider choosing a less ambiguous name
3241

33-
error: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
42+
error: methods called `to_*` usually take self by reference
3443
--> $DIR/wrong_self_convention.rs:42:15
3544
|
3645
LL | fn to_i32(self) {}
3746
| ^^^^
47+
|
48+
= help: consider choosing a less ambiguous name
3849

39-
error: methods called `from_*` usually take no self; consider choosing a less ambiguous name
50+
error: methods called `from_*` usually take no self
4051
--> $DIR/wrong_self_convention.rs:44:17
4152
|
4253
LL | fn from_i32(self) {}
4354
| ^^^^
55+
|
56+
= help: consider choosing a less ambiguous name
4457

45-
error: methods called `as_*` usually take self by reference or self by mutable reference; consider choosing a less ambiguous name
58+
error: methods called `as_*` usually take self by reference or self by mutable reference
4659
--> $DIR/wrong_self_convention.rs:46:19
4760
|
4861
LL | pub fn as_i64(self) {}
4962
| ^^^^
63+
|
64+
= help: consider choosing a less ambiguous name
5065

51-
error: methods called `into_*` usually take self by value; consider choosing a less ambiguous name
66+
error: methods called `into_*` usually take self by value
5267
--> $DIR/wrong_self_convention.rs:47:21
5368
|
5469
LL | pub fn into_i64(&self) {}
5570
| ^^^^^
71+
|
72+
= help: consider choosing a less ambiguous name
5673

57-
error: methods called `is_*` usually take self by reference or no self; consider choosing a less ambiguous name
74+
error: methods called `is_*` usually take self by reference or no self
5875
--> $DIR/wrong_self_convention.rs:48:19
5976
|
6077
LL | pub fn is_i64(self) {}
6178
| ^^^^
79+
|
80+
= help: consider choosing a less ambiguous name
6281

63-
error: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
82+
error: methods called `to_*` usually take self by reference
6483
--> $DIR/wrong_self_convention.rs:49:19
6584
|
6685
LL | pub fn to_i64(self) {}
6786
| ^^^^
87+
|
88+
= help: consider choosing a less ambiguous name
6889

69-
error: methods called `from_*` usually take no self; consider choosing a less ambiguous name
90+
error: methods called `from_*` usually take no self
7091
--> $DIR/wrong_self_convention.rs:50:21
7192
|
7293
LL | pub fn from_i64(self) {}
7394
| ^^^^
95+
|
96+
= help: consider choosing a less ambiguous name
7497

75-
error: methods called `as_*` usually take self by reference or self by mutable reference; consider choosing a less ambiguous name
98+
error: methods called `as_*` usually take self by reference or self by mutable reference
7699
--> $DIR/wrong_self_convention.rs:95:19
77100
|
78101
LL | fn as_i32(self) {}
79102
| ^^^^
103+
|
104+
= help: consider choosing a less ambiguous name
80105

81-
error: methods called `into_*` usually take self by value; consider choosing a less ambiguous name
106+
error: methods called `into_*` usually take self by value
82107
--> $DIR/wrong_self_convention.rs:98:25
83108
|
84109
LL | fn into_i32_ref(&self) {}
85110
| ^^^^^
111+
|
112+
= help: consider choosing a less ambiguous name
86113

87-
error: methods called `is_*` usually take self by reference or no self; consider choosing a less ambiguous name
114+
error: methods called `is_*` usually take self by reference or no self
88115
--> $DIR/wrong_self_convention.rs:100:19
89116
|
90117
LL | fn is_i32(self) {}
91118
| ^^^^
119+
|
120+
= help: consider choosing a less ambiguous name
92121

93-
error: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
122+
error: methods called `to_*` usually take self by reference
94123
--> $DIR/wrong_self_convention.rs:102:19
95124
|
96125
LL | fn to_i32(self) {}
97126
| ^^^^
127+
|
128+
= help: consider choosing a less ambiguous name
98129

99-
error: methods called `from_*` usually take no self; consider choosing a less ambiguous name
130+
error: methods called `from_*` usually take no self
100131
--> $DIR/wrong_self_convention.rs:104:21
101132
|
102133
LL | fn from_i32(self) {}
103134
| ^^^^
135+
|
136+
= help: consider choosing a less ambiguous name
104137

105-
error: methods called `as_*` usually take self by reference or self by mutable reference; consider choosing a less ambiguous name
138+
error: methods called `as_*` usually take self by reference or self by mutable reference
106139
--> $DIR/wrong_self_convention.rs:119:19
107140
|
108141
LL | fn as_i32(self);
109142
| ^^^^
143+
|
144+
= help: consider choosing a less ambiguous name
110145

111-
error: methods called `into_*` usually take self by value; consider choosing a less ambiguous name
146+
error: methods called `into_*` usually take self by value
112147
--> $DIR/wrong_self_convention.rs:122:25
113148
|
114149
LL | fn into_i32_ref(&self);
115150
| ^^^^^
151+
|
152+
= help: consider choosing a less ambiguous name
116153

117-
error: methods called `is_*` usually take self by reference or no self; consider choosing a less ambiguous name
154+
error: methods called `is_*` usually take self by reference or no self
118155
--> $DIR/wrong_self_convention.rs:124:19
119156
|
120157
LL | fn is_i32(self);
121158
| ^^^^
159+
|
160+
= help: consider choosing a less ambiguous name
122161

123-
error: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
162+
error: methods called `to_*` usually take self by reference
124163
--> $DIR/wrong_self_convention.rs:126:19
125164
|
126165
LL | fn to_i32(self);
127166
| ^^^^
167+
|
168+
= help: consider choosing a less ambiguous name
128169

129-
error: methods called `from_*` usually take no self; consider choosing a less ambiguous name
170+
error: methods called `from_*` usually take no self
130171
--> $DIR/wrong_self_convention.rs:128:21
131172
|
132173
LL | fn from_i32(self);
133174
| ^^^^
175+
|
176+
= help: consider choosing a less ambiguous name
134177

135-
error: methods called `into_*` usually take self by value; consider choosing a less ambiguous name
178+
error: methods called `into_*` usually take self by value
136179
--> $DIR/wrong_self_convention.rs:146:25
137180
|
138181
LL | fn into_i32_ref(&self);
139182
| ^^^^^
183+
|
184+
= help: consider choosing a less ambiguous name
140185

141-
error: methods called `from_*` usually take no self; consider choosing a less ambiguous name
186+
error: methods called `from_*` usually take no self
142187
--> $DIR/wrong_self_convention.rs:152:21
143188
|
144189
LL | fn from_i32(self);
145190
| ^^^^
146-
147-
error: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
148-
--> $DIR/wrong_self_convention.rs:175:24
149-
|
150-
LL | pub fn to_many(&mut self) -> Option<&mut [T]> {
151-
| ^^^^^^^^^
152-
153-
error: methods called like this: (`to_*` and `*_mut`) usually take self by mutable reference; consider choosing a less ambiguous name
154-
--> $DIR/wrong_self_convention.rs:183:28
155191
|
156-
LL | pub fn to_many_mut(&self) -> Option<&[T]> {
157-
| ^^^^^
192+
= help: consider choosing a less ambiguous name
158193

159-
error: aborting due to 26 previous errors
194+
error: aborting due to 24 previous errors
160195

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// edition:2018
2+
#![warn(clippy::wrong_self_convention)]
3+
#![allow(dead_code)]
4+
5+
fn main() {}
6+
7+
mod issue6758 {
8+
pub enum Test<T> {
9+
One(T),
10+
Many(Vec<T>),
11+
}
12+
13+
impl<T> Test<T> {
14+
// If a method starts with `to_` and not ends with `_mut` it should expect `&self`
15+
pub fn to_many(&mut self) -> Option<&mut [T]> {
16+
match self {
17+
Self::Many(data) => Some(data),
18+
_ => None,
19+
}
20+
}
21+
22+
// If a method starts with `to_` and ends with `_mut` it should expect `&mut self`
23+
pub fn to_many_mut(&self) -> Option<&[T]> {
24+
match self {
25+
Self::Many(data) => Some(data),
26+
_ => None,
27+
}
28+
}
29+
}
30+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error: methods called `to_*` usually take self by reference
2+
--> $DIR/wrong_self_conventions_mut.rs:15:24
3+
|
4+
LL | pub fn to_many(&mut self) -> Option<&mut [T]> {
5+
| ^^^^^^^^^
6+
|
7+
= note: `-D clippy::wrong-self-convention` implied by `-D warnings`
8+
= help: consider choosing a less ambiguous name
9+
10+
error: methods called like this: (`to_*` and `*_mut`) usually take self by mutable reference
11+
--> $DIR/wrong_self_conventions_mut.rs:23:28
12+
|
13+
LL | pub fn to_many_mut(&self) -> Option<&[T]> {
14+
| ^^^^^
15+
|
16+
= help: consider choosing a less ambiguous name
17+
18+
error: aborting due to 2 previous errors
19+

0 commit comments

Comments
 (0)