2 use rustc::ty::subst::Subst;
3 use rustc::ty::TypeVariants;
6 use syntax::ast::{Attribute, MetaItemKind};
7 use syntax::codemap::Span;
9 use utils::{match_path, span_lint_and_then};
11 /// **What it does:** Checks for deriving `Hash` but implementing `PartialEq`
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 {
35 pub DERIVE_HASH_XOR_EQ,
37 "deriving `Hash` but implementing `PartialEq` explicitly"
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:** None.
56 /// impl Clone for Foo {
61 pub EXPL_IMPL_CLONE_ON_COPY,
63 "implementing `Clone` explicitly on `Copy` types"
68 impl LintPass for Derive {
69 fn get_lints(&self) -> LintArray {
70 lint_array!(EXPL_IMPL_CLONE_ON_COPY, DERIVE_HASH_XOR_EQ)
74 impl LateLintPass for Derive {
75 fn check_item(&mut self, cx: &LateContext, item: &Item) {
76 if let ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node {
77 let ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id(item.id)).ty;
78 let is_automatically_derived = item.attrs.iter().any(is_automatically_derived);
80 check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived);
82 if !is_automatically_derived {
83 check_copy_clone(cx, item, trait_ref, ty);
89 /// Implementation of the `DERIVE_HASH_XOR_EQ` lint.
90 fn check_hash_peq<'a, 'tcx: 'a>(cx: &LateContext<'a, 'tcx>, span: Span, trait_ref: &TraitRef, ty: ty::Ty<'tcx>,
91 hash_is_automatically_derived: bool) {
93 match_path(&trait_ref.path, &paths::HASH),
94 let Some(peq_trait_def_id) = cx.tcx.lang_items.eq_trait()
96 let peq_trait_def = cx.tcx.lookup_trait_def(peq_trait_def_id);
98 // Look for the PartialEq implementations for `ty`
99 peq_trait_def.for_each_relevant_impl(cx.tcx, ty, |impl_id| {
100 let peq_is_automatically_derived = cx.tcx.get_attrs(impl_id).iter().any(is_automatically_derived);
102 if peq_is_automatically_derived == hash_is_automatically_derived {
106 let trait_ref = cx.tcx.impl_trait_ref(impl_id).expect("must be a trait implementation");
108 // Only care about `impl PartialEq<Foo> for Foo`
109 // For `impl PartialEq<B> for A, input_types is [A, B]
110 if trait_ref.substs.type_at(1) == ty {
111 let mess = if peq_is_automatically_derived {
112 "you are implementing `Hash` explicitly but have derived `PartialEq`"
114 "you are deriving `Hash` but have implemented `PartialEq` explicitly"
118 cx, DERIVE_HASH_XOR_EQ, span,
121 if let Some(node_id) = cx.tcx.map.as_local_node_id(impl_id) {
123 cx.tcx.map.span(node_id),
124 "`PartialEq` implemented here"
133 /// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint.
134 fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item, trait_ref: &TraitRef, ty: ty::Ty<'tcx>) {
135 if match_path(&trait_ref.path, &paths::CLONE_TRAIT) {
136 let parameter_environment = ty::ParameterEnvironment::for_item(cx.tcx, item.id);
137 let subst_ty = ty.subst(cx.tcx, parameter_environment.free_substs);
139 if subst_ty.moves_by_default(cx.tcx.global_tcx(), ¶meter_environment, item.span) {
140 return; // ty is not Copy
144 TypeVariants::TyUnion(..) => return,
146 // Some types are not Clone by default but could be cloned “by hand” if necessary
147 TypeVariants::TyEnum(def, substs) |
148 TypeVariants::TyStruct(def, substs) => {
149 for variant in &def.variants {
150 for field in &variant.fields {
151 match field.ty(cx.tcx, substs).sty {
152 TypeVariants::TyArray(_, size) if size > 32 => {
155 TypeVariants::TyFnPtr(..) => {
158 TypeVariants::TyTuple(tys) if tys.len() > 12 => {
169 span_lint_and_then(cx,
170 EXPL_IMPL_CLONE_ON_COPY,
172 "you are implementing `Clone` explicitly on a `Copy` type",
174 db.span_note(item.span, "consider deriving `Clone` or removing `Copy`");
179 /// Checks for the `#[automatically_derived]` attribute all `#[derive]`d implementations have.
180 fn is_automatically_derived(attr: &Attribute) -> bool {
181 if let MetaItemKind::Word(ref word) = attr.node.value.node {
182 word == &"automatically_derived"