|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::eager_or_lazy::switch_to_eager_eval; |
| 3 | +use clippy_utils::macros::span_is_local; |
| 4 | +use clippy_utils::msrvs::{self, Msrv}; |
| 5 | +use clippy_utils::source::{HasSession, snippet_with_applicability}; |
| 6 | +use clippy_utils::ty::implements_trait; |
| 7 | +use clippy_utils::{higher, peel_blocks_with_stmt, span_contains_comment}; |
| 8 | +use rustc_ast::ast::LitKind; |
| 9 | +use rustc_ast::{RangeLimits, UnOp}; |
| 10 | +use rustc_data_structures::packed::Pu128; |
| 11 | +use rustc_errors::Applicability; |
| 12 | +use rustc_hir::QPath::Resolved; |
| 13 | +use rustc_hir::def::Res; |
| 14 | +use rustc_hir::{Expr, ExprKind, Pat}; |
| 15 | +use rustc_lint::LateContext; |
| 16 | +use rustc_span::source_map::Spanned; |
| 17 | +use rustc_span::sym; |
| 18 | + |
| 19 | +use super::MANUAL_SLICE_FILL; |
| 20 | + |
| 21 | +pub(super) fn check<'tcx>( |
| 22 | + cx: &LateContext<'tcx>, |
| 23 | + pat: &'tcx Pat<'_>, |
| 24 | + arg: &'tcx Expr<'_>, |
| 25 | + body: &'tcx Expr<'_>, |
| 26 | + expr: &'tcx Expr<'_>, |
| 27 | + msrv: &Msrv, |
| 28 | +) { |
| 29 | + if !msrv.meets(msrvs::SLICE_FILL) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // `for _ in 0..slice.len() { slice[_] = value; }` |
| 34 | + if let Some(higher::Range { |
| 35 | + start: Some(start), |
| 36 | + end: Some(end), |
| 37 | + limits: RangeLimits::HalfOpen, |
| 38 | + }) = higher::Range::hir(arg) |
| 39 | + && let ExprKind::Lit(Spanned { |
| 40 | + node: LitKind::Int(Pu128(0), _), |
| 41 | + .. |
| 42 | + }) = start.kind |
| 43 | + && let ExprKind::Block(..) = body.kind |
| 44 | + // Check if the body is an assignment to a slice element. |
| 45 | + && let ExprKind::Assign(assignee, assignval, _) = peel_blocks_with_stmt(body).kind |
| 46 | + && let ExprKind::Index(slice, _, _) = assignee.kind |
| 47 | + // Check if `len()` is used for the range end. |
| 48 | + && let ExprKind::MethodCall(path, recv,..) = end.kind |
| 49 | + && path.ident.name == sym::len |
| 50 | + // Check if the slice which is being assigned to is the same as the one being iterated over. |
| 51 | + && let ExprKind::Path(Resolved(_, recv_path)) = recv.kind |
| 52 | + && let ExprKind::Path(Resolved(_, slice_path)) = slice.kind |
| 53 | + && recv_path.res == slice_path.res |
| 54 | + && !assignval.span.from_expansion() |
| 55 | + // It is generally not equivalent to use the `fill` method if `assignval` can have side effects |
| 56 | + && switch_to_eager_eval(cx, assignval) |
| 57 | + && span_is_local(assignval.span) |
| 58 | + // The `fill` method requires that the slice's element type implements the `Clone` trait. |
| 59 | + && let Some(clone_trait) = cx.tcx.lang_items().clone_trait() |
| 60 | + && implements_trait(cx, cx.typeck_results().expr_ty(slice), clone_trait, &[]) |
| 61 | + { |
| 62 | + sugg(cx, body, expr, slice.span, assignval.span); |
| 63 | + } |
| 64 | + // `for _ in &mut slice { *_ = value; }` |
| 65 | + else if let ExprKind::AddrOf(_, _, recv) = arg.kind |
| 66 | + // Check if the body is an assignment to a slice element. |
| 67 | + && let ExprKind::Assign(assignee, assignval, _) = peel_blocks_with_stmt(body).kind |
| 68 | + && let ExprKind::Unary(UnOp::Deref, slice_iter) = assignee.kind |
| 69 | + && let ExprKind::Path(Resolved(_, recv_path)) = recv.kind |
| 70 | + // Check if the slice which is being assigned to is the same as the one being iterated over. |
| 71 | + && let ExprKind::Path(Resolved(_, slice_path)) = slice_iter.kind |
| 72 | + && let Res::Local(local) = slice_path.res |
| 73 | + && local == pat.hir_id |
| 74 | + && !assignval.span.from_expansion() |
| 75 | + && switch_to_eager_eval(cx, assignval) |
| 76 | + && span_is_local(assignval.span) |
| 77 | + // The `fill` method cannot be used if the slice's element type does not implement the `Clone` trait. |
| 78 | + && let Some(clone_trait) = cx.tcx.lang_items().clone_trait() |
| 79 | + && implements_trait(cx, cx.typeck_results().expr_ty(recv), clone_trait, &[]) |
| 80 | + { |
| 81 | + sugg(cx, body, expr, recv_path.span, assignval.span); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +fn sugg<'tcx>( |
| 86 | + cx: &LateContext<'tcx>, |
| 87 | + body: &'tcx Expr<'_>, |
| 88 | + expr: &'tcx Expr<'_>, |
| 89 | + slice_span: rustc_span::Span, |
| 90 | + assignval_span: rustc_span::Span, |
| 91 | +) { |
| 92 | + let mut app = if span_contains_comment(cx.sess().source_map(), body.span) { |
| 93 | + Applicability::MaybeIncorrect // Comments may be informational. |
| 94 | + } else { |
| 95 | + Applicability::MachineApplicable |
| 96 | + }; |
| 97 | + |
| 98 | + span_lint_and_sugg( |
| 99 | + cx, |
| 100 | + MANUAL_SLICE_FILL, |
| 101 | + expr.span, |
| 102 | + "manually filling a slice", |
| 103 | + "try", |
| 104 | + format!( |
| 105 | + "{}.fill({});", |
| 106 | + snippet_with_applicability(cx, slice_span, "..", &mut app), |
| 107 | + snippet_with_applicability(cx, assignval_span, "..", &mut app), |
| 108 | + ), |
| 109 | + app, |
| 110 | + ); |
| 111 | +} |
0 commit comments