]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/repeat_once.rs
Use Span::from_expansion instead of in_macro
[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::source::snippet;
4 use clippy_utils::ty::is_type_diagnostic_item;
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir::{Expr, ExprKind};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 use rustc_span::sym;
11
12 declare_clippy_lint! {
13     /// ### What it does
14     /// 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?
23     /// For example, `String.repeat(1)` is equivalent to `.clone()`. If cloning
24     /// the string is the intention behind this, `clone()` should be used.
25     ///
26     /// ### Example
27     /// ```rust
28     /// fn main() {
29     ///     let x = String::from("hello world").repeat(1);
30     /// }
31     /// ```
32     /// Use instead:
33     /// ```rust
34     /// fn main() {
35     ///     let x = String::from("hello world").clone();
36     /// }
37     /// ```
38     pub REPEAT_ONCE,
39     complexity,
40     "using `.repeat(1)` instead of `String.clone()`, `str.to_string()` or `slice.to_vec()` "
41 }
42
43 declare_lint_pass!(RepeatOnce => [REPEAT_ONCE]);
44
45 impl<'tcx> LateLintPass<'tcx> for RepeatOnce {
46     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &'tcx Expr<'_>) {
47         if_chain! {
48             if let ExprKind::MethodCall(path, _, [receiver, count], _) = &expr.kind;
49             if path.ident.name == sym!(repeat);
50             if constant_context(cx, cx.typeck_results()).expr(count) == Some(Constant::Int(1));
51             if !receiver.span.from_expansion();
52             then {
53                 let ty = cx.typeck_results().expr_ty(receiver).peel_refs();
54                 if ty.is_str() {
55                     span_lint_and_sugg(
56                         cx,
57                         REPEAT_ONCE,
58                         expr.span,
59                         "calling `repeat(1)` on str",
60                         "consider using `.to_string()` instead",
61                         format!("{}.to_string()", snippet(cx, receiver.span, r#""...""#)),
62                         Applicability::MachineApplicable,
63                     );
64                 } else if ty.builtin_index().is_some() {
65                     span_lint_and_sugg(
66                         cx,
67                         REPEAT_ONCE,
68                         expr.span,
69                         "calling `repeat(1)` on slice",
70                         "consider using `.to_vec()` instead",
71                         format!("{}.to_vec()", snippet(cx, receiver.span, r#""...""#)),
72                         Applicability::MachineApplicable,
73                     );
74                 } else if is_type_diagnostic_item(cx, ty, sym::String) {
75                     span_lint_and_sugg(
76                         cx,
77                         REPEAT_ONCE,
78                         expr.span,
79                         "calling `repeat(1)` on a string literal",
80                         "consider using `.clone()` instead",
81                         format!("{}.clone()", snippet(cx, receiver.span, r#""...""#)),
82                         Applicability::MachineApplicable,
83                     );
84                 }
85             }
86         }
87     }
88 }