2016-10-30 01:33:57 +00:00
|
|
|
use rustc::lint::*;
|
|
|
|
use rustc::hir::*;
|
|
|
|
use utils::{is_automatically_derived, span_lint};
|
|
|
|
|
|
|
|
/// **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) }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
declare_lint! {
|
|
|
|
pub PARTIALEQ_NE_IMPL,
|
|
|
|
Warn,
|
|
|
|
"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) {
|
2016-10-30 01:33:57 +00:00
|
|
|
if_let_chain! {[
|
|
|
|
let ItemImpl(_, _, _, Some(ref trait_ref), _, ref impl_items) = item.node,
|
|
|
|
!is_automatically_derived(&*item.attrs),
|
2016-12-01 21:31:56 +00:00
|
|
|
trait_ref.path.def.def_id() == cx.tcx.lang_items.eq_trait().unwrap(),
|
2016-10-30 01:33:57 +00:00
|
|
|
], {
|
|
|
|
for impl_item in impl_items {
|
2017-03-30 08:21:13 +00:00
|
|
|
if impl_item.name == "ne" {
|
2016-10-30 01:33:57 +00:00
|
|
|
span_lint(cx,
|
|
|
|
PARTIALEQ_NE_IMPL,
|
|
|
|
impl_item.span,
|
|
|
|
"re-implementing `PartialEq::ne` is unnecessary")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
}
|