Auto merge of #11791 - Jacherr:iter_over_hash_type, r=Jarcho
Implement new lint `iter_over_hash_type` Implements and fixes https://github.com/rust-lang/rust-clippy/issues/11788 This PR adds a new *restriction* lint `iter_over_hash_type` which prevents `Hash`-types (that is, `HashSet` and `HashMap`) from being used as the iterator in `for` loops. The justification for this is because in `Hash`-based types, the ordering of items is not guaranteed and may vary between executions of the same program on the same hardware. In addition, it reduces readability due to the unclear iteration order. The implementation of this lint also ensures the following: - Calls to `HashMap::keys`, `HashMap::values`, and `HashSet::iter` are also denied when used in `for` loops, - When this expression is used in procedural macros, it is not linted/denied. changelog: add new `iter_over_hash_type` lint to prevent unordered iterations through hashed data structures
This commit is contained in:
commit
0c42e451d6
7 changed files with 273 additions and 0 deletions
74
tests/ui/iter_over_hash_type.rs
Normal file
74
tests/ui/iter_over_hash_type.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
//@aux-build:proc_macros.rs
|
||||
#![feature(rustc_private)]
|
||||
#![warn(clippy::iter_over_hash_type)]
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
extern crate rustc_data_structures;
|
||||
|
||||
extern crate proc_macros;
|
||||
|
||||
fn main() {
|
||||
let mut hash_set = HashSet::<i32>::new();
|
||||
let mut hash_map = HashMap::<i32, i32>::new();
|
||||
let mut fx_hash_map = rustc_data_structures::fx::FxHashMap::<i32, i32>::default();
|
||||
let mut fx_hash_set = rustc_data_structures::fx::FxHashMap::<i32, i32>::default();
|
||||
let vec = Vec::<i32>::new();
|
||||
|
||||
// test hashset
|
||||
for x in &hash_set {
|
||||
let _ = x;
|
||||
}
|
||||
for x in hash_set.iter() {
|
||||
let _ = x;
|
||||
}
|
||||
for x in hash_set.clone() {
|
||||
let _ = x;
|
||||
}
|
||||
for x in hash_set.drain() {
|
||||
let _ = x;
|
||||
}
|
||||
|
||||
// test hashmap
|
||||
for (x, y) in &hash_map {
|
||||
let _ = (x, y);
|
||||
}
|
||||
for x in hash_map.keys() {
|
||||
let _ = x;
|
||||
}
|
||||
for x in hash_map.values() {
|
||||
let _ = x;
|
||||
}
|
||||
for x in hash_map.values_mut() {
|
||||
*x += 1;
|
||||
}
|
||||
for x in hash_map.iter() {
|
||||
let _ = x;
|
||||
}
|
||||
for x in hash_map.clone() {
|
||||
let _ = x;
|
||||
}
|
||||
for x in hash_map.drain() {
|
||||
let _ = x;
|
||||
}
|
||||
|
||||
// test type-aliased hashers
|
||||
for x in fx_hash_set {
|
||||
let _ = x;
|
||||
}
|
||||
for x in fx_hash_map {
|
||||
let _ = x;
|
||||
}
|
||||
|
||||
// shouldnt fire
|
||||
for x in &vec {
|
||||
let _ = x;
|
||||
}
|
||||
for x in vec {
|
||||
let _ = x;
|
||||
}
|
||||
|
||||
// should not lint, this comes from an external crate
|
||||
proc_macros::external! {
|
||||
for _ in HashMap::<i32, i32>::new() {}
|
||||
}
|
||||
}
|
||||
109
tests/ui/iter_over_hash_type.stderr
Normal file
109
tests/ui/iter_over_hash_type.stderr
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:18:5
|
||||
|
|
||||
LL | / for x in &hash_set {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
|
||||
= note: `-D clippy::iter-over-hash-type` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::iter_over_hash_type)]`
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:21:5
|
||||
|
|
||||
LL | / for x in hash_set.iter() {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:24:5
|
||||
|
|
||||
LL | / for x in hash_set.clone() {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:27:5
|
||||
|
|
||||
LL | / for x in hash_set.drain() {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:32:5
|
||||
|
|
||||
LL | / for (x, y) in &hash_map {
|
||||
LL | | let _ = (x, y);
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:35:5
|
||||
|
|
||||
LL | / for x in hash_map.keys() {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:38:5
|
||||
|
|
||||
LL | / for x in hash_map.values() {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:41:5
|
||||
|
|
||||
LL | / for x in hash_map.values_mut() {
|
||||
LL | | *x += 1;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:44:5
|
||||
|
|
||||
LL | / for x in hash_map.iter() {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:47:5
|
||||
|
|
||||
LL | / for x in hash_map.clone() {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:50:5
|
||||
|
|
||||
LL | / for x in hash_map.drain() {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:55:5
|
||||
|
|
||||
LL | / for x in fx_hash_set {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: iteration over unordered hash-based type
|
||||
--> $DIR/iter_over_hash_type.rs:58:5
|
||||
|
|
||||
LL | / for x in fx_hash_map {
|
||||
LL | | let _ = x;
|
||||
LL | | }
|
||||
| |_____^
|
||||
|
||||
error: aborting due to 13 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue