1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::is_path_diagnostic_item;
3 use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
4 use clippy_utils::sugg::Sugg;
5 use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
6 use if_chain::if_chain;
7 use rustc_ast::LitKind;
8 use rustc_errors::Applicability;
9 use rustc_hir::{Expr, ExprKind, LangItem};
10 use rustc_lint::LateContext;
11 use rustc_middle::ty::{self, Ty};
12 use rustc_span::symbol::sym;
15 use super::MANUAL_STR_REPEAT;
22 fn get_ty_param(ty: Ty<'_>) -> Option<Ty<'_>> {
23 if let ty::Adt(_, subs) = ty.kind() {
30 fn parse_repeat_arg(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<RepeatKind> {
31 if let ExprKind::Lit(lit) = &e.kind {
33 LitKind::Str(..) => Some(RepeatKind::String),
34 LitKind::Char(c) => Some(RepeatKind::Char(c)),
38 let ty = cx.typeck_results().expr_ty(e);
39 if is_type_lang_item(cx, ty, LangItem::String)
40 || (is_type_lang_item(cx, ty, LangItem::OwnedBox) && get_ty_param(ty).map_or(false, Ty::is_str))
41 || (is_type_diagnostic_item(cx, ty, sym::Cow) && get_ty_param(ty).map_or(false, Ty::is_str))
43 Some(RepeatKind::String)
45 let ty = ty.peel_refs();
46 (ty.is_str() || is_type_lang_item(cx, ty, LangItem::String)).then_some(RepeatKind::String)
53 collect_expr: &Expr<'_>,
55 take_self_arg: &Expr<'_>,
59 if let ExprKind::Call(repeat_fn, [repeat_arg]) = take_self_arg.kind;
60 if is_path_diagnostic_item(cx, repeat_fn, sym::iter_repeat);
61 if is_type_lang_item(cx, cx.typeck_results().expr_ty(collect_expr), LangItem::String);
62 if let Some(take_id) = cx.typeck_results().type_dependent_def_id(take_expr.hir_id);
63 if let Some(iter_trait_id) = cx.tcx.get_diagnostic_item(sym::Iterator);
64 if cx.tcx.trait_of_item(take_id) == Some(iter_trait_id);
65 if let Some(repeat_kind) = parse_repeat_arg(cx, repeat_arg);
66 let ctxt = collect_expr.span.ctxt();
67 if ctxt == take_expr.span.ctxt();
68 if ctxt == take_self_arg.span.ctxt();
70 let mut app = Applicability::MachineApplicable;
71 let count_snip = snippet_with_context(cx, take_arg.span, ctxt, "..", &mut app).0;
73 let val_str = match repeat_kind {
74 RepeatKind::Char(_) if repeat_arg.span.ctxt() != ctxt => return,
75 RepeatKind::Char('\'') => r#""'""#.into(),
76 RepeatKind::Char('"') => r#""\"""#.into(),
77 RepeatKind::Char(_) =>
78 match snippet_with_applicability(cx, repeat_arg.span, "..", &mut app) {
79 Cow::Owned(s) => Cow::Owned(format!("\"{}\"", &s[1..s.len() - 1])),
80 s @ Cow::Borrowed(_) => s,
83 Sugg::hir_with_context(cx, repeat_arg, ctxt, "..", &mut app).maybe_par().to_string().into(),
90 "manual implementation of `str::repeat` using iterators",
92 format!("{val_str}.repeat({count_snip})"),