@@ -20,14 +20,20 @@ declare_clippy_lint! {
2020 /// ```rust
2121 /// let v = vec![std::sync::Arc::new("some data".to_string()); 100];
2222 /// // or
23- /// let v = vec![std::rc::Rc::new("some data".to_string())];
23+ /// let v = vec![std::rc::Rc::new("some data".to_string()); 100 ];
2424 /// ```
2525 /// Use instead:
2626 /// ```rust
27- /// let data = std::sync::Arc::new("some data".to_string());
28- /// // or
27+ /// // Create the reference beforehand to clarify it that
28+ /// // this reference should be cloned for each value
2929 /// let data = std::rc::Rc::new("some data".to_string());
3030 /// let v = vec![data; 100];
31+ ///
32+ /// // Or initialize each value separately:
33+ /// let mut data = Vec::with_capacity(100);
34+ /// for _ in 0..=100 {
35+ /// data.push(std::rc::Rc::new("some data".to_string()));
36+ /// }
3137 /// ```
3238 #[ clippy:: version = "1.62.0" ]
3339 pub REF_CLONE_IN_VEC_INIT ,
@@ -42,11 +48,11 @@ impl LateLintPass<'_> for RefCloneInVecInit {
4248 let Some ( VecArgs :: Repeat ( elem, _) ) = VecArgs :: hir ( cx, expr) else { return ; } ;
4349 let Some ( symbol) = new_reference_call ( cx, elem) else { return ; } ;
4450
45- yield_lint ( cx, symbol, & macro_call) ;
51+ emit_lint ( cx, symbol, & macro_call) ;
4652 }
4753}
4854
49- fn yield_lint ( cx : & LateContext < ' _ > , symbol : Symbol , macro_call : & MacroCall ) {
55+ fn emit_lint ( cx : & LateContext < ' _ > , symbol : Symbol , macro_call : & MacroCall ) {
5056 let symbol_name = symbol. as_str ( ) ;
5157
5258 span_lint_and_then (
@@ -55,10 +61,11 @@ fn yield_lint(cx: &LateContext<'_>, symbol: Symbol, macro_call: &MacroCall) {
5561 macro_call. span ,
5662 & format ! ( "calling `{symbol_name}::new` in `vec![elem; len]`" ) ,
5763 |diag| {
58- diag. note ( format ! ( "each `{symbol_name}` will point to the same allocation" ) ) ;
59- diag. help ( format ! (
60- "if this is intentional, consider extracting the `{symbol_name}` initialization to a variable"
61- ) ) ;
64+ diag. note ( format ! ( "each element will point to the same `{symbol_name}` instance" ) )
65+ . help ( format ! (
66+ "if this is intentional, consider extracting the `{symbol_name}` initialization to a variable"
67+ ) )
68+ . help ( "or if not, initilaize each element individually" ) ;
6269 } ,
6370 ) ;
6471}
0 commit comments