]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/partialeq_ne_impl.rs
Auto merge of #4809 - iankronquist:patch-1, r=flip1995
[rust.git] / clippy_lints / src / partialeq_ne_impl.rs
index f57fd22c0707d71f0ae0982a643567df726d0c88..dfd25f1c9db6b4b8764d909b1edccd361ee6fb6d 100644 (file)
@@ -1,61 +1,49 @@
-use crate::utils::{is_automatically_derived, span_lint_node};
+use crate::utils::{is_automatically_derived, span_lint_hir};
 use if_chain::if_chain;
-use rustc::hir::*;
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use rustc_hir::*;
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
 
-/// **What it does:** Checks for manual re-implementations of `PartialEq::ne`.
-///
-/// **Why is this bad?** `PartialEq::ne` is required to always return the
-/// negated result of `PartialEq::eq`, which is exactly what the default
-/// implementation does. Therefore, there should never be any need to
-/// re-implement it.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// struct Foo;
-///
-/// impl PartialEq for Foo {
-///    fn eq(&self, other: &Foo) -> bool { ... }
-///    fn ne(&self, other: &Foo) -> bool { !(self == other) }
-/// }
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for manual re-implementations of `PartialEq::ne`.
+    ///
+    /// **Why is this bad?** `PartialEq::ne` is required to always return the
+    /// negated result of `PartialEq::eq`, which is exactly what the default
+    /// implementation does. Therefore, there should never be any need to
+    /// re-implement it.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// struct Foo;
+    ///
+    /// impl PartialEq for Foo {
+    ///    fn eq(&self, other: &Foo) -> bool { true }
+    ///    fn ne(&self, other: &Foo) -> bool { !(self == other) }
+    /// }
+    /// ```
     pub PARTIALEQ_NE_IMPL,
     complexity,
     "re-implementing `PartialEq::ne`"
 }
 
-#[derive(Clone, Copy)]
-pub struct Pass;
+declare_lint_pass!(PartialEqNeImpl => [PARTIALEQ_NE_IMPL]);
 
-impl LintPass for Pass {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(PARTIALEQ_NE_IMPL)
-    }
-
-    fn name(&self) -> &'static str {
-        "PartialEqNeImpl"
-    }
-}
-
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
-    fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PartialEqNeImpl {
+    fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
         if_chain! {
-            if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, ref impl_items) = item.node;
+            if let ItemKind::Impl{ of_trait: Some(ref trait_ref), items: impl_items, .. } = item.kind;
             if !is_automatically_derived(&*item.attrs);
             if let Some(eq_trait) = cx.tcx.lang_items().eq_trait();
-            if trait_ref.path.def.def_id() == eq_trait;
+            if trait_ref.path.res.def_id() == eq_trait;
             then {
                 for impl_item in impl_items {
-                    if impl_item.ident.name == "ne" {
-                        let hir_id = cx.tcx.hir().node_to_hir_id(impl_item.id.node_id);
-                        span_lint_node(
+                    if impl_item.ident.name == sym!(ne) {
+                        span_lint_hir(
                             cx,
                             PARTIALEQ_NE_IMPL,
-                            hir_id,
+                            impl_item.id.hir_id,
                             impl_item.span,
                             "re-implementing `PartialEq::ne` is unnecessary",
                         );