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