|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet_with_context; |
| 3 | +use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item, match_type}; |
| 4 | +use clippy_utils::{is_expr_path_def_path, paths}; |
| 5 | +use if_chain::if_chain; |
| 6 | +use rustc_ast::util::parser::PREC_POSTFIX; |
| 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_span::symbol::{sym, Symbol}; |
| 12 | + |
| 13 | +use super::MANUAL_STR_REPEAT; |
| 14 | + |
| 15 | +enum RepeatKind { |
| 16 | + Str, |
| 17 | + String, |
| 18 | + Char, |
| 19 | +} |
| 20 | + |
| 21 | +fn parse_repeat_arg(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<RepeatKind> { |
| 22 | + if let ExprKind::Lit(lit) = &e.kind { |
| 23 | + match lit.node { |
| 24 | + LitKind::Str(..) => Some(RepeatKind::Str), |
| 25 | + LitKind::Char(_) => Some(RepeatKind::Char), |
| 26 | + _ => None, |
| 27 | + } |
| 28 | + } else { |
| 29 | + let ty = cx.typeck_results().expr_ty(e); |
| 30 | + if is_type_diagnostic_item(cx, ty, sym::string_type) |
| 31 | + || is_type_lang_item(cx, ty, LangItem::OwnedBox) |
| 32 | + || match_type(cx, ty, &paths::COW) |
| 33 | + { |
| 34 | + Some(RepeatKind::String) |
| 35 | + } else { |
| 36 | + let ty = ty.peel_refs(); |
| 37 | + (ty.is_str() || is_type_diagnostic_item(cx, ty, sym::string_type)).then(|| RepeatKind::Str) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +pub(super) fn check( |
| 43 | + cx: &LateContext<'_>, |
| 44 | + collect_expr: &Expr<'_>, |
| 45 | + take_expr: &Expr<'_>, |
| 46 | + take_self_arg: &Expr<'_>, |
| 47 | + take_arg: &Expr<'_>, |
| 48 | +) { |
| 49 | + if_chain! { |
| 50 | + if let ExprKind::Call(repeat_fn, [repeat_arg]) = take_self_arg.kind; |
| 51 | + if is_expr_path_def_path(cx, repeat_fn, &paths::ITER_REPEAT); |
| 52 | + if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(collect_expr), sym::string_type); |
| 53 | + if let Some(collect_id) = cx.typeck_results().type_dependent_def_id(collect_expr.hir_id); |
| 54 | + if let Some(take_id) = cx.typeck_results().type_dependent_def_id(take_expr.hir_id); |
| 55 | + if let Some(iter_trait_id) = cx.tcx.get_diagnostic_item(sym::Iterator); |
| 56 | + if cx.tcx.trait_of_item(collect_id) == Some(iter_trait_id); |
| 57 | + if cx.tcx.trait_of_item(take_id) == Some(iter_trait_id); |
| 58 | + if let Some(repeat_kind) = parse_repeat_arg(cx, repeat_arg); |
| 59 | + let ctxt = collect_expr.span.ctxt(); |
| 60 | + if ctxt == take_expr.span.ctxt(); |
| 61 | + if ctxt == take_self_arg.span.ctxt(); |
| 62 | + then { |
| 63 | + let mut app = Applicability::MachineApplicable; |
| 64 | + let (val_snip, val_is_mac) = snippet_with_context(cx, repeat_arg.span, ctxt, "..", &mut app); |
| 65 | + let count_snip = snippet_with_context(cx, take_arg.span, ctxt, "..", &mut app).0; |
| 66 | + |
| 67 | + let val_str = match repeat_kind { |
| 68 | + RepeatKind::String => format!("(&{})", val_snip), |
| 69 | + RepeatKind::Str if !val_is_mac && repeat_arg.precedence().order() < PREC_POSTFIX => { |
| 70 | + format!("({})", val_snip) |
| 71 | + }, |
| 72 | + RepeatKind::Str => val_snip.into(), |
| 73 | + RepeatKind::Char if val_snip == r#"'"'"# => r#""\"""#.into(), |
| 74 | + RepeatKind::Char if val_snip == r#"'\''"# => r#""'""#.into(), |
| 75 | + RepeatKind::Char => format!("\"{}\"", &val_snip[1..val_snip.len() - 1]), |
| 76 | + }; |
| 77 | + |
| 78 | + span_lint_and_sugg( |
| 79 | + cx, |
| 80 | + MANUAL_STR_REPEAT, |
| 81 | + collect_expr.span, |
| 82 | + "manual implementation of `str::repeat` using iterators", |
| 83 | + "try this", |
| 84 | + format!("{}.repeat({})", val_str, count_snip), |
| 85 | + app |
| 86 | + ) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments