add iter_over_hash_type lint

This commit is contained in:
Jacherr 2023-11-11 00:20:47 +00:00
parent 9a4dd106d9
commit cb90674aed
7 changed files with 167 additions and 0 deletions

View file

@ -0,0 +1,44 @@
//@aux-build:proc_macros.rs
#![warn(clippy::iter_over_hash_type)]
use std::collections::{HashMap, HashSet};
extern crate proc_macros;
fn main() {
let hash_set = HashSet::<i32>::new();
let hash_map = HashMap::<i32, i32>::new();
let vec = Vec::<i32>::new();
for x in &hash_set {
let _ = x;
}
for x in hash_set.iter() {
let _ = x;
}
for x in hash_set {
let _ = x;
}
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;
}
// 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() {}
}
}

View file

@ -0,0 +1,53 @@
error: iterating over unordered hash-based type
--> $DIR/iter_over_hash_type.rs:13: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: iterating over unordered hash-based type
--> $DIR/iter_over_hash_type.rs:16:5
|
LL | / for x in hash_set.iter() {
LL | | let _ = x;
LL | | }
| |_____^
error: iterating over unordered hash-based type
--> $DIR/iter_over_hash_type.rs:19:5
|
LL | / for x in hash_set {
LL | | let _ = x;
LL | | }
| |_____^
error: iterating over unordered hash-based type
--> $DIR/iter_over_hash_type.rs:22:5
|
LL | / for (x, y) in &hash_map {
LL | | let _ = (x, y);
LL | | }
| |_____^
error: iterating over unordered hash-based type
--> $DIR/iter_over_hash_type.rs:25:5
|
LL | / for x in hash_map.keys() {
LL | | let _ = x;
LL | | }
| |_____^
error: iterating over unordered hash-based type
--> $DIR/iter_over_hash_type.rs:28:5
|
LL | / for x in hash_map.values() {
LL | | let _ = x;
LL | | }
| |_____^
error: aborting due to 6 previous errors