|
| 1 | +use clippy_utils::diagnostics::span_lint_and_help; |
| 2 | +use clippy_utils::ty::{match_type, peel_mid_ty_refs_is_mutable}; |
| 3 | +use clippy_utils::{fn_def_id, path_to_local_id, paths, peel_ref_operators}; |
| 4 | +use rustc_ast::Mutability; |
| 5 | +use rustc_hir::intravisit::{walk_expr, Visitor}; |
| 6 | +use rustc_hir::lang_items::LangItem; |
| 7 | +use rustc_hir::{Block, Expr, ExprKind, HirId, Local, Node, PatKind, PathSegment, StmtKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass}; |
| 9 | +use rustc_middle::ty::Ty; |
| 10 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// ### What it does |
| 14 | + /// Checks for the creation of a `peekable` iterator that is never `.peek()`ed |
| 15 | + /// |
| 16 | + /// ### Why is this bad? |
| 17 | + /// Creating a peekable iterator without using any of its methods is likely a mistake, |
| 18 | + /// or just a leftover after a refactor. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```rust |
| 22 | + /// let collection = vec![1, 2, 3]; |
| 23 | + /// let iter = collection.iter().peekable(); |
| 24 | + /// |
| 25 | + /// for item in iter { |
| 26 | + /// // ... |
| 27 | + /// } |
| 28 | + /// ``` |
| 29 | + /// |
| 30 | + /// Use instead: |
| 31 | + /// ```rust |
| 32 | + /// let collection = vec![1, 2, 3]; |
| 33 | + /// let iter = collection.iter(); |
| 34 | + /// |
| 35 | + /// for item in iter { |
| 36 | + /// // ... |
| 37 | + /// } |
| 38 | + /// ``` |
| 39 | + #[clippy::version = "1.64.0"] |
| 40 | + pub UNUSED_PEEKABLE, |
| 41 | + suspicious, |
| 42 | + "creating a peekable iterator without using any of its methods" |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Default)] |
| 46 | +pub struct UnusedPeekable { |
| 47 | + visited: Vec<HirId>, |
| 48 | +} |
| 49 | + |
| 50 | +impl_lint_pass!(UnusedPeekable => [UNUSED_PEEKABLE]); |
| 51 | + |
| 52 | +impl<'tcx> LateLintPass<'tcx> for UnusedPeekable { |
| 53 | + fn check_block(&mut self, cx: &LateContext<'tcx>, block: &Block<'tcx>) { |
| 54 | + // Don't lint `Peekable`s returned from a block |
| 55 | + if let Some(expr) = block.expr |
| 56 | + && let Some(ty) = cx.typeck_results().expr_ty_opt(peel_ref_operators(cx, expr)) |
| 57 | + && match_type(cx, ty, &paths::PEEKABLE) |
| 58 | + { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + for (idx, stmt) in block.stmts.iter().enumerate() { |
| 63 | + if !stmt.span.from_expansion() |
| 64 | + && let StmtKind::Local(local) = stmt.kind |
| 65 | + && !self.visited.contains(&local.pat.hir_id) |
| 66 | + && let PatKind::Binding(_, _, ident, _) = local.pat.kind |
| 67 | + && let Some(init) = local.init |
| 68 | + && !init.span.from_expansion() |
| 69 | + && let Some(ty) = cx.typeck_results().expr_ty_opt(init) |
| 70 | + && let (ty, _, Mutability::Mut) = peel_mid_ty_refs_is_mutable(ty) |
| 71 | + && match_type(cx, ty, &paths::PEEKABLE) |
| 72 | + { |
| 73 | + let mut vis = PeekableVisitor::new(cx, local.pat.hir_id); |
| 74 | + |
| 75 | + if idx + 1 == block.stmts.len() && block.expr.is_none() { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + for stmt in &block.stmts[idx..] { |
| 80 | + vis.visit_stmt(stmt); |
| 81 | + } |
| 82 | + |
| 83 | + if let Some(expr) = block.expr { |
| 84 | + vis.visit_expr(expr); |
| 85 | + } |
| 86 | + |
| 87 | + if !vis.found_peek_call { |
| 88 | + span_lint_and_help( |
| 89 | + cx, |
| 90 | + UNUSED_PEEKABLE, |
| 91 | + ident.span, |
| 92 | + "`peek` never called on `Peekable` iterator", |
| 93 | + None, |
| 94 | + "consider removing the call to `peekable`" |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +struct PeekableVisitor<'a, 'tcx> { |
| 103 | + cx: &'a LateContext<'tcx>, |
| 104 | + expected_hir_id: HirId, |
| 105 | + found_peek_call: bool, |
| 106 | +} |
| 107 | + |
| 108 | +impl<'a, 'tcx> PeekableVisitor<'a, 'tcx> { |
| 109 | + fn new(cx: &'a LateContext<'tcx>, expected_hir_id: HirId) -> Self { |
| 110 | + Self { |
| 111 | + cx, |
| 112 | + expected_hir_id, |
| 113 | + found_peek_call: false, |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl<'tcx> Visitor<'_> for PeekableVisitor<'_, 'tcx> { |
| 119 | + fn visit_expr(&mut self, ex: &'_ Expr<'_>) { |
| 120 | + if path_to_local_id(ex, self.expected_hir_id) { |
| 121 | + for (_, node) in self.cx.tcx.hir().parent_iter(ex.hir_id) { |
| 122 | + match node { |
| 123 | + Node::Expr(expr) => { |
| 124 | + match expr.kind { |
| 125 | + // some_function(peekable) |
| 126 | + // |
| 127 | + // If the Peekable is passed to a function, stop |
| 128 | + ExprKind::Call(_, args) => { |
| 129 | + if let Some(func_did) = fn_def_id(self.cx, expr) |
| 130 | + && let Ok(into_iter_did) = self |
| 131 | + .cx |
| 132 | + .tcx |
| 133 | + .lang_items() |
| 134 | + .require(LangItem::IntoIterIntoIter) |
| 135 | + && func_did == into_iter_did |
| 136 | + { |
| 137 | + // Probably a for loop desugar, stop searching |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + for arg in args.iter().map(|arg| peel_ref_operators(self.cx, arg)) { |
| 142 | + if let ExprKind::Path(_) = arg.kind |
| 143 | + && let Some(ty) = self |
| 144 | + .cx |
| 145 | + .typeck_results() |
| 146 | + .expr_ty_opt(arg) |
| 147 | + .map(Ty::peel_refs) |
| 148 | + && match_type(self.cx, ty, &paths::PEEKABLE) |
| 149 | + { |
| 150 | + self.found_peek_call = true; |
| 151 | + return; |
| 152 | + } |
| 153 | + } |
| 154 | + }, |
| 155 | + // Peekable::peek() |
| 156 | + ExprKind::MethodCall(PathSegment { ident: method_name, .. }, [arg, ..], _) => { |
| 157 | + let arg = peel_ref_operators(self.cx, arg); |
| 158 | + let method_name = method_name.name.as_str(); |
| 159 | + |
| 160 | + if (method_name == "peek" |
| 161 | + || method_name == "peek_mut" |
| 162 | + || method_name == "next_if" |
| 163 | + || method_name == "next_if_eq") |
| 164 | + && let ExprKind::Path(_) = arg.kind |
| 165 | + && let Some(ty) = self.cx.typeck_results().expr_ty_opt(arg).map(Ty::peel_refs) |
| 166 | + && match_type(self.cx, ty, &paths::PEEKABLE) |
| 167 | + { |
| 168 | + self.found_peek_call = true; |
| 169 | + return; |
| 170 | + } |
| 171 | + }, |
| 172 | + // Don't bother if moved into a struct |
| 173 | + ExprKind::Struct(..) => { |
| 174 | + self.found_peek_call = true; |
| 175 | + return; |
| 176 | + }, |
| 177 | + _ => {}, |
| 178 | + } |
| 179 | + }, |
| 180 | + Node::Local(Local { init: Some(init), .. }) => { |
| 181 | + if let Some(ty) = self.cx.typeck_results().expr_ty_opt(init) |
| 182 | + && let (ty, _, Mutability::Mut) = peel_mid_ty_refs_is_mutable(ty) |
| 183 | + && match_type(self.cx, ty, &paths::PEEKABLE) |
| 184 | + { |
| 185 | + self.found_peek_call = true; |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + break; |
| 190 | + }, |
| 191 | + Node::Stmt(stmt) => match stmt.kind { |
| 192 | + StmtKind::Expr(_) | StmtKind::Semi(_) => {}, |
| 193 | + _ => { |
| 194 | + self.found_peek_call = true; |
| 195 | + return; |
| 196 | + }, |
| 197 | + }, |
| 198 | + Node::Block(_) => {}, |
| 199 | + _ => { |
| 200 | + break; |
| 201 | + }, |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + walk_expr(self, ex); |
| 207 | + } |
| 208 | +} |
0 commit comments