1 use crate::consts::{constant_context, Constant};
2 use crate::utils::{in_macro, is_type_diagnostic_item, snippet, span_lint_and_sugg};
3 use if_chain::if_chain;
4 use rustc_errors::Applicability;
5 use rustc_hir::{Expr, ExprKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 /// **What it does:** Checks for usage of `.repeat(1)` and suggest the following method for each types.
11 /// - `.to_string()` for `str`
12 /// - `.clone()` for `String`
13 /// - `.to_vec()` for `slice`
15 /// **Why is this bad?** For example, `String.repeat(1)` is equivalent to `.clone()`. If cloning the string is the intention behind this, `clone()` should be used.
17 /// **Known problems:** None.
23 /// let x = String::from("hello world").repeat(1);
29 /// let x = String::from("hello world").clone();
34 "using `.repeat(1)` instead of `String.clone()`, `str.to_string()` or `slice.to_vec()` "
37 declare_lint_pass!(RepeatOnce => [REPEAT_ONCE]);
39 impl<'tcx> LateLintPass<'tcx> for RepeatOnce {
40 fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
42 if let ExprKind::MethodCall(path, _, [receiver, count], _) = &expr.kind;
43 if path.ident.name == sym!(repeat);
44 if let Some(Constant::Int(1)) = constant_context(cx, cx.typeck_results()).expr(&count);
45 if !in_macro(receiver.span);
47 let ty = cx.typeck_results().expr_ty(&receiver).peel_refs();
53 "calling `repeat(1)` on str",
54 "consider using `.to_string()` instead",
55 format!("{}.to_string()", snippet(cx, receiver.span, r#""...""#)),
56 Applicability::MachineApplicable,
58 } else if ty.builtin_index().is_some() {
63 "calling `repeat(1)` on slice",
64 "consider using `.to_vec()` instead",
65 format!("{}.to_vec()", snippet(cx, receiver.span, r#""...""#)),
66 Applicability::MachineApplicable,
68 } else if is_type_diagnostic_item(cx, ty, sym!(string_type)) {
73 "calling `repeat(1)` on a string literal",
74 "consider using `.clone()` instead",
75 format!("{}.clone()", snippet(cx, receiver.span, r#""...""#)),
76 Applicability::MachineApplicable,