Skip to content

Commit 1f2d016

Browse files
committed
wrong_self_convention: Enhance lint message
1 parent 032cdfe commit 1f2d016

File tree

5 files changed

+42
-39
lines changed

5 files changed

+42
-39
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,10 +2348,10 @@ impl SelfKind {
23482348
#[must_use]
23492349
fn description(self) -> &'static str {
23502350
match self {
2351-
Self::Value => "self by value",
2352-
Self::Ref => "self by reference",
2353-
Self::RefMut => "self by mutable reference",
2354-
Self::No => "no self",
2351+
Self::Value => "`self` by value",
2352+
Self::Ref => "`self` by reference",
2353+
Self::RefMut => "`self` by mutable reference",
2354+
Self::No => "no `self`",
23552355
}
23562356
}
23572357
}

clippy_lints/src/methods/wrong_self_convention.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,16 @@ impl Convention {
5151
impl fmt::Display for Convention {
5252
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
5353
match *self {
54-
Self::Eq(this) => this.fmt(f),
55-
Self::StartsWith(this) => this.fmt(f).and_then(|_| '*'.fmt(f)),
56-
Self::EndsWith(this) => '*'.fmt(f).and_then(|_| this.fmt(f)),
57-
Self::NotEndsWith(this) => '~'.fmt(f).and_then(|_| this.fmt(f)),
58-
Self::IsSelfTypeCopy(is_true) => format!("self type is {} Copy", if is_true { "" } else { "not" }).fmt(f),
54+
Self::Eq(this) => format!("`{}`", this).fmt(f),
55+
Self::StartsWith(this) => format!("`{}*`", this).fmt(f),
56+
Self::EndsWith(this) => format!("`*{}`", this).fmt(f),
57+
Self::NotEndsWith(this) => format!("`~{}`", this).fmt(f),
58+
Self::IsSelfTypeCopy(is_true) => {
59+
format!("`self` type is{} `Copy`", if is_true { "" } else { " not" }).fmt(f)
60+
},
5961
Self::ImplementsTrait(is_true) => {
60-
format!("Method {} implement a trait", if is_true { "" } else { "do not" }).fmt(f)
62+
let (negation, s_suffix) = if is_true { ("", "s") } else { (" does not", "") };
63+
format!("Method{} implement{} a trait", negation, s_suffix).fmt(f)
6164
},
6265
}
6366
}
@@ -99,15 +102,15 @@ pub(super) fn check<'tcx>(
99102
{
100103
None
101104
} else {
102-
Some(format!("`{}`", &conv.to_string()))
105+
Some(conv.to_string())
103106
}
104107
})
105108
.collect::<Vec<_>>()
106109
.join(" and ");
107110

108111
format!("methods with the following characteristics: ({})", &s)
109112
} else {
110-
format!("methods called `{}`", &conventions[0])
113+
format!("methods called {}", &conventions[0])
111114
}
112115
};
113116

tests/ui/def_id_nocore.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: methods called `as_*` usually take self by reference or self by mutable reference
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 {

tests/ui/wrong_self_convention.stderr

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: methods called `from_*` usually take no self
1+
error: methods called `from_*` usually take no `self`
22
--> $DIR/wrong_self_convention.rs:18:17
33
|
44
LL | fn from_i32(self) {}
@@ -7,183 +7,183 @@ LL | fn from_i32(self) {}
77
= note: `-D clippy::wrong-self-convention` implied by `-D warnings`
88
= help: consider choosing a less ambiguous name
99

10-
error: methods called `from_*` usually take no self
10+
error: methods called `from_*` usually take no `self`
1111
--> $DIR/wrong_self_convention.rs:24:21
1212
|
1313
LL | pub fn from_i64(self) {}
1414
| ^^^^
1515
|
1616
= help: consider choosing a less ambiguous name
1717

18-
error: methods called `as_*` usually take self by reference or self by mutable reference
18+
error: methods called `as_*` usually take `self` by reference or `self` by mutable reference
1919
--> $DIR/wrong_self_convention.rs:36:15
2020
|
2121
LL | fn as_i32(self) {}
2222
| ^^^^
2323
|
2424
= help: consider choosing a less ambiguous name
2525

26-
error: methods called `into_*` usually take self by value
26+
error: methods called `into_*` usually take `self` by value
2727
--> $DIR/wrong_self_convention.rs:38:17
2828
|
2929
LL | fn into_i32(&self) {}
3030
| ^^^^^
3131
|
3232
= help: consider choosing a less ambiguous name
3333

34-
error: methods called `is_*` usually take self by reference or no self
34+
error: methods called `is_*` usually take `self` by reference or no `self`
3535
--> $DIR/wrong_self_convention.rs:40:15
3636
|
3737
LL | fn is_i32(self) {}
3838
| ^^^^
3939
|
4040
= help: consider choosing a less ambiguous name
4141

42-
error: methods with the following characteristics: (`to_*` and `self type is not Copy`) usually take self by reference
42+
error: methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference
4343
--> $DIR/wrong_self_convention.rs:42:15
4444
|
4545
LL | fn to_i32(self) {}
4646
| ^^^^
4747
|
4848
= help: consider choosing a less ambiguous name
4949

50-
error: methods called `from_*` usually take no self
50+
error: methods called `from_*` usually take no `self`
5151
--> $DIR/wrong_self_convention.rs:44:17
5252
|
5353
LL | fn from_i32(self) {}
5454
| ^^^^
5555
|
5656
= help: consider choosing a less ambiguous name
5757

58-
error: methods called `as_*` usually take self by reference or self by mutable reference
58+
error: methods called `as_*` usually take `self` by reference or `self` by mutable reference
5959
--> $DIR/wrong_self_convention.rs:46:19
6060
|
6161
LL | pub fn as_i64(self) {}
6262
| ^^^^
6363
|
6464
= help: consider choosing a less ambiguous name
6565

66-
error: methods called `into_*` usually take self by value
66+
error: methods called `into_*` usually take `self` by value
6767
--> $DIR/wrong_self_convention.rs:47:21
6868
|
6969
LL | pub fn into_i64(&self) {}
7070
| ^^^^^
7171
|
7272
= help: consider choosing a less ambiguous name
7373

74-
error: methods called `is_*` usually take self by reference or no self
74+
error: methods called `is_*` usually take `self` by reference or no `self`
7575
--> $DIR/wrong_self_convention.rs:48:19
7676
|
7777
LL | pub fn is_i64(self) {}
7878
| ^^^^
7979
|
8080
= help: consider choosing a less ambiguous name
8181

82-
error: methods with the following characteristics: (`to_*` and `self type is not Copy`) usually take self by reference
82+
error: methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference
8383
--> $DIR/wrong_self_convention.rs:49:19
8484
|
8585
LL | pub fn to_i64(self) {}
8686
| ^^^^
8787
|
8888
= help: consider choosing a less ambiguous name
8989

90-
error: methods called `from_*` usually take no self
90+
error: methods called `from_*` usually take no `self`
9191
--> $DIR/wrong_self_convention.rs:50:21
9292
|
9393
LL | pub fn from_i64(self) {}
9494
| ^^^^
9595
|
9696
= help: consider choosing a less ambiguous name
9797

98-
error: methods called `as_*` usually take self by reference or self by mutable reference
98+
error: methods called `as_*` usually take `self` by reference or `self` by mutable reference
9999
--> $DIR/wrong_self_convention.rs:95:19
100100
|
101101
LL | fn as_i32(self) {}
102102
| ^^^^
103103
|
104104
= help: consider choosing a less ambiguous name
105105

106-
error: methods called `into_*` usually take self by value
106+
error: methods called `into_*` usually take `self` by value
107107
--> $DIR/wrong_self_convention.rs:98:25
108108
|
109109
LL | fn into_i32_ref(&self) {}
110110
| ^^^^^
111111
|
112112
= help: consider choosing a less ambiguous name
113113

114-
error: methods called `is_*` usually take self by reference or no self
114+
error: methods called `is_*` usually take `self` by reference or no `self`
115115
--> $DIR/wrong_self_convention.rs:100:19
116116
|
117117
LL | fn is_i32(self) {}
118118
| ^^^^
119119
|
120120
= help: consider choosing a less ambiguous name
121121

122-
error: methods called `from_*` usually take no self
122+
error: methods called `from_*` usually take no `self`
123123
--> $DIR/wrong_self_convention.rs:104:21
124124
|
125125
LL | fn from_i32(self) {}
126126
| ^^^^
127127
|
128128
= help: consider choosing a less ambiguous name
129129

130-
error: methods called `as_*` usually take self by reference or self by mutable reference
130+
error: methods called `as_*` usually take `self` by reference or `self` by mutable reference
131131
--> $DIR/wrong_self_convention.rs:119:19
132132
|
133133
LL | fn as_i32(self);
134134
| ^^^^
135135
|
136136
= help: consider choosing a less ambiguous name
137137

138-
error: methods called `into_*` usually take self by value
138+
error: methods called `into_*` usually take `self` by value
139139
--> $DIR/wrong_self_convention.rs:122:25
140140
|
141141
LL | fn into_i32_ref(&self);
142142
| ^^^^^
143143
|
144144
= help: consider choosing a less ambiguous name
145145

146-
error: methods called `is_*` usually take self by reference or no self
146+
error: methods called `is_*` usually take `self` by reference or no `self`
147147
--> $DIR/wrong_self_convention.rs:124:19
148148
|
149149
LL | fn is_i32(self);
150150
| ^^^^
151151
|
152152
= help: consider choosing a less ambiguous name
153153

154-
error: methods called `from_*` usually take no self
154+
error: methods called `from_*` usually take no `self`
155155
--> $DIR/wrong_self_convention.rs:128:21
156156
|
157157
LL | fn from_i32(self);
158158
| ^^^^
159159
|
160160
= help: consider choosing a less ambiguous name
161161

162-
error: methods called `into_*` usually take self by value
162+
error: methods called `into_*` usually take `self` by value
163163
--> $DIR/wrong_self_convention.rs:146:25
164164
|
165165
LL | fn into_i32_ref(&self);
166166
| ^^^^^
167167
|
168168
= help: consider choosing a less ambiguous name
169169

170-
error: methods called `from_*` usually take no self
170+
error: methods called `from_*` usually take no `self`
171171
--> $DIR/wrong_self_convention.rs:152:21
172172
|
173173
LL | fn from_i32(self);
174174
| ^^^^
175175
|
176176
= help: consider choosing a less ambiguous name
177177

178-
error: methods with the following characteristics: (`to_*` and `self type is Copy`) usually take self by value
178+
error: methods with the following characteristics: (`to_*` and `self` type is `Copy`) usually take `self` by value
179179
--> $DIR/wrong_self_convention.rs:181:22
180180
|
181181
LL | fn to_u64_v2(&self) -> u64 {
182182
| ^^^^^
183183
|
184184
= help: consider choosing a less ambiguous name
185185

186-
error: methods with the following characteristics: (`to_*` and `self type is not Copy`) usually take self by reference
186+
error: methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference
187187
--> $DIR/wrong_self_convention.rs:190:19
188188
|
189189
LL | fn to_u64(self) -> u64 {

tests/ui/wrong_self_conventions_mut.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: methods with the following characteristics: (`to_*` and `self type is not Copy`) usually take self by reference
1+
error: methods with the following characteristics: (`to_*` and `self` type is not `Copy`) usually take `self` by reference
22
--> $DIR/wrong_self_conventions_mut.rs:15:24
33
|
44
LL | pub fn to_many(&mut self) -> Option<&mut [T]> {
@@ -7,7 +7,7 @@ LL | pub fn to_many(&mut self) -> Option<&mut [T]> {
77
= note: `-D clippy::wrong-self-convention` implied by `-D warnings`
88
= help: consider choosing a less ambiguous name
99

10-
error: methods with the following characteristics: (`to_*` and `*_mut`) usually take self by mutable reference
10+
error: methods with the following characteristics: (`to_*` and `*_mut`) usually take `self` by mutable reference
1111
--> $DIR/wrong_self_conventions_mut.rs:23:28
1212
|
1313
LL | pub fn to_many_mut(&self) -> Option<&[T]> {

0 commit comments

Comments
 (0)