2018-08-28 11:13:42 +00:00
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2018-07-19 07:53:23 +00:00
|
|
|
use rustc::{declare_lint, lint_array};
|
2018-07-19 08:00:54 +00:00
|
|
|
use if_chain::if_chain;
|
2016-10-30 01:33:57 +00:00
|
|
|
use rustc::hir::*;
|
2018-05-30 08:15:50 +00:00
|
|
|
use crate::utils::{is_automatically_derived, span_lint};
|
2016-10-30 01:33:57 +00:00
|
|
|
|
|
|
|
/// **What it does:** Checks for manual re-implementations of `PartialEq::ne`.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** `PartialEq::ne` is required to always return the
|
|
|
|
/// negated result of `PartialEq::eq`, which is exactly what the default
|
|
|
|
/// implementation does. Therefore, there should never be any need to
|
|
|
|
/// re-implement it.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// struct Foo;
|
|
|
|
///
|
|
|
|
/// impl PartialEq for Foo {
|
|
|
|
/// fn eq(&self, other: &Foo) -> bool { ... }
|
|
|
|
/// fn ne(&self, other: &Foo) -> bool { !(self == other) }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2016-10-30 01:33:57 +00:00
|
|
|
pub PARTIALEQ_NE_IMPL,
|
2018-03-28 13:24:26 +00:00
|
|
|
complexity,
|
2016-10-30 01:33:57 +00:00
|
|
|
"re-implementing `PartialEq::ne`"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct Pass;
|
|
|
|
|
|
|
|
impl LintPass for Pass {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(PARTIALEQ_NE_IMPL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
|
2017-10-23 19:18:02 +00:00
|
|
|
if_chain! {
|
2018-07-16 13:07:39 +00:00
|
|
|
if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, ref impl_items) = item.node;
|
2017-10-23 19:18:02 +00:00
|
|
|
if !is_automatically_derived(&*item.attrs);
|
|
|
|
if let Some(eq_trait) = cx.tcx.lang_items().eq_trait();
|
|
|
|
if trait_ref.path.def.def_id() == eq_trait;
|
|
|
|
then {
|
|
|
|
for impl_item in impl_items {
|
2018-06-28 13:46:58 +00:00
|
|
|
if impl_item.ident.name == "ne" {
|
2017-10-23 19:18:02 +00:00
|
|
|
span_lint(cx,
|
|
|
|
PARTIALEQ_NE_IMPL,
|
|
|
|
impl_item.span,
|
|
|
|
"re-implementing `PartialEq::ne` is unnecessary")
|
|
|
|
}
|
2016-10-30 01:33:57 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 19:18:02 +00:00
|
|
|
};
|
2016-10-30 01:33:57 +00:00
|
|
|
}
|
|
|
|
}
|