|
| 1 | +use crate::clippy_utils::res::MaybeTypeckRes; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 3 | +use clippy_utils::res::{MaybeDef, MaybeResPath as _}; |
| 4 | +use clippy_utils::sugg::Sugg; |
| 5 | +use clippy_utils::sym; |
| 6 | +use clippy_utils::ty::implements_trait; |
| 7 | +use rustc_ast::BindingMode; |
| 8 | +use rustc_errors::Applicability; |
| 9 | +use rustc_hir::{Expr, ExprKind, LetStmt, Node, PatKind}; |
| 10 | +use rustc_lint::LateContext; |
| 11 | +use rustc_middle::ty; |
| 12 | + |
| 13 | +use super::BY_REF_PEEKABLE_PEEK; |
| 14 | + |
| 15 | +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>) { |
| 16 | + if let ExprKind::MethodCall(maybe_peekable, peekable_recv, [], _) = recv.kind |
| 17 | + && maybe_peekable.ident.name == sym::peekable |
| 18 | + && !peekable_recv.span.from_expansion() |
| 19 | + && let ExprKind::MethodCall(maybe_by_ref, by_ref_recv, [], _) = peekable_recv.kind |
| 20 | + && maybe_by_ref.ident.name == sym::by_ref |
| 21 | + && !by_ref_recv.span.from_expansion() |
| 22 | + && [peekable_recv, recv] |
| 23 | + .into_iter() |
| 24 | + .all(|e| cx.ty_based_def(e).opt_parent(cx).is_diag_item(cx, sym::Iterator)) |
| 25 | + { |
| 26 | + span_lint_and_then( |
| 27 | + cx, |
| 28 | + BY_REF_PEEKABLE_PEEK, |
| 29 | + expr.span, |
| 30 | + "calling `.by_ref().peekable().peek()` will advance the underlying iterator and consume its first output", |
| 31 | + |diag| { |
| 32 | + let span = by_ref_recv.span.shrink_to_hi().with_hi(expr.span.hi()); |
| 33 | + if let ty::Ref(_, iter_ty, _) = cx.typeck_results().expr_ty_adjusted(by_ref_recv).kind() |
| 34 | + && let Some(clone_trait) = cx.tcx.lang_items().clone_trait() |
| 35 | + && implements_trait(cx, *iter_ty, clone_trait, &[]) |
| 36 | + { |
| 37 | + diag.span_suggestion_verbose( |
| 38 | + span, |
| 39 | + "to peek the first item without advancing the underlying iterator, use", |
| 40 | + ".clone().next().as_ref()", |
| 41 | + Applicability::MaybeIncorrect, |
| 42 | + ); |
| 43 | + } |
| 44 | + diag.span_suggestion_verbose( |
| 45 | + span, |
| 46 | + "to advance the underlying iterator, use", |
| 47 | + ".next().as_ref()", |
| 48 | + Applicability::MaybeIncorrect, |
| 49 | + ); |
| 50 | + // If the iterator is a local variable, initialized through a simple binding with an inferred |
| 51 | + // initialization expression, suggest making the initialization expression peekable. |
| 52 | + if let Some(iter_local_id) = by_ref_recv.res_local_id() |
| 53 | + && let Node::LetStmt(LetStmt { |
| 54 | + pat: let_pat, |
| 55 | + ty: None, |
| 56 | + init: Some(init_expr), |
| 57 | + els: None, |
| 58 | + span: let_stmt_span, |
| 59 | + .. |
| 60 | + }) = cx.tcx.parent_hir_node(iter_local_id) |
| 61 | + && let PatKind::Binding(BindingMode::MUT, _, _, None) = let_pat.kind |
| 62 | + && !let_stmt_span.from_expansion() |
| 63 | + // Changing the type of the iterator may prevent the code from compiling |
| 64 | + && let mut app = Applicability::MaybeIncorrect |
| 65 | + && let sugg = |
| 66 | + Sugg::hir_with_context(cx, init_expr, let_stmt_span.ctxt(), "_", &mut app).maybe_paren() |
| 67 | + { |
| 68 | + diag.multipart_suggestion( |
| 69 | + "to make the iterator peekable, use", |
| 70 | + vec![ |
| 71 | + (init_expr.span.source_callsite(), format!("{sugg}.peekable()")), |
| 72 | + (recv.span.with_lo(by_ref_recv.span.hi()), String::new()), |
| 73 | + ], |
| 74 | + app, |
| 75 | + ); |
| 76 | + } else { |
| 77 | + diag.help( |
| 78 | + "you might want to transform the iterator itself using `.peekable()` without using `.by_ref()`", |
| 79 | + ); |
| 80 | + } |
| 81 | + }, |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
0 commit comments