forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#3603 - xfix:random-state-lint, r=phansch
random_state lint
- Loading branch information
Showing
7 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#![warn(clippy::random_state)] | ||
|
||
use std::collections::hash_map::RandomState; | ||
use std::collections::hash_map::{DefaultHasher, HashMap}; | ||
use std::hash::BuildHasherDefault; | ||
|
||
fn main() { | ||
// Should warn | ||
let mut map = HashMap::new(); | ||
map.insert(3, 4); | ||
let mut map = HashMap::with_hasher(RandomState::new()); | ||
map.insert(true, false); | ||
let _map: HashMap<_, _> = vec![(2, 3)].into_iter().collect(); | ||
let _vec: Vec<HashMap<i32, i32>>; | ||
// Shouldn't warn | ||
let _map: HashMap<i32, i32, BuildHasherDefault<DefaultHasher>> = HashMap::default(); | ||
let mut map = HashMap::with_hasher(BuildHasherDefault::<DefaultHasher>::default()); | ||
map.insert("a", "b"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error: usage of RandomState | ||
--> $DIR/random_state.rs:9:19 | ||
| | ||
LL | let mut map = HashMap::new(); | ||
| ^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::random-state` implied by `-D warnings` | ||
|
||
error: usage of RandomState | ||
--> $DIR/random_state.rs:11:19 | ||
| | ||
LL | let mut map = HashMap::with_hasher(RandomState::new()); | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: usage of RandomState | ||
--> $DIR/random_state.rs:13:15 | ||
| | ||
LL | let _map: HashMap<_, _> = vec![(2, 3)].into_iter().collect(); | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: usage of RandomState | ||
--> $DIR/random_state.rs:14:19 | ||
| | ||
LL | let _vec: Vec<HashMap<i32, i32>>; | ||
| ^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 4 previous errors | ||
|