]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/partialeq_ne_impl.rs
Merge pull request #1373 from Manishearth/rustup
[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_lint! {
24     pub PARTIALEQ_NE_IMPL,
25     Warn,
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_let_chain! {[
41             let ItemImpl(_, _, _, Some(ref trait_ref), _, ref impl_items) = item.node,
42             !is_automatically_derived(&*item.attrs),
43             trait_ref.path.def.def_id() == cx.tcx.lang_items.eq_trait().unwrap(),
44         ], {
45             for impl_item in impl_items {
46                 if &*impl_item.name.as_str() == "ne" {
47                     span_lint(cx,
48                               PARTIALEQ_NE_IMPL,
49                               impl_item.span,
50                               "re-implementing `PartialEq::ne` is unnecessary")
51                 }
52             }
53         }};
54     }
55 }