Skip to content

Commit 11cc847

Browse files
authored
Merge pull request #3245 from JoshMcguigan/wrong_self_convention-1530
Correct false positive in wrong_self_convention lint for to_mut
2 parents 902aca7 + f142098 commit 11cc847

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -882,12 +882,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
882882
let ty = cx.tcx.type_of(def_id);
883883
let is_copy = is_copy(cx, ty);
884884
for &(ref conv, self_kinds) in &CONVENTIONS {
885-
if_chain! {
886-
if conv.check(&name.as_str());
885+
if conv.check(&name.as_str()) {
887886
if !self_kinds
888-
.iter()
889-
.any(|k| k.matches(cx, first_arg_ty, first_arg, self_ty, is_copy, &implitem.generics));
890-
then {
887+
.iter()
888+
.any(|k| k.matches(cx, first_arg_ty, first_arg, self_ty, is_copy, &implitem.generics)) {
891889
let lint = if item.vis.node.is_pub() {
892890
WRONG_PUB_SELF_CONVENTION
893891
} else {
@@ -904,6 +902,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
904902
.collect::<Vec<_>>()
905903
.join(" or ")));
906904
}
905+
906+
// Only check the first convention to match (CONVENTIONS should be listed from most to least specific)
907+
break;
907908
}
908909
}
909910

@@ -1183,8 +1184,8 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr, arg: &hir::Exp
11831184
Applicability::MaybeIncorrect,
11841185
);
11851186
db.span_suggestion_with_applicability(
1186-
expr.span,
1187-
"or try being explicit about what type to clone",
1187+
expr.span,
1188+
"or try being explicit about what type to clone",
11881189
explicit,
11891190
Applicability::MaybeIncorrect,
11901191
);
@@ -2067,12 +2068,13 @@ enum Convention {
20672068
}
20682069

20692070
#[rustfmt::skip]
2070-
const CONVENTIONS: [(Convention, &[SelfKind]); 6] = [
2071+
const CONVENTIONS: [(Convention, &[SelfKind]); 7] = [
20712072
(Convention::Eq("new"), &[SelfKind::No]),
20722073
(Convention::StartsWith("as_"), &[SelfKind::Ref, SelfKind::RefMut]),
20732074
(Convention::StartsWith("from_"), &[SelfKind::No]),
20742075
(Convention::StartsWith("into_"), &[SelfKind::Value]),
20752076
(Convention::StartsWith("is_"), &[SelfKind::Ref, SelfKind::No]),
2077+
(Convention::Eq("to_mut"), &[SelfKind::RefMut]),
20762078
(Convention::StartsWith("to_"), &[SelfKind::Ref]),
20772079
];
20782080

tests/ui/wrong_self_convention.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ impl Bar {
5959
fn is_(self) {}
6060
fn to_(self) {}
6161
fn from_(self) {}
62+
fn to_mut(&mut self) {}
6263
}

0 commit comments

Comments
 (0)