]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/partialeq_ne_impl.rs
Auto merge of #6325 - flip1995:rustup, r=flip1995
[rust.git] / clippy_lints / src / partialeq_ne_impl.rs
1 use crate::utils::{is_automatically_derived, span_lint_hir};
2 use if_chain::if_chain;
3 use rustc_hir::{Item, ItemKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::sym;
7
8 declare_clippy_lint! {
9     /// **What it does:** Checks for manual re-implementations of `PartialEq::ne`.
10     ///
11     /// **Why is this bad?** `PartialEq::ne` is required to always return the
12     /// negated result of `PartialEq::eq`, which is exactly what the default
13     /// implementation does. Therefore, there should never be any need to
14     /// re-implement it.
15     ///
16     /// **Known problems:** None.
17     ///
18     /// **Example:**
19     /// ```rust
20     /// struct Foo;
21     ///
22     /// impl PartialEq for Foo {
23     ///    fn eq(&self, other: &Foo) -> bool { true }
24     ///    fn ne(&self, other: &Foo) -> bool { !(self == other) }
25     /// }
26     /// ```
27     pub PARTIALEQ_NE_IMPL,
28     complexity,
29     "re-implementing `PartialEq::ne`"
30 }
31
32 declare_lint_pass!(PartialEqNeImpl => [PARTIALEQ_NE_IMPL]);
33
34 impl<'tcx> LateLintPass<'tcx> for PartialEqNeImpl {
35     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
36         if_chain! {
37             if let ItemKind::Impl{ of_trait: Some(ref trait_ref), items: impl_items, .. } = item.kind;
38             if !is_automatically_derived(&*item.attrs);
39             if let Some(eq_trait) = cx.tcx.lang_items().eq_trait();
40             if trait_ref.path.res.def_id() == eq_trait;
41             then {
42                 for impl_item in impl_items {
43                     if impl_item.ident.name == sym::ne {
44                         span_lint_hir(
45                             cx,
46                             PARTIALEQ_NE_IMPL,
47                             impl_item.id.hir_id,
48                             impl_item.span,
49                             "re-implementing `PartialEq::ne` is unnecessary",
50                         );
51                     }
52                 }
53             }
54         };
55     }
56 }