|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::ty::is_type_diagnostic_item; |
| 3 | +use clippy_utils::visitors::for_each_expr_with_closures; |
| 4 | +use clippy_utils::{get_enclosing_block, get_parent_node, path_to_local_id}; |
| 5 | +use core::ops::ControlFlow; |
| 6 | +use rustc_hir::{Block, ExprKind, HirId, Local, Node, PatKind}; |
| 7 | +use rustc_lint::{LateContext, LateLintPass}; |
| 8 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 9 | +use rustc_span::symbol::sym; |
| 10 | +use rustc_span::Symbol; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks for collections that are never queried. |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// Putting effort into constructing a collection but then never querying it might indicate that |
| 18 | + /// the author forgot to do whatever they intended to do with the collection. Example: Clone |
| 19 | + /// a vector, sort it for iteration, but then mistakenly iterate the original vector |
| 20 | + /// instead. |
| 21 | + /// |
| 22 | + /// ### Example |
| 23 | + /// ```rust |
| 24 | + /// # let samples = vec![3, 1, 2]; |
| 25 | + /// let mut sorted_samples = samples.clone(); |
| 26 | + /// sorted_samples.sort(); |
| 27 | + /// for sample in &samples { // Oops, meant to use `sorted_samples`. |
| 28 | + /// println!("{sample}"); |
| 29 | + /// } |
| 30 | + /// ``` |
| 31 | + /// Use instead: |
| 32 | + /// ```rust |
| 33 | + /// # let samples = vec![3, 1, 2]; |
| 34 | + /// let mut sorted_samples = samples.clone(); |
| 35 | + /// sorted_samples.sort(); |
| 36 | + /// for sample in &sorted_samples { |
| 37 | + /// println!("{sample}"); |
| 38 | + /// } |
| 39 | + /// ``` |
| 40 | + #[clippy::version = "1.69.0"] |
| 41 | + pub COLLECTION_IS_NEVER_READ, |
| 42 | + nursery, |
| 43 | + "a collection is never queried" |
| 44 | +} |
| 45 | +declare_lint_pass!(CollectionIsNeverRead => [COLLECTION_IS_NEVER_READ]); |
| 46 | + |
| 47 | +static COLLECTIONS: [Symbol; 10] = [ |
| 48 | + sym::BTreeMap, |
| 49 | + sym::BTreeSet, |
| 50 | + sym::BinaryHeap, |
| 51 | + sym::HashMap, |
| 52 | + sym::HashSet, |
| 53 | + sym::LinkedList, |
| 54 | + sym::Option, |
| 55 | + sym::String, |
| 56 | + sym::Vec, |
| 57 | + sym::VecDeque, |
| 58 | +]; |
| 59 | + |
| 60 | +impl<'tcx> LateLintPass<'tcx> for CollectionIsNeverRead { |
| 61 | + fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) { |
| 62 | + // Look for local variables whose type is a container. Search surrounding bock for read access. |
| 63 | + let ty = cx.typeck_results().pat_ty(local.pat); |
| 64 | + if COLLECTIONS.iter().any(|&sym| is_type_diagnostic_item(cx, ty, sym)) |
| 65 | + && let PatKind::Binding(_, local_id, _, _) = local.pat.kind |
| 66 | + && let Some(enclosing_block) = get_enclosing_block(cx, local.hir_id) |
| 67 | + && has_no_read_access(cx, local_id, enclosing_block) |
| 68 | + { |
| 69 | + span_lint(cx, COLLECTION_IS_NEVER_READ, local.span, "collection is never read"); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +fn has_no_read_access<'tcx>(cx: &LateContext<'tcx>, id: HirId, block: &'tcx Block<'tcx>) -> bool { |
| 75 | + let mut has_access = false; |
| 76 | + let mut has_read_access = false; |
| 77 | + |
| 78 | + // Inspect all expressions and sub-expressions in the block. |
| 79 | + for_each_expr_with_closures(cx, block, |expr| { |
| 80 | + // Ignore expressions that are not simply `id`. |
| 81 | + if !path_to_local_id(expr, id) { |
| 82 | + return ControlFlow::Continue(()); |
| 83 | + } |
| 84 | + |
| 85 | + // `id` is being accessed. Investigate if it's a read access. |
| 86 | + has_access = true; |
| 87 | + |
| 88 | + // `id` appearing in the left-hand side of an assignment is not a read access: |
| 89 | + // |
| 90 | + // id = ...; // Not reading `id`. |
| 91 | + if let Some(Node::Expr(parent)) = get_parent_node(cx.tcx, expr.hir_id) |
| 92 | + && let ExprKind::Assign(lhs, ..) = parent.kind |
| 93 | + && path_to_local_id(lhs, id) |
| 94 | + { |
| 95 | + return ControlFlow::Continue(()); |
| 96 | + } |
| 97 | + |
| 98 | + // Method call on `id` in a statement ignores any return value, so it's not a read access: |
| 99 | + // |
| 100 | + // id.foo(...); // Not reading `id`. |
| 101 | + // |
| 102 | + // Only assuming this for "official" methods defined on the type. For methods defined in extension |
| 103 | + // traits (identified as local, based on the orphan rule), pessimistically assume that they might |
| 104 | + // have side effects, so consider them a read. |
| 105 | + if let Some(Node::Expr(parent)) = get_parent_node(cx.tcx, expr.hir_id) |
| 106 | + && let ExprKind::MethodCall(_, receiver, _, _) = parent.kind |
| 107 | + && path_to_local_id(receiver, id) |
| 108 | + && let Some(Node::Stmt(..)) = get_parent_node(cx.tcx, parent.hir_id) |
| 109 | + && let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id) |
| 110 | + && !method_def_id.is_local() |
| 111 | + { |
| 112 | + return ControlFlow::Continue(()); |
| 113 | + } |
| 114 | + |
| 115 | + // Any other access to `id` is a read access. Stop searching. |
| 116 | + has_read_access = true; |
| 117 | + ControlFlow::Break(()) |
| 118 | + }); |
| 119 | + |
| 120 | + // Ignore collections that have no access at all. Other lints should catch them. |
| 121 | + has_access && !has_read_access |
| 122 | +} |
0 commit comments