|
| 1 | +use crate::utils::{match_type, paths, span_lint}; |
| 2 | +use rustc::hir::Ty; |
| 3 | +use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; |
| 4 | +use rustc::ty::subst::UnpackedKind; |
| 5 | +use rustc::ty::TyKind; |
| 6 | +use rustc::{declare_tool_lint, lint_array}; |
| 7 | + |
| 8 | +/// **What it does:** Checks for usage of `RandomState` |
| 9 | +/// |
| 10 | +/// **Why is this bad?** Some applications don't need collision prevention |
| 11 | +/// which lowers the performance. |
| 12 | +/// |
| 13 | +/// **Known problems:** None. |
| 14 | +/// |
| 15 | +/// **Example:** |
| 16 | +/// ```rust |
| 17 | +/// fn x() { |
| 18 | +/// let mut map = std::collections::HashMap::new(); |
| 19 | +/// map.insert(3, 4); |
| 20 | +/// } |
| 21 | +/// ``` |
| 22 | +declare_clippy_lint! { |
| 23 | + pub RANDOM_STATE, |
| 24 | + nursery, |
| 25 | + "use of RandomState" |
| 26 | +} |
| 27 | + |
| 28 | +pub struct Pass; |
| 29 | + |
| 30 | +impl LintPass for Pass { |
| 31 | + fn get_lints(&self) -> LintArray { |
| 32 | + lint_array!(RANDOM_STATE) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { |
| 37 | + fn check_ty(&mut self, cx: &LateContext<'a, 'tcx>, ty: &Ty) { |
| 38 | + if let Some(tys) = cx.tables.node_id_to_type_opt(ty.hir_id) { |
| 39 | + if let TyKind::Adt(_, substs) = tys.sty { |
| 40 | + for subst in substs { |
| 41 | + if let UnpackedKind::Type(build_hasher) = subst.unpack() { |
| 42 | + if match_type(cx, build_hasher, &paths::RANDOM_STATE) { |
| 43 | + span_lint(cx, RANDOM_STATE, ty.span, "usage of RandomState"); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments