Skip to content

Commit 6aa3684

Browse files
committed
Auto merge of rust-lang#8738 - tamaroning:fix_wrong_self_convention, r=xFrednet
wrong_self_convention allows `is_*` to take `&mut self` fix rust-lang#8480 and rust-lang#8513 Allowing `is_*` to take `&self` or none is too restrictive. changelog: FPs: [`wrong_self_convention`] now allows `&mut self` and no self as arguments for `is_*` methods
2 parents 2a5ee68 + 51db157 commit 6aa3684

File tree

5 files changed

+25
-28
lines changed

5 files changed

+25
-28
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,15 @@ declare_clippy_lint! {
296296
/// Checks for methods with certain name prefixes and which
297297
/// doesn't match how self is taken. The actual rules are:
298298
///
299-
/// |Prefix |Postfix |`self` taken | `self` type |
300-
/// |-------|------------|-----------------------|--------------|
301-
/// |`as_` | none |`&self` or `&mut self` | any |
302-
/// |`from_`| none | none | any |
303-
/// |`into_`| none |`self` | any |
304-
/// |`is_` | none |`&self` or none | any |
305-
/// |`to_` | `_mut` |`&mut self` | any |
306-
/// |`to_` | not `_mut` |`self` | `Copy` |
307-
/// |`to_` | not `_mut` |`&self` | not `Copy` |
299+
/// |Prefix |Postfix |`self` taken | `self` type |
300+
/// |-------|------------|-------------------------------|--------------|
301+
/// |`as_` | none |`&self` or `&mut self` | any |
302+
/// |`from_`| none | none | any |
303+
/// |`into_`| none |`self` | any |
304+
/// |`is_` | none |`&mut self` or `&self` or none | any |
305+
/// |`to_` | `_mut` |`&mut self` | any |
306+
/// |`to_` | not `_mut` |`self` | `Copy` |
307+
/// |`to_` | not `_mut` |`&self` | not `Copy` |
308308
///
309309
/// Note: Clippy doesn't trigger methods with `to_` prefix in:
310310
/// - Traits definition.

clippy_lints/src/methods/wrong_self_convention.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const CONVENTIONS: [(&[Convention], &[SelfKind]); 9] = [
1414
(&[Convention::StartsWith("as_")], &[SelfKind::Ref, SelfKind::RefMut]),
1515
(&[Convention::StartsWith("from_")], &[SelfKind::No]),
1616
(&[Convention::StartsWith("into_")], &[SelfKind::Value]),
17-
(&[Convention::StartsWith("is_")], &[SelfKind::Ref, SelfKind::No]),
17+
(&[Convention::StartsWith("is_")], &[SelfKind::RefMut, SelfKind::Ref, SelfKind::No]),
1818
(&[Convention::Eq("to_mut")], &[SelfKind::RefMut]),
1919
(&[Convention::StartsWith("to_"), Convention::EndsWith("_mut")], &[SelfKind::RefMut]),
2020

tests/ui/wrong_self_convention.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,6 @@ pub mod issue8142 {
193193
struct S;
194194

195195
impl S {
196-
// Should lint: is_ methods should only take &self, or no self at all.
197-
fn is_still_buggy(&mut self) -> bool {
198-
false
199-
}
200-
201196
// Should not lint: "no self at all" is allowed.
202197
fn is_forty_two(x: u32) -> bool {
203198
x == 42

tests/ui/wrong_self_convention.stderr

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ LL | fn into_i32(&self) {}
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 mutable reference or `self` by reference or no `self`
3535
--> $DIR/wrong_self_convention.rs:38:15
3636
|
3737
LL | fn is_i32(self) {}
@@ -71,7 +71,7 @@ LL | pub fn into_i64(&self) {}
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 mutable reference or `self` by reference or no `self`
7575
--> $DIR/wrong_self_convention.rs:46:19
7676
|
7777
LL | pub fn is_i64(self) {}
@@ -111,7 +111,7 @@ LL | fn into_i32_ref(&self) {}
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 mutable reference or `self` by reference or no `self`
115115
--> $DIR/wrong_self_convention.rs:98:19
116116
|
117117
LL | fn is_i32(self) {}
@@ -143,7 +143,7 @@ LL | fn into_i32_ref(&self);
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 mutable reference or `self` by reference or no `self`
147147
--> $DIR/wrong_self_convention.rs:122:19
148148
|
149149
LL | fn is_i32(self);
@@ -191,13 +191,5 @@ LL | fn to_u64(self) -> u64 {
191191
|
192192
= help: consider choosing a less ambiguous name
193193

194-
error: methods called `is_*` usually take `self` by reference or no `self`
195-
--> $DIR/wrong_self_convention.rs:197:27
196-
|
197-
LL | fn is_still_buggy(&mut self) -> bool {
198-
| ^^^^^^^^^
199-
|
200-
= help: consider choosing a less ambiguous name
201-
202-
error: aborting due to 25 previous errors
194+
error: aborting due to 24 previous errors
203195

tests/ui/wrong_self_convention2.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,13 @@ mod issue4546 {
104104
pub fn to_other_thingy(self: Pin<&Self>) {}
105105
}
106106
}
107+
108+
mod issue_8480_8513 {
109+
struct Cat(String);
110+
111+
impl Cat {
112+
fn is_animal(&mut self) -> bool {
113+
todo!();
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)