1 use crate::utils::paths;
2 use crate::utils::{is_automatically_derived, is_copy, match_path, span_lint_and_then};
3 use if_chain::if_chain;
4 use rustc_hir::{Item, ItemKind, TraitRef};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_middle::ty::{self, Ty};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::source_map::Span;
10 declare_clippy_lint! {
11 /// **What it does:** Checks for deriving `Hash` but implementing `PartialEq`
12 /// explicitly or vice versa.
14 /// **Why is this bad?** The implementation of these traits must agree (for
15 /// example for use with `HashMap`) so it’s probably a bad idea to use a
16 /// default-generated `Hash` implementation with an explicitly defined
17 /// `PartialEq`. In particular, the following must hold for any type:
20 /// k1 == k2 ⇒ hash(k1) == hash(k2)
23 /// **Known problems:** None.
30 /// impl PartialEq for Foo {
34 pub DERIVE_HASH_XOR_EQ,
36 "deriving `Hash` but implementing `PartialEq` explicitly"
39 declare_clippy_lint! {
40 /// **What it does:** Checks for explicit `Clone` implementations for `Copy`
43 /// **Why is this bad?** To avoid surprising behaviour, these traits should
44 /// agree and the behaviour of `Copy` cannot be overridden. In almost all
45 /// situations a `Copy` type should have a `Clone` implementation that does
46 /// nothing more than copy the object, which is what `#[derive(Copy, Clone)]`
49 /// **Known problems:** Bounds of generic types are sometimes wrong: https://github.com/rust-lang/rust/issues/26925
56 /// impl Clone for Foo {
60 pub EXPL_IMPL_CLONE_ON_COPY,
62 "implementing `Clone` explicitly on `Copy` types"
65 declare_lint_pass!(Derive => [EXPL_IMPL_CLONE_ON_COPY, DERIVE_HASH_XOR_EQ]);
67 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive {
68 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
69 if let ItemKind::Impl {
70 of_trait: Some(ref trait_ref),
74 let ty = cx.tcx.type_of(cx.tcx.hir().local_def_id(item.hir_id));
75 let is_automatically_derived = is_automatically_derived(&*item.attrs);
77 check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived);
79 if !is_automatically_derived {
80 check_copy_clone(cx, item, trait_ref, ty);
86 /// Implementation of the `DERIVE_HASH_XOR_EQ` lint.
87 fn check_hash_peq<'a, 'tcx>(
88 cx: &LateContext<'a, 'tcx>,
90 trait_ref: &TraitRef<'_>,
92 hash_is_automatically_derived: bool,
95 if match_path(&trait_ref.path, &paths::HASH);
96 if let Some(peq_trait_def_id) = cx.tcx.lang_items().eq_trait();
97 if let Some(def_id) = &trait_ref.trait_def_id();
98 if !def_id.is_local();
100 // Look for the PartialEq implementations for `ty`
101 cx.tcx.for_each_relevant_impl(peq_trait_def_id, ty, |impl_id| {
102 let peq_is_automatically_derived = is_automatically_derived(&cx.tcx.get_attrs(impl_id));
104 if peq_is_automatically_derived == hash_is_automatically_derived {
108 let trait_ref = cx.tcx.impl_trait_ref(impl_id).expect("must be a trait implementation");
110 // Only care about `impl PartialEq<Foo> for Foo`
111 // For `impl PartialEq<B> for A, input_types is [A, B]
112 if trait_ref.substs.type_at(1) == ty {
113 let mess = if peq_is_automatically_derived {
114 "you are implementing `Hash` explicitly but have derived `PartialEq`"
116 "you are deriving `Hash` but have implemented `PartialEq` explicitly"
120 cx, DERIVE_HASH_XOR_EQ, span,
123 if let Some(node_id) = cx.tcx.hir().as_local_hir_id(impl_id) {
125 cx.tcx.hir().span(node_id),
126 "`PartialEq` implemented here"
136 /// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint.
137 fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item<'_>, trait_ref: &TraitRef<'_>, ty: Ty<'tcx>) {
138 if match_path(&trait_ref.path, &paths::CLONE_TRAIT) {
139 if !is_copy(cx, ty) {
144 ty::Adt(def, _) if def.is_union() => return,
146 // Some types are not Clone by default but could be cloned “by hand” if necessary
147 ty::Adt(def, substs) => {
148 for variant in &def.variants {
149 for field in &variant.fields {
150 if let ty::FnDef(..) = field.ty(cx.tcx, substs).kind {
154 for subst in substs {
155 if let ty::subst::GenericArgKind::Type(subst) = subst.unpack() {
156 if let ty::Param(_) = subst.kind {
168 EXPL_IMPL_CLONE_ON_COPY,
170 "you are implementing `Clone` explicitly on a `Copy` type",
172 db.span_note(item.span, "consider deriving `Clone` or removing `Copy`");