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