about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThibsG <Thibs@debian.com>2020-11-10 08:56:17 +0100
committerThibsG <Thibs@debian.com>2020-12-10 17:08:42 +0100
commit4af9382bec3f49d9c7e0f03e9cfb5e8f1c70fdbe (patch)
treee5c34ceace932bee158fce27e2b4e65cd45f30ab
parenta6bb9276f7964b96899d04d680bc04bf99c8bf47 (diff)
downloadrust-4af9382bec3f49d9c7e0f03e9cfb5e8f1c70fdbe.tar.gz
rust-4af9382bec3f49d9c7e0f03e9cfb5e8f1c70fdbe.zip
Common function to lint wrong self convention from impl and trait def
-rw-r--r--clippy_lints/src/methods/mod.rs88
-rw-r--r--tests/ui/wrong_self_convention.rs6
-rw-r--r--tests/ui/wrong_self_convention.stderr2
3 files changed, 45 insertions, 51 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 3c89c1b6ed2..9a082a89229 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -1674,32 +1674,14 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
                     }
                 }
 
-                if let Some((ref conv, self_kinds)) = &CONVENTIONS
-                    .iter()
-                    .find(|(ref conv, _)| conv.check(&name))
-                {
-                    if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
-                        let lint = if item.vis.node.is_pub() {
-                            WRONG_PUB_SELF_CONVENTION
-                        } else {
-                            WRONG_SELF_CONVENTION
-                        };
-
-                        span_lint(
-                            cx,
-                            lint,
-                            first_arg.pat.span,
-                            &format!("methods called `{}` usually take {}; consider choosing a less ambiguous name",
-                                conv,
-                                &self_kinds
-                                    .iter()
-                                    .map(|k| k.description())
-                                    .collect::<Vec<_>>()
-                                    .join(" or ")
-                            ),
-                        );
-                    }
-                }
+                lint_wrong_self_convention(
+                    cx,
+                    &name,
+                    item.vis.node.is_pub(),
+                    self_ty,
+                    first_arg_ty,
+                    first_arg.pat.span
+                );
             }
         }
 
@@ -1748,26 +1730,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
             let self_ty = TraitRef::identity(cx.tcx, item.hir_id.owner.to_def_id()).self_ty();
 
             then {
-                if let Some((ref conv, self_kinds)) = &CONVENTIONS
-                    .iter()
-                    .find(|(ref conv, _)| conv.check(&item.ident.name.as_str()))
-                {
-                    if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
-                        span_lint(
-                            cx,
-                            WRONG_PUB_SELF_CONVENTION,
-                            first_arg_span,
-                            &format!("methods called `{}` usually take {}; consider choosing a less ambiguous name",
-                                conv,
-                                &self_kinds
-                                    .iter()
-                                    .map(|k| k.description())
-                                    .collect::<Vec<_>>()
-                                    .join(" or ")
-                            ),
-                        );
-                    }
-                }
+                lint_wrong_self_convention(cx, &item.ident.name.as_str(), false, self_ty, first_arg_ty, first_arg_span);
             }
         }
 
@@ -1792,6 +1755,39 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
     extract_msrv_attr!(LateContext);
 }
 
+fn lint_wrong_self_convention<'tcx>(
+    cx: &LateContext<'tcx>,
+    item_name: &str,
+    is_pub: bool,
+    self_ty: &'tcx TyS<'tcx>,
+    first_arg_ty: &'tcx TyS<'tcx>,
+    first_arg_span: Span,
+) {
+    let lint = if is_pub {
+        WRONG_PUB_SELF_CONVENTION
+    } else {
+        WRONG_SELF_CONVENTION
+    };
+    if let Some((ref conv, self_kinds)) = &CONVENTIONS.iter().find(|(ref conv, _)| conv.check(item_name)) {
+        if !self_kinds.iter().any(|k| k.matches(cx, self_ty, first_arg_ty)) {
+            span_lint(
+                cx,
+                lint,
+                first_arg_span,
+                &format!(
+                    "methods called `{}` usually take {}; consider choosing a less ambiguous name",
+                    conv,
+                    &self_kinds
+                        .iter()
+                        .map(|k| k.description())
+                        .collect::<Vec<_>>()
+                        .join(" or ")
+                ),
+            );
+        }
+    }
+}
+
 /// Checks for the `OR_FUN_CALL` lint.
 #[allow(clippy::too_many_lines)]
 fn lint_or_fun_call<'tcx>(
diff --git a/tests/ui/wrong_self_convention.rs b/tests/ui/wrong_self_convention.rs
index 275866b8248..795ba77274c 100644
--- a/tests/ui/wrong_self_convention.rs
+++ b/tests/ui/wrong_self_convention.rs
@@ -90,7 +90,7 @@ mod issue4037 {
 }
 
 // Lint also in trait definition (see #6307)
-mod issue6307{
+mod issue6307 {
     trait T: Sized {
         fn as_i32(self) {}
         fn as_u32(&self) {}
@@ -102,7 +102,7 @@ mod issue6307{
         fn to_u32(&self) {}
         fn from_i32(self) {}
         // check whether the lint can be allowed at the function level
-        #[allow(clippy::wrong_pub_self_convention)]
+        #[allow(clippy::wrong_self_convention)]
         fn from_cake(self) {}
 
         // test for false positives
@@ -113,4 +113,4 @@ mod issue6307{
         fn from_(self) {}
         fn to_mut(&mut self) {}
     }
-}
\ No newline at end of file
+}
diff --git a/tests/ui/wrong_self_convention.stderr b/tests/ui/wrong_self_convention.stderr
index 64aa957fed6..289da6f059e 100644
--- a/tests/ui/wrong_self_convention.stderr
+++ b/tests/ui/wrong_self_convention.stderr
@@ -77,8 +77,6 @@ error: methods called `as_*` usually take self by reference or self by mutable r
    |
 LL |         fn as_i32(self) {}
    |                   ^^^^
-   |
-   = note: `-D clippy::wrong-pub-self-convention` implied by `-D warnings`
 
 error: methods called `into_*` usually take self by value; consider choosing a less ambiguous name
   --> $DIR/wrong_self_convention.rs:97:21