1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 use crate::rustc::hir::*;
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use crate::utils::{is_automatically_derived, span_lint_node};
14 use if_chain::if_chain;
16 /// **What it does:** Checks for manual re-implementations of `PartialEq::ne`.
18 /// **Why is this bad?** `PartialEq::ne` is required to always return the
19 /// negated result of `PartialEq::eq`, which is exactly what the default
20 /// implementation does. Therefore, there should never be any need to
23 /// **Known problems:** None.
29 /// impl PartialEq for Foo {
30 /// fn eq(&self, other: &Foo) -> bool { ... }
31 /// fn ne(&self, other: &Foo) -> bool { !(self == other) }
34 declare_clippy_lint! {
35 pub PARTIALEQ_NE_IMPL,
37 "re-implementing `PartialEq::ne`"
40 #[derive(Clone, Copy)]
43 impl LintPass for Pass {
44 fn get_lints(&self) -> LintArray {
45 lint_array!(PARTIALEQ_NE_IMPL)
49 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
50 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
52 if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, ref impl_items) = item.node;
53 if !is_automatically_derived(&*item.attrs);
54 if let Some(eq_trait) = cx.tcx.lang_items().eq_trait();
55 if trait_ref.path.def.def_id() == eq_trait;
57 for impl_item in impl_items {
58 if impl_item.ident.name == "ne" {
64 "re-implementing `PartialEq::ne` is unnecessary",