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