|
| 1 | +use crate::methods::MAP_WITH_UNUSED_ARGUMENT_OVER_RANGES; |
| 2 | +use clippy_config::msrvs::{self, Msrv}; |
| 3 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 4 | +use clippy_utils::source::snippet_with_applicability; |
| 5 | +use clippy_utils::sugg::Sugg; |
| 6 | +use clippy_utils::{eager_or_lazy, higher, usage}; |
| 7 | +use rustc_ast::LitKind; |
| 8 | +use rustc_ast::ast::RangeLimits; |
| 9 | +use rustc_data_structures::packed::Pu128; |
| 10 | +use rustc_errors::Applicability; |
| 11 | +use rustc_hir::{Body, Closure, Expr, ExprKind}; |
| 12 | +use rustc_lint::LateContext; |
| 13 | +use rustc_span::Span; |
| 14 | + |
| 15 | +fn extract_count_with_applicability( |
| 16 | + cx: &LateContext<'_>, |
| 17 | + range: higher::Range<'_>, |
| 18 | + applicability: &mut Applicability, |
| 19 | +) -> Option<String> { |
| 20 | + let start = range.start?; |
| 21 | + let end = range.end?; |
| 22 | + // TODO: This doens't handle if either the start or end are negative literals, or if the start is |
| 23 | + // not a literal. In the first case, we need to be careful about how we handle computing the |
| 24 | + // count to avoid overflows. In the second, we may need to add parenthesis to make the |
| 25 | + // suggestion correct. |
| 26 | + if let ExprKind::Lit(lit) = start.kind |
| 27 | + && let LitKind::Int(Pu128(lower_bound), _) = lit.node |
| 28 | + { |
| 29 | + if let ExprKind::Lit(lit) = end.kind |
| 30 | + && let LitKind::Int(Pu128(upper_bound), _) = lit.node |
| 31 | + { |
| 32 | + // Here we can explicitly calculate the number of iterations |
| 33 | + let count = if upper_bound >= lower_bound { |
| 34 | + match range.limits { |
| 35 | + RangeLimits::HalfOpen => upper_bound - lower_bound, |
| 36 | + RangeLimits::Closed => (upper_bound - lower_bound).checked_add(1)?, |
| 37 | + } |
| 38 | + } else { |
| 39 | + 0 |
| 40 | + }; |
| 41 | + return Some(format!("{count}")); |
| 42 | + } |
| 43 | + let end_snippet = Sugg::hir_with_applicability(cx, end, "...", applicability) |
| 44 | + .maybe_par() |
| 45 | + .into_string(); |
| 46 | + if lower_bound == 0 { |
| 47 | + if range.limits == RangeLimits::Closed { |
| 48 | + return Some(format!("{end_snippet} + 1")); |
| 49 | + } |
| 50 | + return Some(end_snippet); |
| 51 | + } |
| 52 | + if range.limits == RangeLimits::Closed { |
| 53 | + return Some(format!("{end_snippet} - {}", lower_bound - 1)); |
| 54 | + } |
| 55 | + return Some(format!("{end_snippet} - {lower_bound}")); |
| 56 | + } |
| 57 | + None |
| 58 | +} |
| 59 | + |
| 60 | +pub(super) fn check( |
| 61 | + cx: &LateContext<'_>, |
| 62 | + ex: &Expr<'_>, |
| 63 | + receiver: &Expr<'_>, |
| 64 | + arg: &Expr<'_>, |
| 65 | + msrv: &Msrv, |
| 66 | + method_call_span: Span, |
| 67 | +) { |
| 68 | + let mut applicability = Applicability::MaybeIncorrect; |
| 69 | + if let Some(range) = higher::Range::hir(receiver) |
| 70 | + && let ExprKind::Closure(Closure { body, .. }) = arg.kind |
| 71 | + && let body_hir = cx.tcx.hir().body(*body) |
| 72 | + && let Body { |
| 73 | + params: [param], |
| 74 | + value: body_expr, |
| 75 | + } = body_hir |
| 76 | + && !usage::BindingUsageFinder::are_params_used(cx, body_hir) |
| 77 | + && let Some(count) = extract_count_with_applicability(cx, range, &mut applicability) |
| 78 | + { |
| 79 | + let method_to_use_name; |
| 80 | + let new_span; |
| 81 | + let use_take; |
| 82 | + |
| 83 | + if eager_or_lazy::switch_to_eager_eval(cx, body_expr) { |
| 84 | + if msrv.meets(msrvs::REPEAT_N) { |
| 85 | + method_to_use_name = "repeat_n"; |
| 86 | + let body_snippet = snippet_with_applicability(cx, body_expr.span, "..", &mut applicability); |
| 87 | + new_span = (arg.span, format!("{body_snippet}, {count}")); |
| 88 | + use_take = false; |
| 89 | + } else { |
| 90 | + method_to_use_name = "repeat"; |
| 91 | + let body_snippet = snippet_with_applicability(cx, body_expr.span, "..", &mut applicability); |
| 92 | + new_span = (arg.span, body_snippet.to_string()); |
| 93 | + use_take = true; |
| 94 | + } |
| 95 | + } else if msrv.meets(msrvs::REPEAT_WITH) { |
| 96 | + method_to_use_name = "repeat_with"; |
| 97 | + new_span = (param.span, String::new()); |
| 98 | + use_take = true; |
| 99 | + } else { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + // We need to provide nonempty parts to diag.multipart_suggestion so we |
| 104 | + // collate all our parts here and then remove those that are empty. |
| 105 | + let mut parts = vec![ |
| 106 | + ( |
| 107 | + receiver.span.to(method_call_span), |
| 108 | + format!("std::iter::{method_to_use_name}"), |
| 109 | + ), |
| 110 | + new_span, |
| 111 | + ]; |
| 112 | + if use_take { |
| 113 | + parts.push((ex.span.shrink_to_hi(), format!(".take({count})"))); |
| 114 | + } |
| 115 | + |
| 116 | + span_lint_and_then( |
| 117 | + cx, |
| 118 | + MAP_WITH_UNUSED_ARGUMENT_OVER_RANGES, |
| 119 | + ex.span, |
| 120 | + "map of a closure that does not depend on its parameter over a range", |
| 121 | + |diag| { |
| 122 | + diag.multipart_suggestion( |
| 123 | + if use_take { |
| 124 | + format!("remove the explicit range and use `{method_to_use_name}` and `take`") |
| 125 | + } else { |
| 126 | + format!("remove the explicit range and use `{method_to_use_name}`") |
| 127 | + }, |
| 128 | + parts, |
| 129 | + applicability, |
| 130 | + ); |
| 131 | + }, |
| 132 | + ); |
| 133 | + } |
| 134 | +} |
0 commit comments