]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/partialeq_ne_impl.rs
Rollup merge of #105123 - BlackHoleFox:fixing-the-macos-deployment, r=oli-obk
[rust.git] / src / tools / clippy / clippy_lints / src / partialeq_ne_impl.rs
1 use clippy_utils::diagnostics::span_lint_hir;
2 use if_chain::if_chain;
3 use rustc_hir::{Impl, Item, ItemKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::sym;
7
8 declare_clippy_lint! {
9     /// ### What it does
10     /// Checks for manual re-implementations of `PartialEq::ne`.
11     ///
12     /// ### Why is this bad?
13     /// `PartialEq::ne` is required to always return the
14     /// negated result of `PartialEq::eq`, which is exactly what the default
15     /// implementation does. Therefore, there should never be any need to
16     /// re-implement it.
17     ///
18     /// ### Example
19     /// ```rust
20     /// struct Foo;
21     ///
22     /// impl PartialEq for Foo {
23     ///    fn eq(&self, other: &Foo) -> bool { true }
24     ///    fn ne(&self, other: &Foo) -> bool { !(self == other) }
25     /// }
26     /// ```
27     #[clippy::version = "pre 1.29.0"]
28     pub PARTIALEQ_NE_IMPL,
29     complexity,
30     "re-implementing `PartialEq::ne`"
31 }
32
33 declare_lint_pass!(PartialEqNeImpl => [PARTIALEQ_NE_IMPL]);
34
35 impl<'tcx> LateLintPass<'tcx> for PartialEqNeImpl {
36     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
37         if_chain! {
38             if let ItemKind::Impl(Impl { of_trait: Some(ref trait_ref), items: impl_items, .. }) = item.kind;
39             if !cx.tcx.has_attr(item.owner_id.to_def_id(), sym::automatically_derived);
40             if let Some(eq_trait) = cx.tcx.lang_items().eq_trait();
41             if trait_ref.path.res.def_id() == eq_trait;
42             then {
43                 for impl_item in *impl_items {
44                     if impl_item.ident.name == sym::ne {
45                         span_lint_hir(
46                             cx,
47                             PARTIALEQ_NE_IMPL,
48                             impl_item.id.hir_id(),
49                             impl_item.span,
50                             "re-implementing `PartialEq::ne` is unnecessary",
51                         );
52                     }
53                 }
54             }
55         };
56     }
57 }