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