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: Match SelfKind::No more restrictively #8208

Merged
merged 1 commit into from
Jan 2, 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
8 changes: 7 additions & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2535,11 +2535,17 @@ impl SelfKind {
implements_trait(cx, ty, trait_def_id, &[parent_ty.into()])
}

fn matches_none<'a>(cx: &LateContext<'a>, parent_ty: Ty<'a>, ty: Ty<'a>) -> bool {
!matches_value(cx, parent_ty, ty)
&& !matches_ref(cx, hir::Mutability::Not, parent_ty, ty)
&& !matches_ref(cx, hir::Mutability::Mut, parent_ty, ty)
}

match self {
Self::Value => matches_value(cx, parent_ty, ty),
Self::Ref => matches_ref(cx, hir::Mutability::Not, parent_ty, ty) || ty == parent_ty && is_copy(cx, ty),
Self::RefMut => matches_ref(cx, hir::Mutability::Mut, parent_ty, ty),
Self::No => ty != parent_ty,
Self::No => matches_none(cx, parent_ty, ty),
}
}

Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/redundant_clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
continue;
} else if let Some(loc) = clone_usage.cloned_consume_or_mutate_loc {
// cloned value is mutated, and the clone is alive.
if possible_borrower.is_alive_at(ret_local, loc) {
if possible_borrower.local_is_alive_at(ret_local, loc) {
continue;
}
}
Expand Down Expand Up @@ -767,7 +767,7 @@ impl PossibleBorrowerMap<'_, '_> {
self.bitset.0 == self.bitset.1
}

fn is_alive_at(&mut self, local: mir::Local, at: mir::Location) -> bool {
fn local_is_alive_at(&mut self, local: mir::Local, at: mir::Location) -> bool {
self.maybe_live.seek_after_primary_effect(at);
self.maybe_live.contains(local)
}
Expand Down
11 changes: 10 additions & 1 deletion tests/ui/issue_4266.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ error: explicit lifetimes given in parameter types where they could be elided (o
LL | async fn one_to_one<'a>(s: &'a str) -> &'a str {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

error: aborting due to 3 previous errors

21 changes: 21 additions & 0 deletions tests/ui/wrong_self_convention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,24 @@ mod issue6727 {
}
}
}

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
}

// Should not lint: &self is allowed.
fn is_test_code(&self) -> bool {
true
}
}
}
10 changes: 9 additions & 1 deletion tests/ui/wrong_self_convention.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,13 @@ LL | fn to_u64(self) -> u64 {
|
= help: consider choosing a less ambiguous name

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