]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/manual_str_repeat.rs
Merge remote-tracking branch 'upstream/master' into rustup
[rust.git] / clippy_lints / src / methods / manual_str_repeat.rs
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;
13 use std::borrow::Cow;
14
15 use super::MANUAL_STR_REPEAT;
16
17 enum RepeatKind {
18     String,
19     Char(char),
20 }
21
22 fn get_ty_param(ty: Ty<'_>) -> Option<Ty<'_>> {
23     if let ty::Adt(_, subs) = ty.kind() {
24         subs.types().next()
25     } else {
26         None
27     }
28 }
29
30 fn parse_repeat_arg(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<RepeatKind> {
31     if let ExprKind::Lit(lit) = &e.kind {
32         match lit.node {
33             LitKind::Str(..) => Some(RepeatKind::String),
34             LitKind::Char(c) => Some(RepeatKind::Char(c)),
35             _ => None,
36         }
37     } else {
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))
42         {
43             Some(RepeatKind::String)
44         } else {
45             let ty = ty.peel_refs();
46             (ty.is_str() || is_type_lang_item(cx, ty, LangItem::String)).then_some(RepeatKind::String)
47         }
48     }
49 }
50
51 pub(super) fn check(
52     cx: &LateContext<'_>,
53     collect_expr: &Expr<'_>,
54     take_expr: &Expr<'_>,
55     take_self_arg: &Expr<'_>,
56     take_arg: &Expr<'_>,
57 ) {
58     if_chain! {
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();
69         then {
70             let mut app = Applicability::MachineApplicable;
71             let count_snip = snippet_with_context(cx, take_arg.span, ctxt, "..", &mut app).0;
72
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,
81                     },
82                 RepeatKind::String =>
83                     Sugg::hir_with_context(cx, repeat_arg, ctxt, "..", &mut app).maybe_par().to_string().into(),
84             };
85
86             span_lint_and_sugg(
87                 cx,
88                 MANUAL_STR_REPEAT,
89                 collect_expr.span,
90                 "manual implementation of `str::repeat` using iterators",
91                 "try this",
92                 format!("{val_str}.repeat({count_snip})"),
93                 app
94             )
95         }
96     }
97 }