]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/partialeq_ne_impl.rs
Fix ICE for issues 2767, 2499, 1782
[rust.git] / clippy_lints / src / partialeq_ne_impl.rs
1 use rustc::lint::*;
2 use rustc::hir::*;
3 use utils::{is_automatically_derived, span_lint};
4
5 /// **What it does:** Checks for manual re-implementations of `PartialEq::ne`.
6 ///
7 /// **Why is this bad?** `PartialEq::ne` is required to always return the
8 /// negated result of `PartialEq::eq`, which is exactly what the default
9 /// implementation does. Therefore, there should never be any need to
10 /// re-implement it.
11 ///
12 /// **Known problems:** None.
13 ///
14 /// **Example:**
15 /// ```rust
16 /// struct Foo;
17 ///
18 /// impl PartialEq for Foo {
19 ///    fn eq(&self, other: &Foo) -> bool { ... }
20 ///    fn ne(&self, other: &Foo) -> bool { !(self == other) }
21 /// }
22 /// ```
23 declare_clippy_lint! {
24     pub PARTIALEQ_NE_IMPL,
25     complexity,
26     "re-implementing `PartialEq::ne`"
27 }
28
29 #[derive(Clone, Copy)]
30 pub struct Pass;
31
32 impl LintPass for Pass {
33     fn get_lints(&self) -> LintArray {
34         lint_array!(PARTIALEQ_NE_IMPL)
35     }
36 }
37
38 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
39     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
40         if_chain! {
41             if let ItemImpl(_, _, _, _, Some(ref trait_ref), _, ref impl_items) = item.node;
42             if !is_automatically_derived(&*item.attrs);
43             if let Some(eq_trait) = cx.tcx.lang_items().eq_trait();
44             if trait_ref.path.def.def_id() == eq_trait;
45             then {
46                 for impl_item in impl_items {
47                     if impl_item.name == "ne" {
48                         span_lint(cx,
49                                   PARTIALEQ_NE_IMPL,
50                                   impl_item.span,
51                                   "re-implementing `PartialEq::ne` is unnecessary")
52                     }
53                 }
54             }
55         };
56     }
57 }