Email me

Clippy lints / Suspicious

Clippy lint · Suspicious · checked with Clippy 0.1.99

clippy::rc_clone_in_vec_init: initializing a reference-counted pointer in `vec![elem; len]`

What it meansChecks for reference-counted pointers (Arc, Rc, rc::Weak, and sync::Weak) in vec![elem; len] It is in the suspicious group and warns by default.

What you see

warning: initializing a reference-counted pointer in `vec![elem; len]`
 --> main.rs:3:9
  |
3 | let v = vec![std::sync::Arc::new("some data".to_string()); 100];
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: each element will point to the same `Arc` instance
  = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#rc_clone_in_vec_init
  = note: `#[warn(clippy::rc_clone_in_vec_init)]` on by default
help: consider initializing each `Arc` element individually
  |
3 ~ let v = {
4 +     let mut v = Vec::with_capacity(100);
5 +     (0..100).for_each(|_| v.push(std::sync::Arc::new(..)));
6 +     v
7 ~ };
  |
help: or if this is intentional, consider extracting the `Arc` initialization to a variable
  |
3 ~ let v = {
4 +     let data = std::sync::Arc::new(..);
5 +     vec![data; 100]
6 ~ };
  |

Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.

Does it matter?

This will create elem once and clone it len times - doing so with Arc/Rc/Weak is a bit misleading, as it will create references to the same pointer, rather than different instances.

Code that is most likely wrong or useless. Warns by default.

How to fix it

Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:

Flagged

let v = vec![std::sync::Arc::new("some data".to_string()); 100];
// or
let v = vec![std::rc::Rc::new("some data".to_string()); 100];

Write this instead

// Initialize each value separately:
let mut data = Vec::with_capacity(100);
for _ in 0..100 {
    data.push(std::rc::Rc::new("some data".to_string()));
}

// Or if you want clones of the same reference,
// Create the reference beforehand to clarify that
// it should be cloned for each value
let data = std::rc::Rc::new("some data".to_string());
let v = vec![data; 100];

Turn it off, or on

Allow it where it does not fit your code, as narrowly as you can:

On one item

#[allow(clippy::rc_clone_in_vec_init)]
fn my_function() { /* ... */ }

Whole crate (Cargo.toml)

[lints.clippy]
rc_clone_in_vec_init = "allow"

One run

cargo clippy -- -A clippy::rc-clone-in-vec-init

The [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.

Suspiciousgroup
warndefault level
nofixed by cargo clippy --fix
1.63.0added in Rust

Clippy's own documentation

What it does

Checks for reference-counted pointers (Arc, Rc, rc::Weak, and sync::Weak) in vec![elem; len]

Why is this bad?

This will create elem once and clone it len times - doing so with Arc/Rc/Weak is a bit misleading, as it will create references to the same pointer, rather than different instances.

Example

let v = vec![std::sync::Arc::new("some data".to_string()); 100];
// or
let v = vec![std::rc::Rc::new("some data".to_string()); 100];

Use instead:

// Initialize each value separately:
let mut data = Vec::with_capacity(100);
for _ in 0..100 {
    data.push(std::rc::Rc::new("some data".to_string()));
}

// Or if you want clones of the same reference,
// Create the reference beforehand to clarify that
// it should be cloned for each value
let data = std::rc::Rc::new("some data".to_string());
let v = vec![data; 100];

From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.

Related lints

All 847 Clippy lints · Rust compiler errors · Cargo.toml checker

Up next

Related lintclippy::pointers_in_nomem_asm_blockpassing pointers to nomem asm blockRead it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ