Skip to content

Commit 25ac230

Browse files
committed
wrong_self_conversion: don't mention ~_mut at the end in the message
1 parent 6df6808 commit 25ac230

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,13 +1954,32 @@ fn lint_wrong_self_convention<'tcx>(
19541954
if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
19551955
let suggestion = {
19561956
if conventions.len() > 1 {
1957-
let s = conventions
1958-
.iter()
1959-
.map(|c| format!("`{}`", &c.to_string()))
1960-
.collect::<Vec<_>>()
1961-
.join(" and ");
1962-
1963-
format!("methods called like this: ({})", &s)
1957+
let special_case = {
1958+
// Don't mention `NotEndsWith` when there is also `StartsWith` convention present
1959+
if conventions.len() == 2 {
1960+
match conventions {
1961+
[Convention::StartsWith(starts_with), Convention::NotEndsWith(_)]
1962+
| [Convention::NotEndsWith(_), Convention::StartsWith(starts_with)] => {
1963+
Some(format!("methods called `{}`", Convention::StartsWith(starts_with)))
1964+
},
1965+
_ => None,
1966+
}
1967+
} else {
1968+
None
1969+
}
1970+
};
1971+
match special_case {
1972+
Some(suggestion) => suggestion,
1973+
None => {
1974+
let s = conventions
1975+
.iter()
1976+
.map(|c| format!("`{}`", &c.to_string()))
1977+
.collect::<Vec<_>>()
1978+
.join(" and ");
1979+
1980+
format!("methods called like this: ({})", &s)
1981+
},
1982+
}
19641983
} else {
19651984
format!("methods called `{}`", &conventions[0])
19661985
}

tests/ui/wrong_self_convention.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ error: methods called `is_*` usually take self by reference or no self; consider
3030
LL | fn is_i32(self) {}
3131
| ^^^^
3232

33-
error: methods called like this: (`to_*` and `~_mut`) usually take self by reference; consider choosing a less ambiguous name
33+
error: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
3434
--> $DIR/wrong_self_convention.rs:42:15
3535
|
3636
LL | fn to_i32(self) {}
@@ -60,7 +60,7 @@ error: methods called `is_*` usually take self by reference or no self; consider
6060
LL | pub fn is_i64(self) {}
6161
| ^^^^
6262

63-
error: methods called like this: (`to_*` and `~_mut`) usually take self by reference; consider choosing a less ambiguous name
63+
error: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
6464
--> $DIR/wrong_self_convention.rs:49:19
6565
|
6666
LL | pub fn to_i64(self) {}
@@ -90,7 +90,7 @@ error: methods called `is_*` usually take self by reference or no self; consider
9090
LL | fn is_i32(self) {}
9191
| ^^^^
9292

93-
error: methods called like this: (`to_*` and `~_mut`) usually take self by reference; consider choosing a less ambiguous name
93+
error: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
9494
--> $DIR/wrong_self_convention.rs:102:19
9595
|
9696
LL | fn to_i32(self) {}
@@ -120,7 +120,7 @@ error: methods called `is_*` usually take self by reference or no self; consider
120120
LL | fn is_i32(self);
121121
| ^^^^
122122

123-
error: methods called like this: (`to_*` and `~_mut`) usually take self by reference; consider choosing a less ambiguous name
123+
error: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
124124
--> $DIR/wrong_self_convention.rs:126:19
125125
|
126126
LL | fn to_i32(self);
@@ -144,7 +144,7 @@ error: methods called `from_*` usually take no self; consider choosing a less am
144144
LL | fn from_i32(self);
145145
| ^^^^
146146

147-
error: methods called like this: (`to_*` and `~_mut`) usually take self by reference; consider choosing a less ambiguous name
147+
error: methods called `to_*` usually take self by reference; consider choosing a less ambiguous name
148148
--> $DIR/wrong_self_convention.rs:175:24
149149
|
150150
LL | pub fn to_many(&mut self) -> Option<&mut [T]> {

0 commit comments

Comments
 (0)