From f142098474fbe4d46bd20227ae318adf168302dc Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Mon, 1 Oct 2018 04:47:06 -0700 Subject: [PATCH] Correct false positive in wrong_self_convention lint for to_mut --- clippy_lints/src/methods/mod.rs | 18 ++++++++++-------- tests/ui/wrong_self_convention.rs | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 2524152a1..e0d858bd2 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -882,12 +882,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { let ty = cx.tcx.type_of(def_id); let is_copy = is_copy(cx, ty); for &(ref conv, self_kinds) in &CONVENTIONS { - if_chain! { - if conv.check(&name.as_str()); + if conv.check(&name.as_str()) { if !self_kinds - .iter() - .any(|k| k.matches(cx, first_arg_ty, first_arg, self_ty, is_copy, &implitem.generics)); - then { + .iter() + .any(|k| k.matches(cx, first_arg_ty, first_arg, self_ty, is_copy, &implitem.generics)) { let lint = if item.vis.node.is_pub() { WRONG_PUB_SELF_CONVENTION } else { @@ -904,6 +902,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { .collect::>() .join(" or "))); } + + // Only check the first convention to match (CONVENTIONS should be listed from most to least specific) + break; } } @@ -1183,8 +1184,8 @@ fn lint_clone_on_copy(cx: &LateContext<'_, '_>, expr: &hir::Expr, arg: &hir::Exp Applicability::MaybeIncorrect, ); db.span_suggestion_with_applicability( - expr.span, - "or try being explicit about what type to clone", + expr.span, + "or try being explicit about what type to clone", explicit, Applicability::MaybeIncorrect, ); @@ -2067,12 +2068,13 @@ enum Convention { } #[rustfmt::skip] -const CONVENTIONS: [(Convention, &[SelfKind]); 6] = [ +const CONVENTIONS: [(Convention, &[SelfKind]); 7] = [ (Convention::Eq("new"), &[SelfKind::No]), (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::Eq("to_mut"), &[SelfKind::RefMut]), (Convention::StartsWith("to_"), &[SelfKind::Ref]), ]; diff --git a/tests/ui/wrong_self_convention.rs b/tests/ui/wrong_self_convention.rs index 1e718c1c6..2fb33d086 100644 --- a/tests/ui/wrong_self_convention.rs +++ b/tests/ui/wrong_self_convention.rs @@ -59,4 +59,5 @@ impl Bar { fn is_(self) {} fn to_(self) {} fn from_(self) {} + fn to_mut(&mut self) {} }