rust/clippy_lints/src/partialeq_ne_impl.rs

56 lines
1.9 KiB
Rust
Raw Normal View History

2019-05-14 03:06:21 -05:00
use crate::utils::{is_automatically_derived, span_lint_hir};
2018-11-27 14:14:15 -06:00
use if_chain::if_chain;
2020-01-11 05:37:08 -06:00
use rustc::lint::{LateContext, LateLintPass};
2020-01-06 10:39:50 -06:00
use rustc_hir::*;
2020-01-11 05:37:08 -06:00
use rustc_session::{declare_lint_pass, declare_tool_lint};
2018-03-28 08:24:26 -05:00
declare_clippy_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 { true }
/// fn ne(&self, other: &Foo) -> bool { !(self == other) }
/// }
/// ```
pub PARTIALEQ_NE_IMPL,
2018-03-28 08:24:26 -05:00
complexity,
"re-implementing `PartialEq::ne`"
}
2019-04-08 15:43:55 -05:00
declare_lint_pass!(PartialEqNeImpl => [PARTIALEQ_NE_IMPL]);
2019-04-08 15:43:55 -05:00
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PartialEqNeImpl {
2019-12-22 08:42:41 -06:00
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
if_chain! {
2019-12-22 08:56:34 -06:00
if let ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, impl_items) = item.kind;
if !is_automatically_derived(&*item.attrs);
if let Some(eq_trait) = cx.tcx.lang_items().eq_trait();
if trait_ref.path.res.def_id() == eq_trait;
then {
for impl_item in impl_items {
2019-05-17 16:53:54 -05:00
if impl_item.ident.name == sym!(ne) {
span_lint_hir(
cx,
PARTIALEQ_NE_IMPL,
2019-03-01 06:26:06 -06:00
impl_item.id.hir_id,
impl_item.span,
"re-implementing `PartialEq::ne` is unnecessary",
);
}
}
}
};
}
}