|  | 
|  | 1 | +use clippy_utils::diagnostics::span_lint_and_help; | 
|  | 2 | +use clippy_utils::ty::match_type; | 
|  | 3 | +use clippy_utils::{match_qpath, paths, peel_ref_operators}; | 
|  | 4 | +use rustc_hir::intravisit::{walk_expr, Visitor}; | 
|  | 5 | +use rustc_hir::{Block, Expr, ExprKind, PatKind, PathSegment, StmtKind}; | 
|  | 6 | +use rustc_lint::{LateContext, LateLintPass}; | 
|  | 7 | +use rustc_middle::ty::Ty; | 
|  | 8 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; | 
|  | 9 | + | 
|  | 10 | +declare_clippy_lint! { | 
|  | 11 | +    /// ### What it does | 
|  | 12 | +    /// Checks for the creation of a `peekable` iterator that is never `.peek()`ed | 
|  | 13 | +    /// | 
|  | 14 | +    /// ### Why is this bad? | 
|  | 15 | +    /// Creating a peekable iterator without using any of its methods is likely a mistake, | 
|  | 16 | +    /// or just a leftover after a refactor. | 
|  | 17 | +    /// | 
|  | 18 | +    /// ### Example | 
|  | 19 | +    /// ```rust | 
|  | 20 | +    /// let collection = vec![1, 2, 3]; | 
|  | 21 | +    /// let iter = collection.iter().peekable(); | 
|  | 22 | +    /// | 
|  | 23 | +    /// for item in iter { | 
|  | 24 | +    ///     // ... | 
|  | 25 | +    /// } | 
|  | 26 | +    /// ``` | 
|  | 27 | +    /// | 
|  | 28 | +    /// Use instead: | 
|  | 29 | +    /// ```rust | 
|  | 30 | +    /// let collection = vec![1, 2, 3]; | 
|  | 31 | +    /// let iter = collection.iter(); | 
|  | 32 | +    /// | 
|  | 33 | +    /// for item in iter { | 
|  | 34 | +    ///     // ... | 
|  | 35 | +    /// } | 
|  | 36 | +    /// ``` | 
|  | 37 | +    #[clippy::version = "1.64.0"] | 
|  | 38 | +    pub UNUSED_PEEKABLE, | 
|  | 39 | +    suspicious, | 
|  | 40 | +    "creating a peekable iterator without using any of its methods" | 
|  | 41 | +} | 
|  | 42 | + | 
|  | 43 | +declare_lint_pass!(UnusedPeekable => [UNUSED_PEEKABLE]); | 
|  | 44 | + | 
|  | 45 | +impl<'tcx> LateLintPass<'tcx> for UnusedPeekable { | 
|  | 46 | +    fn check_block(&mut self, cx: &LateContext<'tcx>, block: &Block<'tcx>) { | 
|  | 47 | +        // Don't lint `Peekable`s returned from a block | 
|  | 48 | +        if let Some(expr) = block.expr | 
|  | 49 | +            && let Some(ty) = cx.typeck_results().expr_ty_opt(peel_ref_operators(cx, expr)) | 
|  | 50 | +            && match_type(cx, ty, &paths::PEEKABLE) | 
|  | 51 | +        { | 
|  | 52 | +            return; | 
|  | 53 | +        } | 
|  | 54 | + | 
|  | 55 | +        for (idx, stmt) in block.stmts.iter().enumerate() { | 
|  | 56 | +            if !stmt.span.from_expansion() | 
|  | 57 | +                && let StmtKind::Local(local) = stmt.kind | 
|  | 58 | +                && let PatKind::Binding(_, _, ident, _) = local.pat.kind | 
|  | 59 | +                && let Some(init) = local.init | 
|  | 60 | +                && !init.span.from_expansion() | 
|  | 61 | +                && let Some(ty) = cx.typeck_results().expr_ty_opt(init) | 
|  | 62 | +                && match_type(cx, ty, &paths::PEEKABLE) | 
|  | 63 | +            { | 
|  | 64 | +                let ident_str = ident.name.as_str(); | 
|  | 65 | +                let mut vis = PeekableVisitor::new(cx, ident_str); | 
|  | 66 | + | 
|  | 67 | +                if idx + 1 == block.stmts.len() { | 
|  | 68 | +                    if let Some(expr) = block.expr { | 
|  | 69 | +                        vis.visit_expr(expr); | 
|  | 70 | +                    } else { | 
|  | 71 | +                        return; | 
|  | 72 | +                    } | 
|  | 73 | +                } else { | 
|  | 74 | +                    for stmt in &block.stmts[idx..] { | 
|  | 75 | +                        vis.visit_stmt(stmt); | 
|  | 76 | +                    } | 
|  | 77 | +                } | 
|  | 78 | + | 
|  | 79 | +                if !vis.found_peek_call { | 
|  | 80 | +                    span_lint_and_help( | 
|  | 81 | +                        cx, | 
|  | 82 | +                    UNUSED_PEEKABLE, | 
|  | 83 | +                        ident.span, | 
|  | 84 | +                        "`peek` never called on `Peekable` iterator", | 
|  | 85 | +                        None, | 
|  | 86 | +                        "consider removing the call to `peekable`" | 
|  | 87 | +                   ); | 
|  | 88 | +                } | 
|  | 89 | +            } | 
|  | 90 | +        } | 
|  | 91 | +    } | 
|  | 92 | +} | 
|  | 93 | + | 
|  | 94 | +struct PeekableVisitor<'a, 'tcx> { | 
|  | 95 | +    cx: &'a LateContext<'tcx>, | 
|  | 96 | +    expected_ident: &'a str, | 
|  | 97 | +    found_peek_call: bool, | 
|  | 98 | +} | 
|  | 99 | + | 
|  | 100 | +impl<'a, 'tcx> PeekableVisitor<'a, 'tcx> { | 
|  | 101 | +    fn new(cx: &'a LateContext<'tcx>, expected_ident: &'a str) -> Self { | 
|  | 102 | +        Self { | 
|  | 103 | +            cx, | 
|  | 104 | +            expected_ident, | 
|  | 105 | +            found_peek_call: false, | 
|  | 106 | +        } | 
|  | 107 | +    } | 
|  | 108 | +} | 
|  | 109 | + | 
|  | 110 | +impl<'tcx> Visitor<'_> for PeekableVisitor<'_, 'tcx> { | 
|  | 111 | +    fn visit_expr(&mut self, ex: &'_ Expr<'_>) { | 
|  | 112 | +        match ex.kind { | 
|  | 113 | +            // some_function(peekable) | 
|  | 114 | +            // | 
|  | 115 | +            // If the Peekable is passed to a function, stop | 
|  | 116 | +            ExprKind::Call(_, args) => { | 
|  | 117 | +                for arg in args.iter().map(|arg| peel_ref_operators(self.cx, arg)) { | 
|  | 118 | +                    if let ExprKind::Path(ref qpath) = arg.kind | 
|  | 119 | +                        && match_qpath(qpath, &[self.expected_ident]) | 
|  | 120 | +                        && let Some(ty) = self.cx.typeck_results().expr_ty_opt(arg).map(Ty::peel_refs) | 
|  | 121 | +                        && match_type(self.cx, ty.peel_refs(), &paths::PEEKABLE) | 
|  | 122 | +                    { | 
|  | 123 | +                        self.found_peek_call = true; | 
|  | 124 | +                        return; | 
|  | 125 | +                    } | 
|  | 126 | +                } | 
|  | 127 | +            }, | 
|  | 128 | +            // Peekable::peek() | 
|  | 129 | +            ExprKind::MethodCall(PathSegment { ident: method_name, .. }, [arg, ..], _) => { | 
|  | 130 | +                let arg = peel_ref_operators(self.cx, arg); | 
|  | 131 | +                let method_name = method_name.name.as_str(); | 
|  | 132 | + | 
|  | 133 | +                if (method_name == "peek" | 
|  | 134 | +                    || method_name == "peek_mut" | 
|  | 135 | +                    || method_name == "next_if" | 
|  | 136 | +                    || method_name == "next_if_eq") | 
|  | 137 | +                    && let ExprKind::Path(ref qpath) = arg.kind | 
|  | 138 | +                    && match_qpath(qpath, &[self.expected_ident]) | 
|  | 139 | +                    && let Some(ty) = self.cx.typeck_results().expr_ty_opt(arg).map(Ty::peel_refs) | 
|  | 140 | +                    && match_type(self.cx, ty, &paths::PEEKABLE) | 
|  | 141 | +                { | 
|  | 142 | +                    self.found_peek_call = true; | 
|  | 143 | +                    return; | 
|  | 144 | +                } | 
|  | 145 | +            }, | 
|  | 146 | +            _ => {}, | 
|  | 147 | +        } | 
|  | 148 | + | 
|  | 149 | +        walk_expr(self, ex); | 
|  | 150 | +    } | 
|  | 151 | +} | 
0 commit comments