|
| 1 | +use rustc::lint::*; |
| 2 | +use rustc_front::hir::*; |
| 3 | +use syntax::ast::{Attribute, MetaItem_}; |
| 4 | +use utils::{match_path, span_lint_and_then}; |
| 5 | +use utils::HASH_PATH; |
| 6 | + |
| 7 | +use rustc::middle::ty::fast_reject::simplify_type; |
| 8 | + |
| 9 | +/// **What it does:** This lint warns about deriving `Hash` but implementing `PartialEq` |
| 10 | +/// explicitely. |
| 11 | +/// |
| 12 | +/// **Why is this bad?** The implementation of these traits must agree (for example for use with |
| 13 | +/// `HashMap`) so it’s probably a bad idea to use a default-generated `Hash` implementation with |
| 14 | +/// an explicitely defined `PartialEq`. In particular, the following must hold for any type: |
| 15 | +/// |
| 16 | +/// ```rust |
| 17 | +/// k1 == k2 -> hash(k1) == hash(k2) |
| 18 | +/// ``` |
| 19 | +/// |
| 20 | +/// **Known problems:** None. |
| 21 | +/// |
| 22 | +/// **Example:** |
| 23 | +/// ```rust |
| 24 | +/// #[derive(Hash)] |
| 25 | +/// struct Foo; |
| 26 | +/// |
| 27 | +/// impl PartialEq for Foo { |
| 28 | +/// .. |
| 29 | +/// } |
| 30 | +declare_lint! { |
| 31 | + pub DERIVE_HASH_NOT_EQ, |
| 32 | + Warn, |
| 33 | + "deriving `Hash` but implementing `PartialEq` explicitly" |
| 34 | +} |
| 35 | + |
| 36 | +pub struct Derive; |
| 37 | + |
| 38 | +impl LintPass for Derive { |
| 39 | + fn get_lints(&self) -> LintArray { |
| 40 | + lint_array!(DERIVE_HASH_NOT_EQ) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl LateLintPass for Derive { |
| 45 | + fn check_item(&mut self, cx: &LateContext, item: &Item) { |
| 46 | + /// A `#[derive]`d implementation has a `#[automatically_derived]` attribute. |
| 47 | + fn is_automatically_derived(attr: &Attribute) -> bool { |
| 48 | + if let MetaItem_::MetaWord(ref word) = attr.node.value.node { |
| 49 | + word == &"automatically_derived" |
| 50 | + } |
| 51 | + else { |
| 52 | + false |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // If `item` is an automatically derived `Hash` implementation |
| 57 | + if_let_chain! {[ |
| 58 | + let ItemImpl(_, _, _, Some(ref trait_ref), ref ast_ty, _) = item.node, |
| 59 | + match_path(&trait_ref.path, &HASH_PATH), |
| 60 | + item.attrs.iter().any(is_automatically_derived), |
| 61 | + let Some(peq_trait_def_id) = cx.tcx.lang_items.eq_trait() |
| 62 | + ], { |
| 63 | + let peq_trait_def = cx.tcx.lookup_trait_def(peq_trait_def_id); |
| 64 | + |
| 65 | + cx.tcx.populate_implementations_for_trait_if_necessary(peq_trait_def.trait_ref.def_id); |
| 66 | + let peq_impls = peq_trait_def.borrow_impl_lists(cx.tcx).1; |
| 67 | + let ast_ty_to_ty_cache = cx.tcx.ast_ty_to_ty_cache.borrow(); |
| 68 | + |
| 69 | + |
| 70 | + // Look for the PartialEq implementations for `ty` |
| 71 | + if_let_chain! {[ |
| 72 | + let Some(ty) = ast_ty_to_ty_cache.get(&ast_ty.id), |
| 73 | + let Some(simpl_ty) = simplify_type(cx.tcx, ty, false), |
| 74 | + let Some(impl_ids) = peq_impls.get(&simpl_ty) |
| 75 | + ], { |
| 76 | + for &impl_id in impl_ids { |
| 77 | + let trait_ref = cx.tcx.impl_trait_ref(impl_id).expect("must be a trait implementation"); |
| 78 | + |
| 79 | + // Only care about `impl PartialEq<Foo> for Foo` |
| 80 | + if trait_ref.input_types()[0] == *ty && |
| 81 | + !cx.tcx.get_attrs(impl_id).iter().any(is_automatically_derived) { |
| 82 | + span_lint_and_then( |
| 83 | + cx, DERIVE_HASH_NOT_EQ, item.span, |
| 84 | + &format!("you are deriving `Hash` but have implemented \ |
| 85 | + `PartialEq` explicitely"), |db| { |
| 86 | + if let Some(node_id) = cx.tcx.map.as_local_node_id(impl_id) { |
| 87 | + db.span_note( |
| 88 | + cx.tcx.map.span(node_id), |
| 89 | + "`PartialEq` implemented here" |
| 90 | + ); |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | + }} |
| 96 | + }} |
| 97 | + } |
| 98 | +} |
0 commit comments