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