Skip to content

Commit 55adb13

Browse files
committed
Auto merge of #3603 - xfix:random-state-lint, r=phansch
random_state lint
2 parents 0fc5857 + 11b957e commit 55adb13

File tree

7 files changed

+103
-1
lines changed

7 files changed

+103
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,7 @@ All notable changes to this project will be documented in this file.
814814
[`ptr_offset_with_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast
815815
[`pub_enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_enum_variant_names
816816
[`question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#question_mark
817+
[`random_state`]: https://rust-lang.github.io/rust-clippy/master/index.html#random_state
817818
[`range_minus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_minus_one
818819
[`range_plus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_plus_one
819820
[`range_step_by_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_step_by_zero

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
99

10-
[There are 290 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
10+
[There are 291 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1111

1212
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1313

clippy_lints/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ pub mod precedence;
180180
pub mod ptr;
181181
pub mod ptr_offset_with_cast;
182182
pub mod question_mark;
183+
pub mod random_state;
183184
pub mod ranges;
184185
pub mod redundant_clone;
185186
pub mod redundant_field_names;
@@ -486,6 +487,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
486487
reg.register_late_lint_pass(box ptr_offset_with_cast::Pass);
487488
reg.register_late_lint_pass(box redundant_clone::RedundantClone);
488489
reg.register_late_lint_pass(box slow_vector_initialization::Pass);
490+
reg.register_late_lint_pass(box random_state::Pass);
489491

490492
reg.register_lint_group("clippy::restriction", Some("clippy_restriction"), vec![
491493
arithmetic::FLOAT_ARITHMETIC,
@@ -1025,6 +1027,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
10251027
fallible_impl_from::FALLIBLE_IMPL_FROM,
10261028
mutex_atomic::MUTEX_INTEGER,
10271029
needless_borrow::NEEDLESS_BORROW,
1030+
random_state::RANDOM_STATE,
10281031
redundant_clone::REDUNDANT_CLONE,
10291032
unwrap::PANICKING_UNWRAP,
10301033
unwrap::UNNECESSARY_UNWRAP,

clippy_lints/src/random_state.rs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

clippy_lints/src/utils/paths.rs

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
7373
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];
7474
pub const PTR_NULL: [&str; 2] = ["ptr", "null"];
7575
pub const PTR_NULL_MUT: [&str; 2] = ["ptr", "null_mut"];
76+
pub const RANDOM_STATE: [&str; 5] = ["std", "collections", "hash", "map", "RandomState"];
7677
pub const RANGE: [&str; 3] = ["core", "ops", "Range"];
7778
pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"];
7879
pub const RANGE_FROM: [&str; 3] = ["core", "ops", "RangeFrom"];

tests/ui/random_state.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![warn(clippy::random_state)]
2+
3+
use std::collections::hash_map::RandomState;
4+
use std::collections::hash_map::{DefaultHasher, HashMap};
5+
use std::hash::BuildHasherDefault;
6+
7+
fn main() {
8+
// Should warn
9+
let mut map = HashMap::new();
10+
map.insert(3, 4);
11+
let mut map = HashMap::with_hasher(RandomState::new());
12+
map.insert(true, false);
13+
let _map: HashMap<_, _> = vec![(2, 3)].into_iter().collect();
14+
let _vec: Vec<HashMap<i32, i32>>;
15+
// Shouldn't warn
16+
let _map: HashMap<i32, i32, BuildHasherDefault<DefaultHasher>> = HashMap::default();
17+
let mut map = HashMap::with_hasher(BuildHasherDefault::<DefaultHasher>::default());
18+
map.insert("a", "b");
19+
}

tests/ui/random_state.stderr

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error: usage of RandomState
2+
--> $DIR/random_state.rs:9:19
3+
|
4+
LL | let mut map = HashMap::new();
5+
| ^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::random-state` implied by `-D warnings`
8+
9+
error: usage of RandomState
10+
--> $DIR/random_state.rs:11:19
11+
|
12+
LL | let mut map = HashMap::with_hasher(RandomState::new());
13+
| ^^^^^^^^^^^^^^^^^^^^
14+
15+
error: usage of RandomState
16+
--> $DIR/random_state.rs:13:15
17+
|
18+
LL | let _map: HashMap<_, _> = vec![(2, 3)].into_iter().collect();
19+
| ^^^^^^^^^^^^^
20+
21+
error: usage of RandomState
22+
--> $DIR/random_state.rs:14:19
23+
|
24+
LL | let _vec: Vec<HashMap<i32, i32>>;
25+
| ^^^^^^^^^^^^^^^^^
26+
27+
error: aborting due to 4 previous errors
28+

0 commit comments

Comments
 (0)