]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/repeat_once.rs
Auto merge of #7484 - camsteffen:author, r=flip1995
[rust.git] / clippy_lints / src / repeat_once.rs
1 use clippy_utils::consts::{constant_context, Constant};
2 use clippy_utils::diagnostics::span_lint_and_sugg;
3 use clippy_utils::in_macro;
4 use clippy_utils::source::snippet;
5 use clippy_utils::ty::is_type_diagnostic_item;
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
8 use rustc_hir::{Expr, ExprKind};
9 use rustc_lint::{LateContext, LateLintPass};
10 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 use rustc_span::sym;
12
13 declare_clippy_lint! {
14     /// **What it does:** Checks for usage of `.repeat(1)` and suggest the following method for each types.
15     /// - `.to_string()` for `str`
16     /// - `.clone()` for `String`
17     /// - `.to_vec()` for `slice`
18     ///
19     /// The lint will evaluate constant expressions and values as arguments of `.repeat(..)` and emit a message if
20     /// they are equivalent to `1`. (Related discussion in [rust-clippy#7306](https://github.com/rust-lang/rust-clippy/issues/7306))
21     ///
22     /// **Why is this bad?** For example, `String.repeat(1)` is equivalent to `.clone()`. If cloning
23     /// the string is the intention behind this, `clone()` should be used.
24     ///
25     /// **Known problems:** None.
26     ///
27     /// **Example:**
28     ///
29     /// ```rust
30     /// fn main() {
31     ///     let x = String::from("hello world").repeat(1);
32     /// }
33     /// ```
34     /// Use instead:
35     /// ```rust
36     /// fn main() {
37     ///     let x = String::from("hello world").clone();
38     /// }
39     /// ```
40     pub REPEAT_ONCE,
41     complexity,
42     "using `.repeat(1)` instead of `String.clone()`, `str.to_string()` or `slice.to_vec()` "
43 }
44
45 declare_lint_pass!(RepeatOnce => [REPEAT_ONCE]);
46
47 impl<'tcx> LateLintPass<'tcx> for RepeatOnce {
48     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
49         if_chain! {
50             if let ExprKind::MethodCall(path, _, [receiver, count], _) = &expr.kind;
51             if path.ident.name == sym!(repeat);
52             if let Some(Constant::Int(1)) = constant_context(cx, cx.typeck_results()).expr(count);
53             if !in_macro(receiver.span);
54             then {
55                 let ty = cx.typeck_results().expr_ty(receiver).peel_refs();
56                 if ty.is_str() {
57                     span_lint_and_sugg(
58                         cx,
59                         REPEAT_ONCE,
60                         expr.span,
61                         "calling `repeat(1)` on str",
62                         "consider using `.to_string()` instead",
63                         format!("{}.to_string()", snippet(cx, receiver.span, r#""...""#)),
64                         Applicability::MachineApplicable,
65                     );
66                 } else if ty.builtin_index().is_some() {
67                     span_lint_and_sugg(
68                         cx,
69                         REPEAT_ONCE,
70                         expr.span,
71                         "calling `repeat(1)` on slice",
72                         "consider using `.to_vec()` instead",
73                         format!("{}.to_vec()", snippet(cx, receiver.span, r#""...""#)),
74                         Applicability::MachineApplicable,
75                     );
76                 } else if is_type_diagnostic_item(cx, ty, sym::string_type) {
77                     span_lint_and_sugg(
78                         cx,
79                         REPEAT_ONCE,
80                         expr.span,
81                         "calling `repeat(1)` on a string literal",
82                         "consider using `.clone()` instead",
83                         format!("{}.clone()", snippet(cx, receiver.span, r#""...""#)),
84                         Applicability::MachineApplicable,
85                     );
86                 }
87             }
88         }
89     }
90 }