Skip to content

Commit 435b101

Browse files
committed
fix review comments
1 parent 4019c7e commit 435b101

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

clippy_lints/src/ref_clone_in_vec_init.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/ui/ref_clone_in_vec_init/arc/complex_case.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ LL | | ];
1212
| |_____^
1313
|
1414
= note: `-D clippy::ref-clone-in-vec-init` implied by `-D warnings`
15-
= note: each `Arc` will point to the same allocation
15+
= note: each element will point to the same `Arc` instance
1616
= help: if this is intentional, consider extracting the `Arc` initialization to a variable
17+
= help: or if not, initilaize each element individually
1718

1819
error: aborting due to previous error
1920

tests/ui/ref_clone_in_vec_init/arc/simple_case.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ LL | let v = vec![Arc::new("x".to_string()); 2];
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::ref-clone-in-vec-init` implied by `-D warnings`
8-
= note: each `Arc` will point to the same allocation
8+
= note: each element will point to the same `Arc` instance
99
= help: if this is intentional, consider extracting the `Arc` initialization to a variable
10+
= help: or if not, initilaize each element individually
1011

1112
error: aborting due to previous error
1213

tests/ui/ref_clone_in_vec_init/rc/complex_case.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ LL | | ];
1212
| |_____^
1313
|
1414
= note: `-D clippy::ref-clone-in-vec-init` implied by `-D warnings`
15-
= note: each `Rc` will point to the same allocation
15+
= note: each element will point to the same `Rc` instance
1616
= help: if this is intentional, consider extracting the `Rc` initialization to a variable
17+
= help: or if not, initilaize each element individually
1718

1819
error: aborting due to previous error
1920

tests/ui/ref_clone_in_vec_init/rc/simple_case.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ LL | let v = vec![Rc::new("x".to_string()); 2];
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::ref-clone-in-vec-init` implied by `-D warnings`
8-
= note: each `Rc` will point to the same allocation
8+
= note: each element will point to the same `Rc` instance
99
= help: if this is intentional, consider extracting the `Rc` initialization to a variable
10+
= help: or if not, initilaize each element individually
1011

1112
error: aborting due to previous error
1213

0 commit comments

Comments
 (0)