|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use clippy_utils::ty::implements_trait; |
| 3 | +use rustc_hir::def::{DefKind, Res}; |
| 4 | +use rustc_hir::{Item, ItemKind, Path, TraitRef}; |
| 5 | +use rustc_lint::{LateContext, LateLintPass}; |
| 6 | +use rustc_middle::ty::Ty; |
| 7 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 8 | +use rustc_span::symbol::sym; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// This lint is concerned with the semantics of Borrow and Hash for a |
| 13 | + /// type that implements all three of Hash, Borrow<str> and Borrow<[u8]> |
| 14 | + /// as it is impossible to satisfy the semantics of Borrow and Hash for |
| 15 | + /// both Borrow<str> and Borrow<[u8]>. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// |
| 19 | + /// When providing implementations for Borrow<T>, one should consider whether the different |
| 20 | + /// implementations should act as facets or representations of the underlying type. Generic code |
| 21 | + /// typically uses Borrow<T> when it relies on the identical behavior of these additional trait |
| 22 | + /// implementations. These traits will likely appear as additional trait bounds. |
| 23 | + /// |
| 24 | + /// In particular Eq, Ord and Hash must be equivalent for borrowed and owned values: |
| 25 | + /// `x.borrow() == y.borrow()` should give the same result as `x == y`. |
| 26 | + /// It follows then that the following equivalence must hold: |
| 27 | + /// `hash(x) == hash((x as Borrow<[u8]>).borrow()) == hash((x as Borrow<str>).borrow())` |
| 28 | + /// |
| 29 | + /// Unfortunately it doesn't hold as `hash("abc") != hash("abc".as_bytes())`. |
| 30 | + /// |
| 31 | + /// ### Example |
| 32 | + /// |
| 33 | + /// ``` |
| 34 | + /// use std::borrow::Borrow; |
| 35 | + /// use std::hash::{Hash, Hasher}; |
| 36 | + /// |
| 37 | + /// struct ExampleType { |
| 38 | + /// data: String |
| 39 | + /// } |
| 40 | + /// |
| 41 | + /// impl Hash for ExampleType { |
| 42 | + /// fn hash<H: Hasher>(&self, state: &mut H) { |
| 43 | + /// self.data.hash(state); |
| 44 | + /// } |
| 45 | + /// } |
| 46 | + /// |
| 47 | + /// impl Borrow<str> for ExampleType { |
| 48 | + /// fn borrow(&self) -> &str { |
| 49 | + /// &self.data |
| 50 | + /// } |
| 51 | + /// } |
| 52 | + /// |
| 53 | + /// impl Borrow<[u8]> for ExampleType { |
| 54 | + /// fn borrow(&self) -> &[u8] { |
| 55 | + /// self.data.as_bytes() |
| 56 | + /// } |
| 57 | + /// } |
| 58 | + /// ``` |
| 59 | + /// As a consequence, hashing a `&ExampleType` and hashing the result of the two |
| 60 | + /// borrows will result in different values. |
| 61 | + /// |
| 62 | + #[clippy::version = "1.75.0"] |
| 63 | + pub HASH_BORROW_STR_SEMANTICS, |
| 64 | + correctness, |
| 65 | + "Ensures that the semantics of Borrow for Hash are satisfied when Borrow<str> and Borrow<[u8]> are implemented" |
| 66 | +} |
| 67 | + |
| 68 | +declare_lint_pass!(HashBorrowStrSemantics => [HASH_BORROW_STR_SEMANTICS]); |
| 69 | + |
| 70 | +impl LateLintPass<'_> for HashBorrowStrSemantics { |
| 71 | + /// We are emitting this lint at the Hash impl of a type that implements all |
| 72 | + /// three of Hash, Borrow<str> and Borrow<[u8]>. |
| 73 | + /// |
| 74 | + /// We check that we are in the Hash impl |
| 75 | + /// Then we check that the type implements Borrow<str> and Borrow<[u8]> |
| 76 | + fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { |
| 77 | + if let ItemKind::Impl(imp) = item.kind |
| 78 | + && let Some(TraitRef {path: Path {span, res, ..}, ..}) = imp.of_trait |
| 79 | + && let ty = cx.tcx.type_of(item.owner_id).instantiate_identity() |
| 80 | + && let Some(hash_id) = cx.tcx.get_diagnostic_item(sym::Hash) |
| 81 | + && Res::Def(DefKind::Trait, hash_id) == *res |
| 82 | + && let Some(borrow_id) = cx.tcx.get_diagnostic_item(sym::Borrow) |
| 83 | + // since we are in the Hash impl, we don't need to check for that. |
| 84 | + // we need only to check for Borrow<str> and Borrow<[u8]> |
| 85 | + && implements_trait(cx, ty, borrow_id, &[cx.tcx.types.str_.into()]) |
| 86 | + &&implements_trait(cx, ty, borrow_id, &[Ty::new_slice(cx.tcx, cx.tcx.types.u8).into()]) |
| 87 | + { |
| 88 | + span_lint_and_then( |
| 89 | + cx, |
| 90 | + HASH_BORROW_STR_SEMANTICS, |
| 91 | + *span, |
| 92 | + "can't satisfy the semantics of `Hash` when both `Borrow<str>` and `Borrow<[u8]>` are implemented", |
| 93 | + |diag| { |
| 94 | + diag.note("types that implement Hash and Borrow<T> should have equivalent Hash results for borrowed and owned values"); |
| 95 | + |
| 96 | + diag.note("this is not the case for Borrow<str> and Borrow<[u8]> as the two types result in different Hash values"); |
| 97 | + |
| 98 | + diag.help("consider removing one implementation of Borrow (Borrow<str> or Borrow<[u8]>) or not implementing Hash for this type"); |
| 99 | + }, |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments