-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implement new lint iter_over_hash_type
#11791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,78 @@ | ||
use clippy_utils::diagnostics::span_lint; | ||
use clippy_utils::higher::ForLoop; | ||
use clippy_utils::match_any_def_paths; | ||
use clippy_utils::paths::{ | ||
HASHMAP_DRAIN, HASHMAP_ITER, HASHMAP_ITER_MUT, HASHMAP_KEYS, HASHMAP_VALUES, HASHMAP_VALUES_MUT, HASHSET_DRAIN, | ||
HASHSET_ITER_TY, | ||
}; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// This is a restriction lint which prevents the use of hash types (i.e., `HashSet` and `HashMap`) in for loops. | ||
/// | ||
/// ### Why is this bad? | ||
/// Because hash types are unordered, when iterated through such as in a for loop, the values are returned in | ||
/// an undefined order. As a result, on redundant systems this may cause inconsistencies and anomalies. | ||
/// In addition, the unknown order of the elements may reduce readability or introduce other undesired | ||
/// side effects. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// let my_map = std::collections::HashMap::<i32, String>::new(); | ||
/// for (key, value) in my_map { /* ... */ } | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// let my_map = std::collections::HashMap::<i32, String>::new(); | ||
/// let mut keys = my_map.keys().clone().collect::<Vec<_>>(); | ||
/// keys.sort(); | ||
/// for key in keys { | ||
/// let value = &my_map[key]; | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.75.0"] | ||
pub ITER_OVER_HASH_TYPE, | ||
restriction, | ||
"iterating over unordered hash-based types (`HashMap` and `HashSet`)" | ||
} | ||
|
||
declare_lint_pass!(IterOverHashType => [ITER_OVER_HASH_TYPE]); | ||
|
||
impl LateLintPass<'_> for IterOverHashType { | ||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) { | ||
if let Some(for_loop) = ForLoop::hir(expr) | ||
&& !for_loop.body.span.from_expansion() | ||
&& let ty = cx.typeck_results().expr_ty(for_loop.arg).peel_refs() | ||
&& let Some(adt) = ty.ty_adt_def() | ||
&& let did = adt.did() | ||
&& (match_any_def_paths( | ||
cx, | ||
did, | ||
&[ | ||
&HASHMAP_KEYS, | ||
&HASHMAP_VALUES, | ||
&HASHMAP_VALUES_MUT, | ||
&HASHMAP_ITER, | ||
&HASHMAP_ITER_MUT, | ||
&HASHMAP_DRAIN, | ||
&HASHSET_ITER_TY, | ||
&HASHSET_DRAIN, | ||
], | ||
) | ||
.is_some() | ||
|| is_type_diagnostic_item(cx, ty, sym::HashMap) | ||
|| is_type_diagnostic_item(cx, ty, sym::HashSet)) | ||
{ | ||
span_lint( | ||
cx, | ||
ITER_OVER_HASH_TYPE, | ||
expr.span, | ||
"iteration over unordered hash-based type", | ||
); | ||
}; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,59 @@ | ||
//@aux-build:proc_macros.rs | ||
|
||
#![warn(clippy::iter_over_hash_type)] | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
extern crate proc_macros; | ||
|
||
fn main() { | ||
let mut hash_set = HashSet::<i32>::new(); | ||
let mut 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.clone() { | ||
let _ = x; | ||
} | ||
for x in hash_set.drain() { | ||
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; | ||
} | ||
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; | ||
} | ||
|
||
// 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() {} | ||
} | ||
} |
This file contains hidden or 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,93 @@ | ||
error: iteration 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: iteration 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: iteration over unordered hash-based type | ||
--> $DIR/iter_over_hash_type.rs:19: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:22: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:25: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:28: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:31: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:34: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:37: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:40: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:43:5 | ||
| | ||
LL | / for x in hash_map.drain() { | ||
LL | | let _ = x; | ||
LL | | } | ||
| |_____^ | ||
|
||
error: aborting due to 11 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like tests with type aliases as well, like
FxHashMap
. This should work since you use the typeck table but it should be linted