2 use rustc::ty::subst::Subst;
3 use rustc::ty::TypeVariants;
6 use syntax::codemap::Span;
8 use utils::{is_automatically_derived, span_lint_and_then, match_path_old};
10 /// **What it does:** Checks for deriving `Hash` but implementing `PartialEq`
13 /// **Why is this bad?** The implementation of these traits must agree (for
14 /// example for use with `HashMap`) so it’s probably a bad idea to use a
15 /// default-generated `Hash` implementation with an explicitly defined
16 /// `PartialEq`. In particular, the following must hold for any type:
19 /// k1 == k2 ⇒ hash(k1) == hash(k2)
22 /// **Known problems:** None.
29 /// impl PartialEq for Foo {
34 pub DERIVE_HASH_XOR_EQ,
36 "deriving `Hash` but implementing `PartialEq` explicitly"
39 /// **What it does:** Checks for explicit `Clone` implementations for `Copy`
42 /// **Why is this bad?** To avoid surprising behaviour, these traits should
43 /// agree and the behaviour of `Copy` cannot be overridden. In almost all
44 /// situations a `Copy` type should have a `Clone` implementation that does
45 /// nothing more than copy the object, which is what `#[derive(Copy, Clone)]`
48 /// **Known problems:** None.
55 /// impl Clone for Foo {
60 pub EXPL_IMPL_CLONE_ON_COPY,
62 "implementing `Clone` explicitly on `Copy` types"
67 impl LintPass for Derive {
68 fn get_lints(&self) -> LintArray {
69 lint_array!(EXPL_IMPL_CLONE_ON_COPY, DERIVE_HASH_XOR_EQ)
73 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Derive {
74 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
75 if let ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node {
76 let ty = cx.tcx.type_of(cx.tcx.hir.local_def_id(item.id));
77 let is_automatically_derived = is_automatically_derived(&*item.attrs);
79 check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived);
81 if !is_automatically_derived {
82 check_copy_clone(cx, item, trait_ref, ty);
88 /// Implementation of the `DERIVE_HASH_XOR_EQ` lint.
89 fn check_hash_peq<'a, 'tcx>(
90 cx: &LateContext<'a, 'tcx>,
94 hash_is_automatically_derived: bool
97 match_path_old(&trait_ref.path, &paths::HASH),
98 let Some(peq_trait_def_id) = cx.tcx.lang_items.eq_trait()
100 let peq_trait_def = cx.tcx.trait_def(peq_trait_def_id);
102 // Look for the PartialEq implementations for `ty`
103 peq_trait_def.for_each_relevant_impl(cx.tcx, ty, |impl_id| {
104 let peq_is_automatically_derived = is_automatically_derived(&cx.tcx.get_attrs(impl_id));
106 if peq_is_automatically_derived == hash_is_automatically_derived {
110 let trait_ref = cx.tcx.impl_trait_ref(impl_id).expect("must be a trait implementation");
112 // Only care about `impl PartialEq<Foo> for Foo`
113 // For `impl PartialEq<B> for A, input_types is [A, B]
114 if trait_ref.substs.type_at(1) == ty {
115 let mess = if peq_is_automatically_derived {
116 "you are implementing `Hash` explicitly but have derived `PartialEq`"
118 "you are deriving `Hash` but have implemented `PartialEq` explicitly"
122 cx, DERIVE_HASH_XOR_EQ, span,
125 if let Some(node_id) = cx.tcx.hir.as_local_node_id(impl_id) {
127 cx.tcx.hir.span(node_id),
128 "`PartialEq` implemented here"
137 /// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint.
138 fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item, trait_ref: &TraitRef, ty: ty::Ty<'tcx>) {
139 if match_path_old(&trait_ref.path, &paths::CLONE_TRAIT) {
140 let parameter_environment = ty::ParameterEnvironment::for_item(cx.tcx, item.id);
141 let subst_ty = ty.subst(cx.tcx, parameter_environment.free_substs);
143 if subst_ty.moves_by_default(cx.tcx.global_tcx(), ¶meter_environment, item.span) {
144 return; // ty is not Copy
148 TypeVariants::TyAdt(def, _) if def.is_union() => return,
150 // Some types are not Clone by default but could be cloned “by hand” if necessary
151 TypeVariants::TyAdt(def, substs) => {
152 for variant in &def.variants {
153 for field in &variant.fields {
154 match field.ty(cx.tcx, substs).sty {
155 TypeVariants::TyArray(_, size) if size > 32 => {
158 TypeVariants::TyFnPtr(..) => {
161 TypeVariants::TyTuple(tys, _) if tys.len() > 12 => {
172 span_lint_and_then(cx,
173 EXPL_IMPL_CLONE_ON_COPY,
175 "you are implementing `Clone` explicitly on a `Copy` type",
176 |db| { db.span_note(item.span, "consider deriving `Clone` or removing `Copy`"); });