2021-03-25 19:29:11 +01:00
|
|
|
use clippy_utils::diagnostics::span_lint_hir;
|
2021-01-15 10:56:44 +01:00
|
|
|
use rustc_hir::{Impl, Item, ItemKind};
|
2020-01-12 15:08:41 +09:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-11-25 17:45:27 +00:00
|
|
|
use rustc_session::declare_lint_pass;
|
2020-11-05 14:29:48 +01:00
|
|
|
use rustc_span::sym;
|
2016-10-29 21:33:57 -04:00
|
|
|
|
2018-03-28 15:24:26 +02:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for manual re-implementations of `PartialEq::ne`.
|
2019-03-05 11:50:33 -05:00
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// `PartialEq::ne` is required to always return the
|
2019-03-05 11:50:33 -05:00
|
|
|
/// negated result of `PartialEq::eq`, which is exactly what the default
|
|
|
|
/// implementation does. Therefore, there should never be any need to
|
|
|
|
/// re-implement it.
|
|
|
|
///
|
2021-07-29 12:16:06 +02:00
|
|
|
/// ### Example
|
2023-10-23 13:49:18 +00:00
|
|
|
/// ```no_run
|
2019-03-05 11:50:33 -05:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
|
|
|
/// impl PartialEq for Foo {
|
2019-08-02 08:13:54 +02:00
|
|
|
/// fn eq(&self, other: &Foo) -> bool { true }
|
2019-03-05 11:50:33 -05:00
|
|
|
/// fn ne(&self, other: &Foo) -> bool { !(self == other) }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 12:33:31 +01:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
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
|
|
|
|
2020-06-25 23:41:36 +03:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for PartialEqNeImpl {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
2023-11-02 19:23:36 +00:00
|
|
|
if let ItemKind::Impl(Impl {
|
|
|
|
of_trait: Some(ref trait_ref),
|
|
|
|
items: impl_items,
|
|
|
|
..
|
|
|
|
}) = item.kind
|
2023-11-10 17:29:28 +00:00
|
|
|
&& !cx.tcx.has_attr(item.owner_id, sym::automatically_derived)
|
|
|
|
&& let Some(eq_trait) = cx.tcx.lang_items().eq_trait()
|
|
|
|
&& trait_ref.path.res.def_id() == eq_trait
|
|
|
|
{
|
|
|
|
for impl_item in *impl_items {
|
|
|
|
if impl_item.ident.name == sym::ne {
|
|
|
|
span_lint_hir(
|
|
|
|
cx,
|
|
|
|
PARTIALEQ_NE_IMPL,
|
|
|
|
impl_item.id.hir_id(),
|
|
|
|
impl_item.span,
|
|
|
|
"re-implementing `PartialEq::ne` is unnecessary",
|
|
|
|
);
|
2016-10-29 21:33:57 -04:00
|
|
|
}
|
|
|
|
}
|
2017-10-23 15:18:02 -04:00
|
|
|
};
|
2016-10-29 21:33:57 -04:00
|
|
|
}
|
|
|
|
}
|