|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::higher::ForLoop; |
| 3 | +use clippy_utils::match_any_def_paths; |
| 4 | +use clippy_utils::paths::{ |
| 5 | + HASHMAP_DRAIN, HASHMAP_ITER, HASHMAP_ITER_MUT, HASHMAP_KEYS, HASHMAP_VALUES, HASHMAP_VALUES_MUT, HASHSET_DRAIN, |
| 6 | + HASHSET_ITER_TY, |
| 7 | +}; |
| 8 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 9 | +use rustc_lint::{LateContext, LateLintPass}; |
| 10 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 11 | +use rustc_span::sym; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// This is a restriction lint which prevents the use of hash types (i.e., `HashSet` and `HashMap`) in for loops. |
| 16 | + /// |
| 17 | + /// ### Why is this bad? |
| 18 | + /// Because hash types are unordered, when iterated through such as in a for loop, the values are returned in |
| 19 | + /// an undefined order. As a result, on redundant systems this may cause inconsistencies and anomalies. |
| 20 | + /// In addition, the unknown order of the elements may reduce readability or introduce other undesired |
| 21 | + /// side effects. |
| 22 | + /// |
| 23 | + /// ### Example |
| 24 | + /// ```no_run |
| 25 | + /// let my_map = std::collections::HashMap::<i32, String>::new(); |
| 26 | + /// for (key, value) in my_map { /* ... */ } |
| 27 | + /// ``` |
| 28 | + /// Use instead: |
| 29 | + /// ```no_run |
| 30 | + /// let my_map = std::collections::HashMap::<i32, String>::new(); |
| 31 | + /// let mut keys = my_map.keys().clone().collect::<Vec<_>>(); |
| 32 | + /// keys.sort(); |
| 33 | + /// for key in keys { |
| 34 | + /// let value = &my_map[key]; |
| 35 | + /// } |
| 36 | + /// ``` |
| 37 | + #[clippy::version = "1.75.0"] |
| 38 | + pub ITER_OVER_HASH_TYPE, |
| 39 | + restriction, |
| 40 | + "iterating over unordered hash-based types (`HashMap` and `HashSet`)" |
| 41 | +} |
| 42 | + |
| 43 | +declare_lint_pass!(IterOverHashType => [ITER_OVER_HASH_TYPE]); |
| 44 | + |
| 45 | +impl LateLintPass<'_> for IterOverHashType { |
| 46 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'_ rustc_hir::Expr<'_>) { |
| 47 | + if let Some(for_loop) = ForLoop::hir(expr) |
| 48 | + && !for_loop.body.span.from_expansion() |
| 49 | + && let ty = cx.typeck_results().expr_ty(for_loop.arg).peel_refs() |
| 50 | + && let Some(adt) = ty.ty_adt_def() |
| 51 | + && let did = adt.did() |
| 52 | + && (match_any_def_paths( |
| 53 | + cx, |
| 54 | + did, |
| 55 | + &[ |
| 56 | + &HASHMAP_KEYS, |
| 57 | + &HASHMAP_VALUES, |
| 58 | + &HASHMAP_VALUES_MUT, |
| 59 | + &HASHMAP_ITER, |
| 60 | + &HASHMAP_ITER_MUT, |
| 61 | + &HASHMAP_DRAIN, |
| 62 | + &HASHSET_ITER_TY, |
| 63 | + &HASHSET_DRAIN, |
| 64 | + ], |
| 65 | + ) |
| 66 | + .is_some() |
| 67 | + || is_type_diagnostic_item(cx, ty, sym::HashMap) |
| 68 | + || is_type_diagnostic_item(cx, ty, sym::HashSet)) |
| 69 | + { |
| 70 | + span_lint( |
| 71 | + cx, |
| 72 | + ITER_OVER_HASH_TYPE, |
| 73 | + expr.span, |
| 74 | + "iteration over unordered hash-based type", |
| 75 | + ); |
| 76 | + }; |
| 77 | + } |
| 78 | +} |
0 commit comments