Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong_self_convention allows is_* to take &mut self #8738

Merged
merged 1 commit into from
Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,15 @@ declare_clippy_lint! {
/// Checks for methods with certain name prefixes and which
/// doesn't match how self is taken. The actual rules are:
///
/// |Prefix |Postfix |`self` taken | `self` type |
/// |-------|------------|-----------------------|--------------|
/// |`as_` | none |`&self` or `&mut self` | any |
/// |`from_`| none | none | any |
/// |`into_`| none |`self` | any |
/// |`is_` | none |`&self` or none | any |
/// |`to_` | `_mut` |`&mut self` | any |
/// |`to_` | not `_mut` |`self` | `Copy` |
/// |`to_` | not `_mut` |`&self` | not `Copy` |
/// |Prefix |Postfix |`self` taken | `self` type |
/// |-------|------------|-------------------------------|--------------|
/// |`as_` | none |`&self` or `&mut self` | any |
/// |`from_`| none | none | any |
/// |`into_`| none |`self` | any |
/// |`is_` | none |`&mut self` or `&self` or none | any |
/// |`to_` | `_mut` |`&mut self` | any |
/// |`to_` | not `_mut` |`self` | `Copy` |
/// |`to_` | not `_mut` |`&self` | not `Copy` |
///
/// Note: Clippy doesn't trigger methods with `to_` prefix in:
/// - Traits definition.
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/wrong_self_convention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const CONVENTIONS: [(&[Convention], &[SelfKind]); 9] = [
(&[Convention::StartsWith("as_")], &[SelfKind::Ref, SelfKind::RefMut]),
(&[Convention::StartsWith("from_")], &[SelfKind::No]),
(&[Convention::StartsWith("into_")], &[SelfKind::Value]),
(&[Convention::StartsWith("is_")], &[SelfKind::Ref, SelfKind::No]),
(&[Convention::StartsWith("is_")], &[SelfKind::RefMut, SelfKind::Ref, SelfKind::No]),
(&[Convention::Eq("to_mut")], &[SelfKind::RefMut]),
(&[Convention::StartsWith("to_"), Convention::EndsWith("_mut")], &[SelfKind::RefMut]),

Expand Down
5 changes: 0 additions & 5 deletions tests/ui/wrong_self_convention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,6 @@ pub mod issue8142 {
struct S;

impl S {
// Should lint: is_ methods should only take &self, or no self at all.
fn is_still_buggy(&mut self) -> bool {
false
}

// Should not lint: "no self at all" is allowed.
fn is_forty_two(x: u32) -> bool {
x == 42
Expand Down
18 changes: 5 additions & 13 deletions tests/ui/wrong_self_convention.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ LL | fn into_i32(&self) {}
|
= help: consider choosing a less ambiguous name

error: methods called `is_*` usually take `self` by reference or no `self`
error: methods called `is_*` usually take `self` by mutable reference or `self` by reference or no `self`
--> $DIR/wrong_self_convention.rs:38:15
|
LL | fn is_i32(self) {}
Expand Down Expand Up @@ -71,7 +71,7 @@ LL | pub fn into_i64(&self) {}
|
= help: consider choosing a less ambiguous name

error: methods called `is_*` usually take `self` by reference or no `self`
error: methods called `is_*` usually take `self` by mutable reference or `self` by reference or no `self`
--> $DIR/wrong_self_convention.rs:46:19
|
LL | pub fn is_i64(self) {}
Expand Down Expand Up @@ -111,7 +111,7 @@ LL | fn into_i32_ref(&self) {}
|
= help: consider choosing a less ambiguous name

error: methods called `is_*` usually take `self` by reference or no `self`
error: methods called `is_*` usually take `self` by mutable reference or `self` by reference or no `self`
--> $DIR/wrong_self_convention.rs:98:19
|
LL | fn is_i32(self) {}
Expand Down Expand Up @@ -143,7 +143,7 @@ LL | fn into_i32_ref(&self);
|
= help: consider choosing a less ambiguous name

error: methods called `is_*` usually take `self` by reference or no `self`
error: methods called `is_*` usually take `self` by mutable reference or `self` by reference or no `self`
--> $DIR/wrong_self_convention.rs:122:19
|
LL | fn is_i32(self);
Expand Down Expand Up @@ -191,13 +191,5 @@ LL | fn to_u64(self) -> u64 {
|
= help: consider choosing a less ambiguous name

error: methods called `is_*` usually take `self` by reference or no `self`
--> $DIR/wrong_self_convention.rs:197:27
|
LL | fn is_still_buggy(&mut self) -> bool {
| ^^^^^^^^^
|
= help: consider choosing a less ambiguous name

error: aborting due to 25 previous errors
error: aborting due to 24 previous errors

10 changes: 10 additions & 0 deletions tests/ui/wrong_self_convention2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,13 @@ mod issue4546 {
pub fn to_other_thingy(self: Pin<&Self>) {}
}
}

mod issue_8480_8513 {
struct Cat(String);

impl Cat {
fn is_animal(&mut self) -> bool {
todo!();
}
}
}