about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2020-01-18 21:05:42 +0900
committerYuki Okushi <huyuumi.dev@gmail.com>2020-01-18 21:05:42 +0900
commitff0a22d99ff9dfbaa849db2cd9d23a77fa2b97a1 (patch)
tree9d9391aa74a7ec1f64c29be80fdf5f2ae600a9b4
parente36a33fac779458b65ae6b45c692a1700b08e506 (diff)
downloadrust-ff0a22d99ff9dfbaa849db2cd9d23a77fa2b97a1.tar.gz
rust-ff0a22d99ff9dfbaa849db2cd9d23a77fa2b97a1.zip
Allow `unused_self` lint at the function level
-rw-r--r--clippy_lints/src/unused_self.rs15
-rw-r--r--tests/ui/unused_self.rs18
2 files changed, 27 insertions, 6 deletions
diff --git a/clippy_lints/src/unused_self.rs b/clippy_lints/src/unused_self.rs
index 71f7f8d0af0..247da580f45 100644
--- a/clippy_lints/src/unused_self.rs
+++ b/clippy_lints/src/unused_self.rs
@@ -2,7 +2,7 @@ use if_chain::if_chain;
 use rustc::hir::map::Map;
 use rustc_hir::def::Res;
 use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
-use rustc_hir::{AssocItemKind, HirId, ImplItemKind, ImplItemRef, Item, ItemKind, Path};
+use rustc_hir::{AssocItemKind, HirId, ImplItem, ImplItemKind, ImplItemRef, ItemKind, Path};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::{declare_lint_pass, declare_tool_lint};
 
@@ -40,10 +40,12 @@ declare_clippy_lint! {
 declare_lint_pass!(UnusedSelf => [UNUSED_SELF]);
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
-    fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &Item<'_>) {
-        if item.span.from_expansion() {
+    fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &ImplItem<'_>) {
+        if impl_item.span.from_expansion() {
             return;
         }
+        let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id);
+        let item = cx.tcx.hir().expect_item(parent);
         if let ItemKind::Impl {
             of_trait: None,
             items: impl_item_refs,
@@ -56,10 +58,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
                         kind: AssocItemKind::Method { has_self: true },
                         ..
                     } = impl_item_ref;
-                    let impl_item = cx.tcx.hir().impl_item(impl_item_ref.id);
                     if let ImplItemKind::Method(_, body_id) = &impl_item.kind;
+                    let body = cx.tcx.hir().body(*body_id);
+                    if !body.params.is_empty();
                     then {
-                        let body = cx.tcx.hir().body(*body_id);
                         let self_param = &body.params[0];
                         let self_hir_id = self_param.pat.hir_id;
                         let mut visitor = UnusedSelfVisitor {
@@ -75,7 +77,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedSelf {
                                 self_param.span,
                                 "unused `self` argument",
                                 "consider refactoring to a associated function",
-                            )
+                            );
+                            return;
                         }
                     }
                 }
diff --git a/tests/ui/unused_self.rs b/tests/ui/unused_self.rs
index 9119c43c082..711c1cd9d6c 100644
--- a/tests/ui/unused_self.rs
+++ b/tests/ui/unused_self.rs
@@ -26,6 +26,24 @@ mod unused_self {
     }
 }
 
+mod unused_self_allow {
+    struct A {}
+
+    impl A {
+        // shouldn't trigger
+        #[allow(clippy::unused_self)]
+        fn unused_self_move(self) {}
+    }
+
+    struct B {}
+
+    // shouldn't trigger
+    #[allow(clippy::unused_self)]
+    impl B {
+        fn unused_self_move(self) {}
+    }
+}
+
 mod used_self {
     use std::pin::Pin;