Skip to content

Commit 5b77ee9

Browse files
committed
Rename some symbols
Renamed some symbols in order to make them a little bit more accurate.
1 parent 2753f1c commit 5b77ee9

File tree

2 files changed

+76
-73
lines changed

2 files changed

+76
-73
lines changed

clippy_lints/src/slow_vector_initialization.rs

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
// except according to those terms.
99

1010
use crate::rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
11-
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
11+
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass, Lint};
1212
use crate::rustc::{declare_tool_lint, lint_array};
1313
use crate::rustc::hir::*;
1414
use if_chain::if_chain;
1515
use crate::syntax_pos::symbol::Symbol;
1616
use crate::syntax::ast::{LitKind, NodeId};
17-
use crate::syntax::source_map::Span;
18-
use crate::utils::{match_qpath, span_lint_and_then, SpanlessEq};
19-
use crate::utils::get_enclosing_block;
17+
use crate::utils::{match_qpath, span_lint_and_then, SpanlessEq, get_enclosing_block};
18+
use crate::utils::sugg::Sugg;
2019
use crate::rustc_errors::{Applicability};
2120

2221
/// **What it does:** Checks slow zero-filled vector initialization
@@ -70,15 +69,15 @@ impl LintPass for Pass {
7069
}
7170
}
7271

73-
/// VecInitialization contains data regarding a vector initialized with `with_capacity` and then
72+
/// `VecAllocation` contains data regarding a vector allocated with `with_capacity` and then
7473
/// assigned to a variable. For example, `let mut vec = Vec::with_capacity(0)` or
7574
/// `vec = Vec::with_capacity(0)`
76-
struct VecInitialization<'tcx> {
75+
struct VecAllocation<'tcx> {
7776
/// Symbol of the local variable name
7877
variable_name: Symbol,
7978

80-
/// Reference to the expression which initializes the vector
81-
initialization_expr: &'tcx Expr,
79+
/// Reference to the expression which allocates the vector
80+
allocation_expr: &'tcx Expr,
8281

8382
/// Reference to the expression used as argument on `with_capacity` call. This is used
8483
/// to only match slow zero-filling idioms of the same length than vector initialization.
@@ -111,13 +110,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
111110
if let Some(ref len_arg) = Pass::is_vec_with_capacity(right);
112111

113112
then {
114-
let vi = VecInitialization {
113+
let vi = VecAllocation {
115114
variable_name: variable_name.ident.name,
116-
initialization_expr: right,
115+
allocation_expr: right,
117116
len_expr: len_arg,
118117
};
119118

120-
Pass::search_slow_initialization(cx, vi, expr.id);
119+
Pass::search_initialization(cx, vi, expr.id);
121120
}
122121
}
123122
}
@@ -132,13 +131,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
132131
if let Some(ref len_arg) = Pass::is_vec_with_capacity(init);
133132

134133
then {
135-
let vi = VecInitialization {
134+
let vi = VecAllocation {
136135
variable_name: variable_name.name,
137-
initialization_expr: init,
136+
allocation_expr: init,
138137
len_expr: len_arg,
139138
};
140139

141-
Pass::search_slow_initialization(cx, vi, stmt.node.id());
140+
Pass::search_initialization(cx, vi, stmt.node.id());
142141
}
143142
}
144143
}
@@ -162,10 +161,10 @@ impl Pass {
162161
None
163162
}
164163

165-
/// Search a slow initialization for the given vector
166-
fn search_slow_initialization<'tcx>(
164+
/// Search initialization for the given vector
165+
fn search_initialization<'tcx>(
167166
cx: &LateContext<'_, 'tcx>,
168-
vec_initialization: VecInitialization<'tcx>,
167+
vec_alloc: VecAllocation<'tcx>,
169168
parent_node: NodeId
170169
) {
171170
let enclosing_body = get_enclosing_block(cx, parent_node);
@@ -174,72 +173,76 @@ impl Pass {
174173
return;
175174
}
176175

177-
let mut v = SlowInitializationVisitor {
176+
let mut v = VectorInitializationVisitor {
178177
cx,
179-
vec_ini: vec_initialization,
178+
vec_alloc,
180179
slow_expression: None,
181180
initialization_found: false,
182181
};
183182

184183
v.visit_block(enclosing_body.unwrap());
185184

186-
if let Some(ref initialization_expr) = v.slow_expression {
187-
let alloc_span = v.vec_ini.initialization_expr.span;
188-
Pass::lint_initialization(cx, initialization_expr, alloc_span);
185+
if let Some(ref allocation_expr) = v.slow_expression {
186+
Pass::lint_initialization(cx, allocation_expr, &v.vec_alloc);
189187
}
190188
}
191189

192-
fn lint_initialization<'tcx>(cx: &LateContext<'_, 'tcx>, initialization: &InitializationType<'tcx>, alloc_span: Span) {
190+
fn lint_initialization<'tcx>(cx: &LateContext<'_, 'tcx>, initialization: &InitializationType<'tcx>, vec_alloc: &VecAllocation<'_>) {
193191
match initialization {
194192
InitializationType::UnsafeSetLen(e) =>
195-
Pass::lint_unsafe_initialization(cx, e, alloc_span),
193+
Pass::emit_lint(
194+
cx,
195+
e,
196+
vec_alloc,
197+
"unsafe vector initialization",
198+
UNSAFE_VECTOR_INITIALIZATION
199+
),
196200

197201
InitializationType::Extend(e) |
198202
InitializationType::Resize(e) =>
199-
Pass::lint_slow_initialization(cx, e, alloc_span),
203+
Pass::emit_lint(
204+
cx,
205+
e,
206+
vec_alloc,
207+
"slow zero-filling initialization",
208+
SLOW_VECTOR_INITIALIZATION
209+
)
200210
};
201211
}
202212

203-
fn lint_slow_initialization<'tcx>(
213+
fn emit_lint<'tcx>(
204214
cx: &LateContext<'_, 'tcx>,
205215
slow_fill: &Expr,
206-
alloc_span: Span,
216+
vec_alloc: &VecAllocation<'_>,
217+
msg: &str,
218+
lint: &'static Lint
207219
) {
208-
span_lint_and_then(
209-
cx,
210-
SLOW_VECTOR_INITIALIZATION,
211-
slow_fill.span,
212-
"detected slow zero-filling initialization",
213-
|db| {
214-
db.span_suggestion_with_applicability(alloc_span, "consider replacing with", "vec![0; ..]".to_string(), Applicability::Unspecified);
215-
}
216-
);
217-
}
220+
let len_expr = Sugg::hir(cx, vec_alloc.len_expr, "len");
218221

219-
fn lint_unsafe_initialization<'tcx>(
220-
cx: &LateContext<'_, 'tcx>,
221-
slow_fill: &Expr,
222-
alloc_span: Span,
223-
) {
224222
span_lint_and_then(
225223
cx,
226-
UNSAFE_VECTOR_INITIALIZATION,
224+
lint,
227225
slow_fill.span,
228-
"detected unsafe vector initialization",
226+
msg,
229227
|db| {
230-
db.span_suggestion_with_applicability(alloc_span, "consider replacing with", "vec![0; ..]".to_string(), Applicability::Unspecified);
228+
db.span_suggestion_with_applicability(
229+
vec_alloc.allocation_expr.span,
230+
"consider replace allocation with",
231+
format!("vec![0; {}]", len_expr),
232+
Applicability::Unspecified
233+
);
231234
}
232235
);
233236
}
234237
}
235238

236-
/// SlowInitializationVisitor searches for slow zero filling vector initialization, for the given
239+
/// `VectorInitializationVisitor` searches for unsafe or slow vector initializations for the given
237240
/// vector.
238-
struct SlowInitializationVisitor<'a, 'tcx: 'a> {
241+
struct VectorInitializationVisitor<'a, 'tcx: 'a> {
239242
cx: &'a LateContext<'a, 'tcx>,
240243

241244
/// Contains the information
242-
vec_ini: VecInitialization<'tcx>,
245+
vec_alloc: VecAllocation<'tcx>,
243246

244247
/// Contains, if found, the slow initialization expression
245248
slow_expression: Option<InitializationType<'tcx>>,
@@ -248,14 +251,14 @@ struct SlowInitializationVisitor<'a, 'tcx: 'a> {
248251
initialization_found: bool,
249252
}
250253

251-
impl<'a, 'tcx> SlowInitializationVisitor<'a, 'tcx> {
254+
impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
252255
/// Checks if the given expression is extending a vector with `repeat(0).take(..)`
253256
fn search_slow_extend_filling(&mut self, expr: &'tcx Expr) {
254257
if_chain! {
255258
if self.initialization_found;
256259
if let ExprKind::MethodCall(ref path, _, ref args) = expr.node;
257260
if let ExprKind::Path(ref qpath_subj) = args[0].node;
258-
if match_qpath(&qpath_subj, &[&self.vec_ini.variable_name.to_string()]);
261+
if match_qpath(&qpath_subj, &[&self.vec_alloc.variable_name.to_string()]);
259262
if path.ident.name == "extend";
260263
if let Some(ref extend_arg) = args.get(1);
261264
if self.is_repeat_take(extend_arg);
@@ -272,7 +275,7 @@ impl<'a, 'tcx> SlowInitializationVisitor<'a, 'tcx> {
272275
if self.initialization_found;
273276
if let ExprKind::MethodCall(ref path, _, ref args) = expr.node;
274277
if let ExprKind::Path(ref qpath_subj) = args[0].node;
275-
if match_qpath(&qpath_subj, &[&self.vec_ini.variable_name.to_string()]);
278+
if match_qpath(&qpath_subj, &[&self.vec_alloc.variable_name.to_string()]);
276279
if path.ident.name == "resize";
277280
if let (Some(ref len_arg), Some(fill_arg)) = (args.get(1), args.get(2));
278281

@@ -281,7 +284,7 @@ impl<'a, 'tcx> SlowInitializationVisitor<'a, 'tcx> {
281284
if let LitKind::Int(0, _) = lit.node;
282285

283286
// Check that len expression is equals to `with_capacity` expression
284-
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_ini.len_expr);
287+
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr);
285288

286289
then {
287290
self.slow_expression = Some(InitializationType::Resize(expr));
@@ -295,12 +298,12 @@ impl<'a, 'tcx> SlowInitializationVisitor<'a, 'tcx> {
295298
if self.initialization_found;
296299
if let ExprKind::MethodCall(ref path, _, ref args) = expr.node;
297300
if let ExprKind::Path(ref qpath_subj) = args[0].node;
298-
if match_qpath(&qpath_subj, &[&self.vec_ini.variable_name.to_string()]);
301+
if match_qpath(&qpath_subj, &[&self.vec_alloc.variable_name.to_string()]);
299302
if path.ident.name == "set_len";
300303
if let Some(ref len_arg) = args.get(1);
301304

302305
// Check that len expression is equals to `with_capacity` expression
303-
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_ini.len_expr);
306+
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr);
304307

305308
then {
306309
self.slow_expression = Some(InitializationType::UnsafeSetLen(expr));
@@ -320,7 +323,7 @@ impl<'a, 'tcx> SlowInitializationVisitor<'a, 'tcx> {
320323

321324
// Check that len expression is equals to `with_capacity` expression
322325
if let Some(ref len_arg) = take_args.get(1);
323-
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_ini.len_expr);
326+
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr);
324327

325328
then {
326329
return true;
@@ -349,15 +352,15 @@ impl<'a, 'tcx> SlowInitializationVisitor<'a, 'tcx> {
349352
}
350353
}
351354

352-
impl<'a, 'tcx> Visitor<'tcx> for SlowInitializationVisitor<'a, 'tcx> {
355+
impl<'a, 'tcx> Visitor<'tcx> for VectorInitializationVisitor<'a, 'tcx> {
353356
fn visit_expr(&mut self, expr: &'tcx Expr) {
354357
// Stop the search if we already found a slow zero-filling initialization
355358
if self.slow_expression.is_some() {
356359
return
357360
}
358361

359362
// Skip all the expressions previous to the vector initialization
360-
if self.vec_ini.initialization_expr.id == expr.id {
363+
if self.vec_alloc.allocation_expr.id == expr.id {
361364
self.initialization_found = true;
362365
}
363366

tests/ui/slow_vector_initialization.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
1-
error: detected slow zero-filling initialization
1+
error: slow zero-filling initialization
22
--> $DIR/slow_vector_initialization.rs:23:5
33
|
44
22 | let mut vec1 = Vec::with_capacity(len);
5-
| ----------------------- help: consider replacing with: `vec![0; ..]`
5+
| ----------------------- help: consider replace allocation with: `vec![0; len]`
66
23 | vec1.extend(repeat(0).take(len));
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88
|
99
= note: `-D clippy::slow-vector-initialization` implied by `-D warnings`
1010

11-
error: detected slow zero-filling initialization
11+
error: slow zero-filling initialization
1212
--> $DIR/slow_vector_initialization.rs:27:5
1313
|
1414
26 | let mut vec2 = Vec::with_capacity(len - 10);
15-
| ---------------------------- help: consider replacing with: `vec![0; ..]`
15+
| ---------------------------- help: consider replace allocation with: `vec![0; len - 10]`
1616
27 | vec2.extend(repeat(0).take(len - 10));
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error: detected slow zero-filling initialization
19+
error: slow zero-filling initialization
2020
--> $DIR/slow_vector_initialization.rs:42:5
2121
|
2222
39 | let mut resized_vec = Vec::with_capacity(30);
23-
| ---------------------- help: consider replacing with: `vec![0; ..]`
23+
| ---------------------- help: consider replace allocation with: `vec![0; 30]`
2424
...
2525
42 | resized_vec.resize(30, 0);
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^
2727

28-
error: detected slow zero-filling initialization
28+
error: slow zero-filling initialization
2929
--> $DIR/slow_vector_initialization.rs:44:5
3030
|
3131
40 | let mut extend_vec = Vec::with_capacity(30);
32-
| ---------------------- help: consider replacing with: `vec![0; ..]`
32+
| ---------------------- help: consider replace allocation with: `vec![0; 30]`
3333
...
3434
44 | extend_vec.extend(repeat(0).take(30));
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3636

37-
error: detected slow zero-filling initialization
37+
error: slow zero-filling initialization
3838
--> $DIR/slow_vector_initialization.rs:51:5
3939
|
4040
50 | let mut vec1 = Vec::with_capacity(len);
41-
| ----------------------- help: consider replacing with: `vec![0; ..]`
41+
| ----------------------- help: consider replace allocation with: `vec![0; len]`
4242
51 | vec1.resize(len, 0);
4343
| ^^^^^^^^^^^^^^^^^^^
4444

45-
error: detected slow zero-filling initialization
45+
error: slow zero-filling initialization
4646
--> $DIR/slow_vector_initialization.rs:59:5
4747
|
4848
58 | let mut vec3 = Vec::with_capacity(len - 10);
49-
| ---------------------------- help: consider replacing with: `vec![0; ..]`
49+
| ---------------------------- help: consider replace allocation with: `vec![0; len - 10]`
5050
59 | vec3.resize(len - 10, 0);
5151
| ^^^^^^^^^^^^^^^^^^^^^^^^
5252

53-
error: detected slow zero-filling initialization
53+
error: slow zero-filling initialization
5454
--> $DIR/slow_vector_initialization.rs:63:5
5555
|
5656
62 | vec1 = Vec::with_capacity(10);
57-
| ---------------------- help: consider replacing with: `vec![0; ..]`
57+
| ---------------------- help: consider replace allocation with: `vec![0; 10]`
5858
63 | vec1.resize(10, 0);
5959
| ^^^^^^^^^^^^^^^^^^
6060

61-
error: detected unsafe vector initialization
61+
error: unsafe vector initialization
6262
--> $DIR/slow_vector_initialization.rs:70:9
6363
|
6464
67 | let mut unsafe_vec: Vec<u8> = Vec::with_capacity(200);
65-
| ----------------------- help: consider replacing with: `vec![0; ..]`
65+
| ----------------------- help: consider replace allocation with: `vec![0; 200]`
6666
...
6767
70 | unsafe_vec.set_len(200);
6868
| ^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)