Skip to content

Commit 77278cc

Browse files
author
Michael Wright
committed
Fix wrong_self_convention issue
Resolves rust-lang#4293
1 parent 170d486 commit 77278cc

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2547,6 +2547,22 @@ enum SelfKind {
25472547

25482548
impl SelfKind {
25492549
fn matches<'a>(self, cx: &LateContext<'_, 'a>, parent_ty: Ty<'a>, ty: Ty<'a>) -> bool {
2550+
fn matches_value(parent_ty: Ty<'_>, ty: Ty<'_>) -> bool {
2551+
if ty == parent_ty {
2552+
true
2553+
} else if ty.is_box() {
2554+
ty.boxed_ty() == parent_ty
2555+
} else if ty.is_rc() || ty.is_arc() {
2556+
if let ty::Adt(_, substs) = ty.sty {
2557+
substs.types().next().map_or(false, |t| t == parent_ty)
2558+
} else {
2559+
false
2560+
}
2561+
} else {
2562+
false
2563+
}
2564+
}
2565+
25502566
fn matches_ref<'a>(
25512567
cx: &LateContext<'_, 'a>,
25522568
mutability: hir::Mutability,
@@ -2567,7 +2583,7 @@ impl SelfKind {
25672583
}
25682584

25692585
match self {
2570-
Self::Value => ty == parent_ty,
2586+
Self::Value => matches_value(parent_ty, ty),
25712587
Self::Ref => {
25722588
matches_ref(cx, hir::Mutability::MutImmutable, parent_ty, ty) || ty == parent_ty && is_copy(cx, ty)
25732589
},

tests/ui/wrong_self_convention.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,22 @@ impl Bar {
5656
fn from_(self) {}
5757
fn to_mut(&mut self) {}
5858
}
59+
60+
// Allow Box<Self>, Rc<Self>, Arc<Self> for methods that take conventionally take Self by value
61+
#[allow(clippy::boxed_local)]
62+
mod issue4293 {
63+
use std::rc::Rc;
64+
use std::sync::Arc;
65+
66+
struct T;
67+
68+
impl T {
69+
fn into_s1(self: Box<Self>) {}
70+
fn into_s2(self: Rc<Self>) {}
71+
fn into_s3(self: Arc<Self>) {}
72+
73+
fn into_t1(self: Box<T>) {}
74+
fn into_t2(self: Rc<T>) {}
75+
fn into_t3(self: Arc<T>) {}
76+
}
77+
}

0 commit comments

Comments
 (0)