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:** This lint warns about deriving `Hash` but implementing `PartialEq`
14 /// **Why is this bad?** The implementation of these traits must agree (for example for use with
15 /// `HashMap`) so it’s probably a bad idea to use a default-generated `Hash` implementation with
16 /// an explicitly defined `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:** This lint warns about explicit `Clone` implementation for `Copy` types.
41 /// **Why is this bad?** To avoid surprising behaviour, these traits should agree and the behaviour
42 /// of `Copy` cannot be overridden. In almost all situations a `Copy` type should have a `Clone`
43 /// implementation that does nothing more than copy the object, which is what
44 /// `#[derive(Copy, Clone)]` gets you.
46 /// **Known problems:** None.
53 /// impl Clone for Foo {
58 pub EXPL_IMPL_CLONE_ON_COPY,
60 "implementing `Clone` explicitly on `Copy` types"
65 impl LintPass for Derive {
66 fn get_lints(&self) -> LintArray {
67 lint_array!(EXPL_IMPL_CLONE_ON_COPY, DERIVE_HASH_XOR_EQ)
71 impl LateLintPass for Derive {
72 fn check_item(&mut self, cx: &LateContext, item: &Item) {
73 if let ItemImpl(_, _, _, Some(ref trait_ref), _, _) = item.node {
74 let ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id(item.id)).ty;
75 let is_automatically_derived = item.attrs.iter().any(is_automatically_derived);
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: 'a>(cx: &LateContext<'a, 'tcx>, span: Span, trait_ref: &TraitRef, ty: ty::Ty<'tcx>,
88 hash_is_automatically_derived: bool) {
90 match_path(&trait_ref.path, &paths::HASH),
91 let Some(peq_trait_def_id) = cx.tcx.lang_items.eq_trait()
93 let peq_trait_def = cx.tcx.lookup_trait_def(peq_trait_def_id);
95 // Look for the PartialEq implementations for `ty`
96 peq_trait_def.for_each_relevant_impl(cx.tcx, ty, |impl_id| {
97 let peq_is_automatically_derived = cx.tcx.get_attrs(impl_id).iter().any(is_automatically_derived);
99 if peq_is_automatically_derived == hash_is_automatically_derived {
103 let trait_ref = cx.tcx.impl_trait_ref(impl_id).expect("must be a trait implementation");
105 // Only care about `impl PartialEq<Foo> for Foo`
106 if trait_ref.input_types()[0] == ty {
107 let mess = if peq_is_automatically_derived {
108 "you are implementing `Hash` explicitly but have derived `PartialEq`"
110 "you are deriving `Hash` but have implemented `PartialEq` explicitly"
114 cx, DERIVE_HASH_XOR_EQ, span,
117 if let Some(node_id) = cx.tcx.map.as_local_node_id(impl_id) {
119 cx.tcx.map.span(node_id),
120 "`PartialEq` implemented here"
129 /// Implementation of the `EXPL_IMPL_CLONE_ON_COPY` lint.
130 fn check_copy_clone<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, item: &Item, trait_ref: &TraitRef, ty: ty::Ty<'tcx>) {
131 if match_path(&trait_ref.path, &paths::CLONE_TRAIT) {
132 let parameter_environment = ty::ParameterEnvironment::for_item(cx.tcx, item.id);
133 let subst_ty = ty.subst(cx.tcx, parameter_environment.free_substs);
135 if subst_ty.moves_by_default(cx.tcx.global_tcx(), ¶meter_environment, item.span) {
136 return; // ty is not Copy
139 // Some types are not Clone by default but could be cloned `by hand` if necessary
141 TypeVariants::TyEnum(def, substs) |
142 TypeVariants::TyStruct(def, substs) => {
143 for variant in &def.variants {
144 for field in &variant.fields {
145 match field.ty(cx.tcx, substs).sty {
146 TypeVariants::TyArray(_, size) if size > 32 => {
149 TypeVariants::TyFnPtr(..) => {
152 TypeVariants::TyTuple(tys) if tys.len() > 12 => {
163 span_lint_and_then(cx,
164 EXPL_IMPL_CLONE_ON_COPY,
166 "you are implementing `Clone` explicitly on a `Copy` type",
168 db.span_note(item.span, "consider deriving `Clone` or removing `Copy`");
173 /// Checks for the `#[automatically_derived]` attribute all `#[derive]`d implementations have.
174 fn is_automatically_derived(attr: &Attribute) -> bool {
175 if let MetaItemKind::Word(ref word) = attr.node.value.node {
176 word == &"automatically_derived"