]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/rc_clone_in_vec_init.rs
Rollup merge of #97415 - cjgillot:is-late-bound-solo, r=estebank
[rust.git] / clippy_lints / src / rc_clone_in_vec_init.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::higher::VecArgs;
3 use clippy_utils::last_path_segment;
4 use clippy_utils::macros::root_macro_call_first_node;
5 use clippy_utils::source::{indent_of, snippet};
6 use rustc_errors::Applicability;
7 use rustc_hir::{Expr, ExprKind, QPath, TyKind};
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 use rustc_span::{sym, Span, Symbol};
11
12 declare_clippy_lint! {
13     /// ### What it does
14     /// Checks for `Arc::new` or `Rc::new` in `vec![elem; len]`
15     ///
16     /// ### Why is this bad?
17     /// This will create `elem` once and clone it `len` times - doing so with `Arc` or `Rc`
18     /// is a bit misleading, as it will create references to the same pointer, rather
19     /// than different instances.
20     ///
21     /// ### Example
22     /// ```rust
23     /// let v = vec![std::sync::Arc::new("some data".to_string()); 100];
24     /// // or
25     /// let v = vec![std::rc::Rc::new("some data".to_string()); 100];
26     /// ```
27     /// Use instead:
28     /// ```rust
29     ///
30     /// // Initialize each value separately:
31     /// let mut data = Vec::with_capacity(100);
32     /// for _ in 0..100 {
33     ///     data.push(std::rc::Rc::new("some data".to_string()));
34     /// }
35     ///
36     /// // Or if you want clones of the same reference,
37     /// // Create the reference beforehand to clarify that
38     /// // it should be cloned for each value
39     /// let data = std::rc::Rc::new("some data".to_string());
40     /// let v = vec![data; 100];
41     /// ```
42     #[clippy::version = "1.62.0"]
43     pub RC_CLONE_IN_VEC_INIT,
44     suspicious,
45     "initializing `Arc` or `Rc` in `vec![elem; len]`"
46 }
47 declare_lint_pass!(RcCloneInVecInit => [RC_CLONE_IN_VEC_INIT]);
48
49 impl LateLintPass<'_> for RcCloneInVecInit {
50     fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
51         let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return; };
52         let Some(VecArgs::Repeat(elem, len)) = VecArgs::hir(cx, expr) else { return; };
53         let Some(symbol) = new_reference_call(cx, elem) else { return; };
54
55         emit_lint(cx, symbol, macro_call.span, elem, len);
56     }
57 }
58
59 fn elem_snippet(cx: &LateContext<'_>, elem: &Expr<'_>, symbol_name: &str) -> String {
60     let elem_snippet = snippet(cx, elem.span, "..").to_string();
61     if elem_snippet.contains('\n') {
62         // This string must be found in `elem_snippet`, otherwise we won't be constructing
63         // the snippet in the first place.
64         let reference_creation = format!("{symbol_name}::new");
65         let (code_until_reference_creation, _right) = elem_snippet.split_once(&reference_creation).unwrap();
66
67         return format!("{code_until_reference_creation}{reference_creation}(..)");
68     }
69
70     elem_snippet
71 }
72
73 fn loop_init_suggestion(elem: &str, len: &str, indent: &str) -> String {
74     format!(
75         r#"{{
76 {indent}    let mut v = Vec::with_capacity({len});
77 {indent}    (0..{len}).for_each(|_| v.push({elem}));
78 {indent}    v
79 {indent}}}"#
80     )
81 }
82
83 fn extract_suggestion(elem: &str, len: &str, indent: &str) -> String {
84     format!(
85         "{{
86 {indent}    let data = {elem};
87 {indent}    vec![data; {len}]
88 {indent}}}"
89     )
90 }
91
92 fn emit_lint(cx: &LateContext<'_>, symbol: Symbol, lint_span: Span, elem: &Expr<'_>, len: &Expr<'_>) {
93     let symbol_name = symbol.as_str();
94
95     span_lint_and_then(
96         cx,
97         RC_CLONE_IN_VEC_INIT,
98         lint_span,
99         &format!("calling `{symbol_name}::new` in `vec![elem; len]`"),
100         |diag| {
101             let len_snippet = snippet(cx, len.span, "..");
102             let elem_snippet = elem_snippet(cx, elem, symbol_name);
103             let indentation = " ".repeat(indent_of(cx, lint_span).unwrap_or(0));
104             let loop_init_suggestion = loop_init_suggestion(&elem_snippet, len_snippet.as_ref(), &indentation);
105             let extract_suggestion = extract_suggestion(&elem_snippet, len_snippet.as_ref(), &indentation);
106
107             diag.note(format!("each element will point to the same `{symbol_name}` instance"));
108             diag.span_suggestion(
109                 lint_span,
110                 format!("consider initializing each `{symbol_name}` element individually"),
111                 loop_init_suggestion,
112                 Applicability::Unspecified,
113             );
114             diag.span_suggestion(
115                 lint_span,
116                 format!(
117                     "or if this is intentional, consider extracting the `{symbol_name}` initialization to a variable"
118                 ),
119                 extract_suggestion,
120                 Applicability::Unspecified,
121             );
122         },
123     );
124 }
125
126 /// Checks whether the given `expr` is a call to `Arc::new` or `Rc::new`
127 fn new_reference_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<Symbol> {
128     if_chain! {
129         if let ExprKind::Call(func, _args) = expr.kind;
130         if let ExprKind::Path(ref func_path @ QPath::TypeRelative(ty, _)) = func.kind;
131         if let TyKind::Path(ref ty_path) = ty.kind;
132         if let Some(def_id) = cx.qpath_res(ty_path, ty.hir_id).opt_def_id();
133         if last_path_segment(func_path).ident.name == sym::new;
134
135         then {
136             return cx.tcx.get_diagnostic_name(def_id).filter(|symbol| symbol == &sym::Arc || symbol == &sym::Rc);
137         }
138     }
139
140     None
141 }