|
| 1 | +use clippy_utils::diagnostics::span_lint_hir_and_then; |
| 2 | +use clippy_utils::msrvs::{self, Msrv}; |
| 3 | +use clippy_utils::source::snippet_opt; |
| 4 | +use clippy_utils::visitors::{is_local_used, local_used_once}; |
| 5 | +use clippy_utils::{is_trait_method, path_to_local_id}; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{BindingAnnotation, ExprKind, Local, Node, PatKind, StmtKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 10 | +use rustc_span::sym; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks for cases where [`BuildHasher::hash_one`] can be used. |
| 15 | + /// |
| 16 | + /// [`BuildHasher::hash_one`]: https://doc.rust-lang.org/std/hash/trait.BuildHasher.html#method.hash_one |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// It is more concise to use the `hash_one` method. |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```rust |
| 23 | + /// use std::hash::{BuildHasher, Hash, Hasher}; |
| 24 | + /// use std::collections::hash_map::RandomState; |
| 25 | + /// |
| 26 | + /// let s = RandomState::new(); |
| 27 | + /// let value = vec![1, 2, 3]; |
| 28 | + /// |
| 29 | + /// let mut hasher = s.build_hasher(); |
| 30 | + /// value.hash(&mut hasher); |
| 31 | + /// let hash = hasher.finish(); |
| 32 | + /// ``` |
| 33 | + /// Use instead: |
| 34 | + /// ```rust |
| 35 | + /// use std::hash::BuildHasher; |
| 36 | + /// use std::collections::hash_map::RandomState; |
| 37 | + /// |
| 38 | + /// let s = RandomState::new(); |
| 39 | + /// let value = vec![1, 2, 3]; |
| 40 | + /// |
| 41 | + /// let hash = s.hash_one(&value); |
| 42 | + /// ``` |
| 43 | + #[clippy::version = "1.74.0"] |
| 44 | + pub MANUAL_HASH_ONE, |
| 45 | + complexity, |
| 46 | + "manual implementations of `BuildHasher::hash_one`" |
| 47 | +} |
| 48 | + |
| 49 | +pub struct ManualHashOne { |
| 50 | + msrv: Msrv, |
| 51 | +} |
| 52 | + |
| 53 | +impl ManualHashOne { |
| 54 | + #[must_use] |
| 55 | + pub fn new(msrv: Msrv) -> Self { |
| 56 | + Self { msrv } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl_lint_pass!(ManualHashOne => [MANUAL_HASH_ONE]); |
| 61 | + |
| 62 | +impl LateLintPass<'_> for ManualHashOne { |
| 63 | + fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) { |
| 64 | + // `let mut hasher = seg.build_hasher();` |
| 65 | + if let PatKind::Binding(BindingAnnotation::MUT, hasher, _, None) = local.pat.kind |
| 66 | + && let Some(init) = local.init |
| 67 | + && !init.span.from_expansion() |
| 68 | + && let ExprKind::MethodCall(seg, build_hasher, [], _) = init.kind |
| 69 | + && seg.ident.name == sym!(build_hasher) |
| 70 | + |
| 71 | + && let Node::Stmt(local_stmt) = cx.tcx.hir().get_parent(local.hir_id) |
| 72 | + && let Node::Block(block) = cx.tcx.hir().get_parent(local_stmt.hir_id) |
| 73 | + |
| 74 | + && let mut stmts = block.stmts.iter() |
| 75 | + .skip_while(|stmt| stmt.hir_id != local_stmt.hir_id) |
| 76 | + .skip(1) |
| 77 | + .filter(|&stmt| is_local_used(cx, stmt, hasher)) |
| 78 | + |
| 79 | + // `hashed_value.hash(&mut hasher);` |
| 80 | + && let Some(hash_stmt) = stmts.next() |
| 81 | + && let StmtKind::Semi(hash_expr) = hash_stmt.kind |
| 82 | + && !hash_expr.span.from_expansion() |
| 83 | + && let ExprKind::MethodCall(seg, hashed_value, [ref_to_hasher], _) = hash_expr.kind |
| 84 | + && seg.ident.name == sym::hash |
| 85 | + && is_trait_method(cx, hash_expr, sym::Hash) |
| 86 | + && path_to_local_id(ref_to_hasher.peel_borrows(), hasher) |
| 87 | + |
| 88 | + && let maybe_finish_stmt = stmts.next() |
| 89 | + // There should be no more statements referencing `hasher` |
| 90 | + && stmts.next().is_none() |
| 91 | + |
| 92 | + // `hasher.finish()`, may be anywhere in a statement or the trailing expr of the block |
| 93 | + && let Some(path_expr) = local_used_once(cx, (maybe_finish_stmt, block.expr), hasher) |
| 94 | + && let Node::Expr(finish_expr) = cx.tcx.hir().get_parent(path_expr.hir_id) |
| 95 | + && !finish_expr.span.from_expansion() |
| 96 | + && let ExprKind::MethodCall(seg, _, [], _) = finish_expr.kind |
| 97 | + && seg.ident.name == sym!(finish) |
| 98 | + |
| 99 | + && self.msrv.meets(msrvs::BUILD_HASHER_HASH_ONE) |
| 100 | + { |
| 101 | + span_lint_hir_and_then( |
| 102 | + cx, |
| 103 | + MANUAL_HASH_ONE, |
| 104 | + finish_expr.hir_id, |
| 105 | + finish_expr.span, |
| 106 | + "manual implementation of `BuildHasher::hash_one`", |
| 107 | + |diag| { |
| 108 | + if let Some(build_hasher) = snippet_opt(cx, build_hasher.span) |
| 109 | + && let Some(hashed_value) = snippet_opt(cx, hashed_value.span) |
| 110 | + { |
| 111 | + diag.multipart_suggestion( |
| 112 | + "try", |
| 113 | + vec![ |
| 114 | + (local_stmt.span, String::new()), |
| 115 | + (hash_stmt.span, String::new()), |
| 116 | + ( |
| 117 | + finish_expr.span, |
| 118 | + // `needless_borrows_for_generic_args` will take care of |
| 119 | + // removing the `&` when it isn't needed |
| 120 | + format!("{build_hasher}.hash_one(&{hashed_value})") |
| 121 | + ) |
| 122 | + ], |
| 123 | + Applicability::MachineApplicable, |
| 124 | + ); |
| 125 | + |
| 126 | + } |
| 127 | + }, |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + extract_msrv_attr!(LateContext); |
| 133 | +} |
0 commit comments