2019-03-12 08:01:21 +01:00
|
|
|
use crate::utils::{is_automatically_derived, span_lint_hir};
|
2018-11-27 21:14:15 +01:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 16:04:45 +01:00
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2019-04-08 13:43:55 -07:00
|
|
|
use rustc::{declare_lint_pass, declare_tool_lint};
|
2016-10-29 21:33:57 -04:00
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 11:50:33 -05: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) }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2016-10-29 21:33:57 -04:00
|
|
|
pub PARTIALEQ_NE_IMPL,
|
2018-03-28 15:24:26 +02:00
|
|
|
complexity,
|
2016-10-29 21:33:57 -04:00
|
|
|
"re-implementing `PartialEq::ne`"
|
|
|
|
}
|
|
|
|
|
2019-04-08 13:43:55 -07:00
|
|
|
declare_lint_pass!(PartialEqNeImpl => [PARTIALEQ_NE_IMPL]);
|
2016-10-29 21:33:57 -04:00
|
|
|
|
2019-04-08 13:43:55 -07:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PartialEqNeImpl {
|
2016-12-07 13:13:40 +01:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
|
2017-10-23 15:18:02 -04:00
|
|
|
if_chain! {
|
2018-07-16 15:07:39 +02:00
|
|
|
if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, ref impl_items) = item.node;
|
2017-10-23 15:18:02 -04: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 15:46:58 +02:00
|
|
|
if impl_item.ident.name == "ne" {
|
2019-03-12 08:01:21 +01:00
|
|
|
span_lint_hir(
|
2018-12-11 15:06:41 +09:00
|
|
|
cx,
|
|
|
|
PARTIALEQ_NE_IMPL,
|
2019-03-01 13:26:06 +01:00
|
|
|
impl_item.id.hir_id,
|
2018-12-11 15:06:41 +09:00
|
|
|
impl_item.span,
|
|
|
|
"re-implementing `PartialEq::ne` is unnecessary",
|
|
|
|
);
|
2017-10-23 15:18:02 -04:00
|
|
|
}
|
2016-10-29 21:33:57 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 15:18:02 -04:00
|
|
|
};
|
2016-10-29 21:33:57 -04:00
|
|
|
}
|
|
|
|
}
|