Skip to content

Commit 344888a

Browse files
committed
fix review comments
1 parent f3e01c4 commit 344888a

File tree

5 files changed

+115
-47
lines changed

5 files changed

+115
-47
lines changed

clippy_lints/src/rc_clone_in_vec_init.rs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::higher::VecArgs;
33
use clippy_utils::last_path_segment;
44
use clippy_utils::macros::root_macro_call_first_node;
55
use clippy_utils::source::{indent_of, snippet};
6-
use rustc_errors::{Applicability, Diagnostic};
6+
use rustc_errors::Applicability;
77
use rustc_hir::{Expr, ExprKind, QPath, TyKind};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -51,70 +51,53 @@ impl LateLintPass<'_> for RcCloneInVecInit {
5151
let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return; };
5252
let Some(VecArgs::Repeat(elem, len)) = VecArgs::hir(cx, expr) else { return; };
5353
let Some(symbol) = new_reference_call(cx, elem) else { return; };
54-
let lint_span = macro_call.span;
55-
let symbol_name = symbol.as_str();
56-
let len_snippet = snippet(cx, len.span, "..");
57-
let elem_snippet = elem_snippet(cx, elem, symbol_name);
58-
let indentation = indent_of(cx, lint_span).unwrap_or(0);
59-
let lint_suggestions =
60-
construct_lint_suggestions(lint_span, symbol_name, &elem_snippet, len_snippet.as_ref(), indentation);
61-
62-
emit_lint(cx, symbol, lint_span, &lint_suggestions);
54+
55+
emit_lint(cx, symbol, macro_call.span, elem, len);
6356
}
6457
}
6558

6659
struct LintSuggestion {
67-
span: Span,
6860
message: String,
69-
suggestion: String,
70-
applicability: Applicability,
71-
}
72-
73-
impl LintSuggestion {
74-
fn span_suggestion(&self, diag: &mut Diagnostic) {
75-
diag.span_suggestion(self.span, &self.message, &self.suggestion, self.applicability);
76-
}
61+
snippet: String,
7762
}
7863

7964
fn construct_lint_suggestions(
65+
cx: &LateContext<'_>,
8066
span: Span,
8167
symbol_name: &str,
82-
elem_snippet: &str,
83-
len_snippet: &str,
84-
indentation: usize,
68+
elem: &Expr<'_>,
69+
len: &Expr<'_>,
8570
) -> Vec<LintSuggestion> {
71+
let len_snippet = snippet(cx, len.span, "..");
72+
let elem_snippet = elem_snippet(cx, elem, symbol_name);
73+
let indentation = indent_of(cx, span).unwrap_or(0);
8674
let indentation = " ".repeat(indentation);
87-
let loop_init_suggestion = loop_init_suggestion(elem_snippet, len_snippet, &indentation);
88-
let extract_suggestion = extract_suggestion(elem_snippet, len_snippet, &indentation);
75+
let loop_init_suggestion = loop_init_suggestion(&elem_snippet, len_snippet.as_ref(), &indentation);
76+
let extract_suggestion = extract_suggestion(&elem_snippet, len_snippet.as_ref(), &indentation);
8977

9078
vec![
9179
LintSuggestion {
92-
span,
9380
message: format!("consider initializing each `{symbol_name}` element individually"),
94-
suggestion: loop_init_suggestion,
95-
applicability: Applicability::Unspecified,
81+
snippet: loop_init_suggestion,
9682
},
9783
LintSuggestion {
98-
span,
9984
message: format!(
10085
"or if this is intentional, consider extracting the `{symbol_name}` initialization to a variable"
10186
),
102-
suggestion: extract_suggestion,
103-
applicability: Applicability::Unspecified,
87+
snippet: extract_suggestion,
10488
},
10589
]
10690
}
10791

10892
fn elem_snippet(cx: &LateContext<'_>, elem: &Expr<'_>, symbol_name: &str) -> String {
109-
let mut elem_snippet = snippet(cx, elem.span, "..").to_string();
93+
let elem_snippet = snippet(cx, elem.span, "..").to_string();
11094
if elem_snippet.contains('\n') {
111-
let reference_initialization = format!("{symbol_name}::new");
112-
// This string must be found in `elem_snippet`, otherwise we won't be constructing the snippet in
113-
// the first place.
114-
let reference_initialization_end =
115-
elem_snippet.find(&reference_initialization).unwrap() + reference_initialization.len();
116-
elem_snippet.replace_range(reference_initialization_end.., "..");
95+
let reference_creation = format!("{symbol_name}::new");
96+
let (code_until_reference_creation, _right) = elem_snippet.split_once(&reference_creation).unwrap();
97+
98+
return format!("{code_until_reference_creation}{reference_creation}(..)");
11799
}
100+
118101
elem_snippet
119102
}
120103

@@ -137,7 +120,7 @@ fn extract_suggestion(elem: &str, len: &str, indent: &str) -> String {
137120
)
138121
}
139122

140-
fn emit_lint(cx: &LateContext<'_>, symbol: Symbol, lint_span: Span, lint_suggestions: &[LintSuggestion]) {
123+
fn emit_lint(cx: &LateContext<'_>, symbol: Symbol, lint_span: Span, elem: &Expr<'_>, len: &Expr<'_>) {
141124
let symbol_name = symbol.as_str();
142125

143126
span_lint_and_then(
@@ -146,10 +129,17 @@ fn emit_lint(cx: &LateContext<'_>, symbol: Symbol, lint_span: Span, lint_suggest
146129
lint_span,
147130
&format!("calling `{symbol_name}::new` in `vec![elem; len]`"),
148131
|diag| {
132+
let suggestions = construct_lint_suggestions(cx, lint_span, symbol_name, elem, len);
133+
149134
diag.note(format!("each element will point to the same `{symbol_name}` instance"));
150-
lint_suggestions
151-
.iter()
152-
.for_each(|suggestion| suggestion.span_suggestion(diag));
135+
suggestions.iter().for_each(|suggestion| {
136+
diag.span_suggestion(
137+
lint_span,
138+
&suggestion.message,
139+
&suggestion.snippet,
140+
Applicability::Unspecified,
141+
);
142+
});
153143
},
154144
);
155145
}

tests/ui/rc_clone_in_vec_init/arc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ fn should_warn_complex_case() {
1616
}));
1717
2
1818
];
19+
20+
let v1 = vec![
21+
Arc::new(Mutex::new({
22+
let x = 1;
23+
dbg!(x);
24+
x
25+
}));
26+
2
27+
];
1928
}
2029

2130
fn should_not_warn_custom_arc() {

tests/ui/rc_clone_in_vec_init/arc.stderr

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,47 @@ help: consider initializing each `Arc` element individually
4040
|
4141
LL ~ let v = {
4242
LL + let mut v = Vec::with_capacity(2);
43-
LL + (0..2).for_each(|_| v.push(std::sync::Arc::new..));
43+
LL + (0..2).for_each(|_| v.push(std::sync::Arc::new(..)));
4444
LL + v
4545
LL ~ };
4646
|
4747
help: or if this is intentional, consider extracting the `Arc` initialization to a variable
4848
|
4949
LL ~ let v = {
50-
LL + let data = std::sync::Arc::new..;
50+
LL + let data = std::sync::Arc::new(..);
5151
LL + vec![data; 2]
5252
LL ~ };
5353
|
5454

55-
error: aborting due to 2 previous errors
55+
error: calling `Arc::new` in `vec![elem; len]`
56+
--> $DIR/arc.rs:20:14
57+
|
58+
LL | let v1 = vec![
59+
| ______________^
60+
LL | | Arc::new(Mutex::new({
61+
LL | | let x = 1;
62+
LL | | dbg!(x);
63+
... |
64+
LL | | 2
65+
LL | | ];
66+
| |_____^
67+
|
68+
= note: each element will point to the same `Arc` instance
69+
help: consider initializing each `Arc` element individually
70+
|
71+
LL ~ let v1 = {
72+
LL + let mut v = Vec::with_capacity(2);
73+
LL + (0..2).for_each(|_| v.push(Arc::new(..)));
74+
LL + v
75+
LL ~ };
76+
|
77+
help: or if this is intentional, consider extracting the `Arc` initialization to a variable
78+
|
79+
LL ~ let v1 = {
80+
LL + let data = Arc::new(..);
81+
LL + vec![data; 2]
82+
LL ~ };
83+
|
84+
85+
error: aborting due to 3 previous errors
5686

tests/ui/rc_clone_in_vec_init/rc.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ fn should_warn_complex_case() {
1717
}));
1818
2
1919
];
20+
21+
let v1 = vec![
22+
Rc::new(Mutex::new({
23+
let x = 1;
24+
dbg!(x);
25+
x
26+
}));
27+
2
28+
];
2029
}
2130

2231
fn should_not_warn_custom_arc() {

tests/ui/rc_clone_in_vec_init/rc.stderr

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,47 @@ help: consider initializing each `Rc` element individually
4040
|
4141
LL ~ let v = {
4242
LL + let mut v = Vec::with_capacity(2);
43-
LL + (0..2).for_each(|_| v.push(std::rc::Rc::new..));
43+
LL + (0..2).for_each(|_| v.push(std::rc::Rc::new(..)));
4444
LL + v
4545
LL ~ };
4646
|
4747
help: or if this is intentional, consider extracting the `Rc` initialization to a variable
4848
|
4949
LL ~ let v = {
50-
LL + let data = std::rc::Rc::new..;
50+
LL + let data = std::rc::Rc::new(..);
5151
LL + vec![data; 2]
5252
LL ~ };
5353
|
5454

55-
error: aborting due to 2 previous errors
55+
error: calling `Rc::new` in `vec![elem; len]`
56+
--> $DIR/rc.rs:21:14
57+
|
58+
LL | let v1 = vec![
59+
| ______________^
60+
LL | | Rc::new(Mutex::new({
61+
LL | | let x = 1;
62+
LL | | dbg!(x);
63+
... |
64+
LL | | 2
65+
LL | | ];
66+
| |_____^
67+
|
68+
= note: each element will point to the same `Rc` instance
69+
help: consider initializing each `Rc` element individually
70+
|
71+
LL ~ let v1 = {
72+
LL + let mut v = Vec::with_capacity(2);
73+
LL + (0..2).for_each(|_| v.push(Rc::new(..)));
74+
LL + v
75+
LL ~ };
76+
|
77+
help: or if this is intentional, consider extracting the `Rc` initialization to a variable
78+
|
79+
LL ~ let v1 = {
80+
LL + let data = Rc::new(..);
81+
LL + vec![data; 2]
82+
LL ~ };
83+
|
84+
85+
error: aborting due to 3 previous errors
5686

0 commit comments

Comments
 (0)