|
| 1 | +use clippy_utils::diagnostics::span_lint_and_note; |
| 2 | +use clippy_utils::{is_trait_method, match_def_path, paths}; |
| 3 | +use rustc_ast::LitKind; |
| 4 | +use rustc_hir::{Expr, ExprKind}; |
| 5 | +use rustc_lint::LateContext; |
| 6 | +use rustc_middle::ty::{self}; |
| 7 | +use rustc_span::sym; |
| 8 | + |
| 9 | +use super::ITER_OUT_OF_BOUNDS; |
| 10 | + |
| 11 | +/// Attempts to extract the length out of an iterator expression. |
| 12 | +fn get_iterator_length<'tcx>(cx: &LateContext<'tcx>, iter: &'tcx Expr<'tcx>) -> Option<u128> { |
| 13 | + let iter_ty = cx.typeck_results().expr_ty(iter); |
| 14 | + |
| 15 | + if let ty::Adt(adt, substs) = iter_ty.kind() { |
| 16 | + let did = adt.did(); |
| 17 | + |
| 18 | + if match_def_path(cx, did, &paths::ARRAY_INTO_ITER) { |
| 19 | + // For array::IntoIter<T, const N: usize>, the length is the second generic |
| 20 | + // parameter. |
| 21 | + substs |
| 22 | + .const_at(1) |
| 23 | + .try_eval_target_usize(cx.tcx, cx.param_env) |
| 24 | + .map(u128::from) |
| 25 | + } else if match_def_path(cx, did, &paths::SLICE_ITER) |
| 26 | + && let ExprKind::MethodCall(_, recv, ..) = iter.kind |
| 27 | + && let ExprKind::Array(array) = recv.peel_borrows().kind |
| 28 | + { |
| 29 | + // For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..) |
| 30 | + array.len().try_into().ok() |
| 31 | + } else if match_def_path(cx, did, &paths::ITER_EMPTY) { |
| 32 | + Some(0) |
| 33 | + } else if match_def_path(cx, did, &paths::ITER_ONCE) { |
| 34 | + Some(1) |
| 35 | + } else { |
| 36 | + None |
| 37 | + } |
| 38 | + } else { |
| 39 | + None |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +fn check<'tcx>( |
| 44 | + cx: &LateContext<'tcx>, |
| 45 | + expr: &'tcx Expr<'tcx>, |
| 46 | + recv: &'tcx Expr<'tcx>, |
| 47 | + arg: &'tcx Expr<'tcx>, |
| 48 | + message: &'static str, |
| 49 | + note: &'static str, |
| 50 | +) { |
| 51 | + if is_trait_method(cx, expr, sym::Iterator) |
| 52 | + && let Some(len) = get_iterator_length(cx, recv) |
| 53 | + && let ExprKind::Lit(lit) = arg.kind |
| 54 | + && let LitKind::Int(skip, _) = lit.node |
| 55 | + && skip > len |
| 56 | + { |
| 57 | + span_lint_and_note(cx, ITER_OUT_OF_BOUNDS, expr.span, message, None, note); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +pub(super) fn check_skip<'tcx>( |
| 62 | + cx: &LateContext<'tcx>, |
| 63 | + expr: &'tcx Expr<'tcx>, |
| 64 | + recv: &'tcx Expr<'tcx>, |
| 65 | + arg: &'tcx Expr<'tcx>, |
| 66 | +) { |
| 67 | + check( |
| 68 | + cx, |
| 69 | + expr, |
| 70 | + recv, |
| 71 | + arg, |
| 72 | + "this `.skip()` call skips more items than the iterator will produce", |
| 73 | + "this operation is useless and will create an empty iterator", |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +pub(super) fn check_take<'tcx>( |
| 78 | + cx: &LateContext<'tcx>, |
| 79 | + expr: &'tcx Expr<'tcx>, |
| 80 | + recv: &'tcx Expr<'tcx>, |
| 81 | + arg: &'tcx Expr<'tcx>, |
| 82 | +) { |
| 83 | + check( |
| 84 | + cx, |
| 85 | + expr, |
| 86 | + recv, |
| 87 | + arg, |
| 88 | + "this `.take()` call takes more items than the iterator will produce", |
| 89 | + "this operation is useless and the returned iterator will simply yield the same items", |
| 90 | + ); |
| 91 | +} |
0 commit comments