Skip to content

Commit 3d41358

Browse files
committed
wrong_self_convention: Match SelfKind::No more restrictively
The `wrong_self_convention` lint uses a `SelfKind` type to decide whether a method has the right kind of "self" for its name, or whether the kind of "self" it has makes its name confusable for a method in a common trait. One possibility is `SelfKind::No`, which is supposed to mean "No `self`". Previously, SelfKind::No matched everything _except_ Self, including references to Self. This patch changes it to match Self, &Self, &mut Self, Box<Self>, and so on. For example, this kind of method was allowed before: ``` impl S { // Should trigger the lint, because // "methods called `is_*` usually take `self` by reference or no `self`" fn is_foo(&mut self) -> bool { todo!() } } ``` But since SelfKind::No matched "&mut self", no lint was triggered (see rust-lang#8142). With this patch, the code above now gives a lint as expected. Fixes rust-lang#8142 changelog: [`wrong_self_convention`] rejects `self` references in more cases
1 parent c736a63 commit 3d41358

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

clippy_lints/src/methods/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2535,11 +2535,17 @@ impl SelfKind {
25352535
implements_trait(cx, ty, trait_def_id, &[parent_ty.into()])
25362536
}
25372537

2538+
fn matches_none<'a>(cx: &LateContext<'a>, parent_ty: Ty<'a>, ty: Ty<'a>) -> bool {
2539+
!matches_value(cx, parent_ty, ty)
2540+
&& !matches_ref(cx, hir::Mutability::Not, parent_ty, ty)
2541+
&& !matches_ref(cx, hir::Mutability::Mut, parent_ty, ty)
2542+
}
2543+
25382544
match self {
25392545
Self::Value => matches_value(cx, parent_ty, ty),
25402546
Self::Ref => matches_ref(cx, hir::Mutability::Not, parent_ty, ty) || ty == parent_ty && is_copy(cx, ty),
25412547
Self::RefMut => matches_ref(cx, hir::Mutability::Mut, parent_ty, ty),
2542-
Self::No => ty != parent_ty,
2548+
Self::No => matches_none(cx, parent_ty, ty),
25432549
}
25442550
}
25452551

clippy_lints/src/redundant_clone.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
220220
continue;
221221
} else if let Some(loc) = clone_usage.cloned_consume_or_mutate_loc {
222222
// cloned value is mutated, and the clone is alive.
223-
if possible_borrower.is_alive_at(ret_local, loc) {
223+
if possible_borrower.local_is_alive_at(ret_local, loc) {
224224
continue;
225225
}
226226
}
@@ -767,7 +767,7 @@ impl PossibleBorrowerMap<'_, '_> {
767767
self.bitset.0 == self.bitset.1
768768
}
769769

770-
fn is_alive_at(&mut self, local: mir::Local, at: mir::Location) -> bool {
770+
fn local_is_alive_at(&mut self, local: mir::Local, at: mir::Location) -> bool {
771771
self.maybe_live.seek_after_primary_effect(at);
772772
self.maybe_live.contains(local)
773773
}

tests/ui/issue_4266.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,14 @@ error: explicit lifetimes given in parameter types where they could be elided (o
1212
LL | async fn one_to_one<'a>(s: &'a str) -> &'a str {
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

15-
error: aborting due to 2 previous errors
15+
error: methods called `new` usually take no `self`
16+
--> $DIR/issue_4266.rs:27:22
17+
|
18+
LL | pub async fn new(&mut self) -> Self {
19+
| ^^^^^^^^^
20+
|
21+
= note: `-D clippy::wrong-self-convention` implied by `-D warnings`
22+
= help: consider choosing a less ambiguous name
23+
24+
error: aborting due to 3 previous errors
1625

tests/ui/wrong_self_convention.rs

+21
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,24 @@ mod issue6727 {
188188
}
189189
}
190190
}
191+
192+
pub mod issue8142 {
193+
struct S;
194+
195+
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+
201+
// Should not lint: "no self at all" is allowed.
202+
fn is_forty_two(x: u32) -> bool {
203+
x == 42
204+
}
205+
206+
// Should not lint: &self is allowed.
207+
fn is_test_code(&self) -> bool {
208+
true
209+
}
210+
}
211+
}

tests/ui/wrong_self_convention.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,13 @@ LL | fn to_u64(self) -> u64 {
191191
|
192192
= help: consider choosing a less ambiguous name
193193

194-
error: aborting due to 24 previous errors
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
195203

0 commit comments

Comments
 (0)