introduce rc_clone_in_vec_init lint

This commit is contained in:
yonip23 2022-05-08 22:20:25 +03:00
parent 0509a96290
commit feb6d8cf30
10 changed files with 255 additions and 0 deletions

View file

@ -0,0 +1,49 @@
#![warn(clippy::rc_clone_in_vec_init)]
use std::sync::{Arc, Mutex};
fn main() {}
fn should_warn_simple_case() {
let v = vec![Arc::new("x".to_string()); 2];
}
fn should_warn_complex_case() {
let v = vec![
std::sync::Arc::new(Mutex::new({
let x = 1;
dbg!(x);
x
}));
2
];
}
fn should_not_warn_custom_arc() {
#[derive(Clone)]
struct Arc;
impl Arc {
fn new() -> Self {
Arc
}
}
let v = vec![Arc::new(); 2];
}
fn should_not_warn_vec_from_elem_but_not_arc() {
let v = vec![String::new(); 2];
let v1 = vec![1; 2];
let v2 = vec![
Box::new(std::sync::Arc::new({
let y = 3;
dbg!(y);
y
}));
2
];
}
fn should_not_warn_vec_macro_but_not_from_elem() {
let v = vec![Arc::new("x".to_string())];
}

View file

@ -0,0 +1,30 @@
error: calling `Arc::new` in `vec![elem; len]`
--> $DIR/arc.rs:7:13
|
LL | let v = vec![Arc::new("x".to_string()); 2];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::rc-clone-in-vec-init` implied by `-D warnings`
= note: each element will point to the same `Arc` instance
= help: if this is intentional, consider extracting the `Arc` initialization to a variable
= help: or if not, initialize each element individually
error: calling `Arc::new` in `vec![elem; len]`
--> $DIR/arc.rs:11:13
|
LL | let v = vec![
| _____________^
LL | | std::sync::Arc::new(Mutex::new({
LL | | let x = 1;
LL | | dbg!(x);
... |
LL | | 2
LL | | ];
| |_____^
|
= note: each element will point to the same `Arc` instance
= help: if this is intentional, consider extracting the `Arc` initialization to a variable
= help: or if not, initialize each element individually
error: aborting due to 2 previous errors

View file

@ -0,0 +1,50 @@
#![warn(clippy::rc_clone_in_vec_init)]
use std::rc::Rc;
use std::sync::Mutex;
fn main() {}
fn should_warn_simple_case() {
let v = vec![Rc::new("x".to_string()); 2];
}
fn should_warn_complex_case() {
let v = vec![
std::rc::Rc::new(Mutex::new({
let x = 1;
dbg!(x);
x
}));
2
];
}
fn should_not_warn_custom_arc() {
#[derive(Clone)]
struct Rc;
impl Rc {
fn new() -> Self {
Rc
}
}
let v = vec![Rc::new(); 2];
}
fn should_not_warn_vec_from_elem_but_not_rc() {
let v = vec![String::new(); 2];
let v1 = vec![1; 2];
let v2 = vec![
Box::new(std::rc::Rc::new({
let y = 3;
dbg!(y);
y
}));
2
];
}
fn should_not_warn_vec_macro_but_not_from_elem() {
let v = vec![Rc::new("x".to_string())];
}

View file

@ -0,0 +1,30 @@
error: calling `Rc::new` in `vec![elem; len]`
--> $DIR/rc.rs:8:13
|
LL | let v = vec![Rc::new("x".to_string()); 2];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::rc-clone-in-vec-init` implied by `-D warnings`
= note: each element will point to the same `Rc` instance
= help: if this is intentional, consider extracting the `Rc` initialization to a variable
= help: or if not, initialize each element individually
error: calling `Rc::new` in `vec![elem; len]`
--> $DIR/rc.rs:12:13
|
LL | let v = vec![
| _____________^
LL | | std::rc::Rc::new(Mutex::new({
LL | | let x = 1;
LL | | dbg!(x);
... |
LL | | 2
LL | | ];
| |_____^
|
= note: each element will point to the same `Rc` instance
= help: if this is intentional, consider extracting the `Rc` initialization to a variable
= help: or if not, initialize each element individually
error: aborting due to 2 previous errors