]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs
New upstream version 1.22.1+dfsg1
[rustc.git] / src / tools / clippy / 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 let Some(eq_trait) = cx.tcx.lang_items.eq_trait(),
44 trait_ref.path.def.def_id() == eq_trait
45 ], {
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 }