Skip to content

Commit dc23b5d

Browse files
committed
fix indentation + test
1 parent a791205 commit dc23b5d

File tree

5 files changed

+77
-11
lines changed

5 files changed

+77
-11
lines changed

clippy_lints/src/rc_clone_in_vec_init.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,18 @@ fn elem_snippet(cx: &LateContext<'_>, elem: &Expr<'_>, symbol_name: &str) -> Str
106106
fn loop_init_suggestion(elem: &str, len: &str, indent: &str) -> String {
107107
format!(
108108
r#"{{
109-
{indent}{indent}let mut v = Vec::with_capacity({len});
110-
{indent}{indent}(0..{len}).for_each(|_| v.push({elem}));
111-
{indent}{indent}v
109+
{indent} let mut v = Vec::with_capacity({len});
110+
{indent} (0..{len}).for_each(|_| v.push({elem}));
111+
{indent} v
112112
{indent}}}"#
113113
)
114114
}
115115

116116
fn extract_suggestion(elem: &str, len: &str, indent: &str) -> String {
117117
format!(
118118
"{{
119-
{indent}{indent}let data = {elem};
120-
{indent}{indent}vec![data; {len}]
119+
{indent} let data = {elem};
120+
{indent} vec![data; {len}]
121121
{indent}}}"
122122
)
123123
}

tests/ui/rc_clone_in_vec_init/arc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ fn should_warn_simple_case() {
77
let v = vec![Arc::new("x".to_string()); 2];
88
}
99

10+
fn should_warn_simple_case_with_big_indentation() {
11+
if true {
12+
let k = 1;
13+
dbg!(k);
14+
if true {
15+
let v = vec![Arc::new("x".to_string()); 2];
16+
}
17+
}
18+
}
19+
1020
fn should_warn_complex_case() {
1121
let v = vec![
1222
std::sync::Arc::new(Mutex::new({

tests/ui/rc_clone_in_vec_init/arc.stderr

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,30 @@ LL ~ };
2323
|
2424

2525
error: calling `Arc::new` in `vec![elem; len]`
26-
--> $DIR/arc.rs:11:13
26+
--> $DIR/arc.rs:15:21
27+
|
28+
LL | let v = vec![Arc::new("x".to_string()); 2];
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
= note: each element will point to the same `Arc` instance
32+
help: consider initializing each `Arc` element individually
33+
|
34+
LL ~ let v = {
35+
LL + let mut v = Vec::with_capacity(2);
36+
LL + (0..2).for_each(|_| v.push(Arc::new("x".to_string())));
37+
LL + v
38+
LL ~ };
39+
|
40+
help: or if this is intentional, consider extracting the `Arc` initialization to a variable
41+
|
42+
LL ~ let v = {
43+
LL + let data = Arc::new("x".to_string());
44+
LL + vec![data; 2]
45+
LL ~ };
46+
|
47+
48+
error: calling `Arc::new` in `vec![elem; len]`
49+
--> $DIR/arc.rs:21:13
2750
|
2851
LL | let v = vec![
2952
| _____________^
@@ -53,7 +76,7 @@ LL ~ };
5376
|
5477

5578
error: calling `Arc::new` in `vec![elem; len]`
56-
--> $DIR/arc.rs:20:14
79+
--> $DIR/arc.rs:30:14
5780
|
5881
LL | let v1 = vec![
5982
| ______________^
@@ -82,5 +105,5 @@ LL + vec![data; 2]
82105
LL ~ };
83106
|
84107

85-
error: aborting due to 3 previous errors
108+
error: aborting due to 4 previous errors
86109

tests/ui/rc_clone_in_vec_init/rc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ fn should_warn_simple_case() {
88
let v = vec![Rc::new("x".to_string()); 2];
99
}
1010

11+
fn should_warn_simple_case_with_big_indentation() {
12+
if true {
13+
let k = 1;
14+
dbg!(k);
15+
if true {
16+
let v = vec![Rc::new("x".to_string()); 2];
17+
}
18+
}
19+
}
20+
1121
fn should_warn_complex_case() {
1222
let v = vec![
1323
std::rc::Rc::new(Mutex::new({

tests/ui/rc_clone_in_vec_init/rc.stderr

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,30 @@ LL ~ };
2323
|
2424

2525
error: calling `Rc::new` in `vec![elem; len]`
26-
--> $DIR/rc.rs:12:13
26+
--> $DIR/rc.rs:16:21
27+
|
28+
LL | let v = vec![Rc::new("x".to_string()); 2];
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
= note: each element will point to the same `Rc` instance
32+
help: consider initializing each `Rc` element individually
33+
|
34+
LL ~ let v = {
35+
LL + let mut v = Vec::with_capacity(2);
36+
LL + (0..2).for_each(|_| v.push(Rc::new("x".to_string())));
37+
LL + v
38+
LL ~ };
39+
|
40+
help: or if this is intentional, consider extracting the `Rc` initialization to a variable
41+
|
42+
LL ~ let v = {
43+
LL + let data = Rc::new("x".to_string());
44+
LL + vec![data; 2]
45+
LL ~ };
46+
|
47+
48+
error: calling `Rc::new` in `vec![elem; len]`
49+
--> $DIR/rc.rs:22:13
2750
|
2851
LL | let v = vec![
2952
| _____________^
@@ -53,7 +76,7 @@ LL ~ };
5376
|
5477

5578
error: calling `Rc::new` in `vec![elem; len]`
56-
--> $DIR/rc.rs:21:14
79+
--> $DIR/rc.rs:31:14
5780
|
5881
LL | let v1 = vec![
5982
| ______________^
@@ -82,5 +105,5 @@ LL + vec![data; 2]
82105
LL ~ };
83106
|
84107

85-
error: aborting due to 3 previous errors
108+
error: aborting due to 4 previous errors
86109

0 commit comments

Comments
 (0)