2 use rustc::ty::TypeVariants;
5 use syntax::codemap::Span;
7 use utils::{is_automatically_derived, span_lint_and_then, match_path_old, is_copy};
9 /// **What it does:** Checks for deriving `Hash` but implementing `PartialEq`
12 /// **Why is this bad?** The implementation of these traits must agree (for
13 /// example for use with `HashMap`) so it’s probably a bad idea to use a
14 /// default-generated `Hash` implementation with an explicitly defined
15 /// `PartialEq`. In particular, the following must hold for any type:
18 /// k1 == k2 ⇒ hash(k1) == hash(k2)
21 /// **Known problems:** None.
28 /// impl PartialEq for Foo {
33 pub DERIVE_HASH_XOR_EQ,
35 "deriving `Hash` but implementing `PartialEq` explicitly"
38 /// **What it does:** Checks for explicit `Clone` implementations for `Copy`
41 /// **Why is this bad?** To avoid surprising behaviour, these traits should
42 /// agree and the behaviour of `Copy` cannot be overridden. In almost all
43 /// situations a `Copy` type should have a `Clone` implementation that does
44 /// nothing more than copy the object, which is what `#[derive(Copy, Clone)]`
47 /// **Known problems:** None.
54 /// impl Clone for Foo {
59 pub EXPL_IMPL_CLONE_ON_COPY,
61 "implementing `Clone` explicitly on `Copy` types"
66 impl LintPass for Derive {
67 fn get_lints(&self) -> LintArray {
68 lint_array!(EXPL_IMPL_CLONE_ON_COPY, DERIVE_HASH_XOR_EQ)
72 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive {
73 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
74 if let ItemImpl(_, _, _, _, Some(ref trait_ref), _, _) = item.node {
75 let ty = cx.tcx.type_of(cx.tcx.hir.local_def_id(item.id));
76 let is_automatically_derived = is_automatically_derived(&*item.attrs);
78 check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived);
80 if !is_automatically_derived {
81 check_copy_clone(cx, item, trait_ref, ty);
87 /// Implementation of the `DERIVE_HASH_XOR_EQ` lint.
88 fn check_hash_peq<'a, 'tcx>(
89 cx: &LateContext<'a, 'tcx>,
93 hash_is_automatically_derived: bool
96 match_path_old(&trait_ref.path, &paths::HASH),
97 let Some(peq_trait_def_id) = cx.tcx.lang_items.eq_trait()
99 let peq_trait_def = cx.tcx.trait_def(peq_trait_def_id);
101 // Look for the PartialEq implementations for `ty`
102 peq_trait_def.for_each_relevant_impl(cx.tcx, ty, |impl_id| {
103 let peq_is_automatically_derived = is_automatically_derived(&cx.tcx.get_attrs(impl_id));
105 if peq_is_automatically_derived == hash_is_automatically_derived {
109 let trait_ref = cx.tcx.impl_trait_ref(impl_id).expect("must be a trait implementation");
111 // Only care about `impl PartialEq<Foo> for Foo`
112 // For `impl PartialEq<B> for A, input_types is [A, B]
113 if trait_ref.substs.type_at(1) == ty {
114 let mess = if peq_is_automatically_derived {
115 "you are implementing `Hash` explicitly but have derived `PartialEq`"
117 "you are deriving `Hash` but have implemented `PartialEq` explicitly"
121 cx, DERIVE_HASH_XOR_EQ, span,
124 if let Some(node_id) = cx.tcx.hir.as_local_node_id(impl_id) {
126 cx.tcx.hir.span(node_id),
127 "`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::Ty<'tcx>) {
138 if match_path_old(&trait_ref.path, &paths::CLONE_TRAIT) {
139 if !is_copy(cx, ty) {
144 TypeVariants::TyAdt(def, _) if def.is_union() => return,
146 // Some types are not Clone by default but could be cloned “by hand” if necessary
147 TypeVariants::TyAdt(def, substs) => {
148 for variant in &def.variants {
149 for field in &variant.fields {
150 match field.ty(cx.tcx, substs).sty {
151 TypeVariants::TyArray(_, size) if size > 32 => {
154 TypeVariants::TyFnPtr(..) => {
157 TypeVariants::TyTuple(tys, _) if tys.len() > 12 => {
168 span_lint_and_then(cx,
169 EXPL_IMPL_CLONE_ON_COPY,
171 "you are implementing `Clone` explicitly on a `Copy` type",
172 |db| { db.span_note(item.span, "consider deriving `Clone` or removing `Copy`"); });