Skip to content

Commit df8bb47

Browse files
committed
Improved snippets and added tests
1 parent b0bd621 commit df8bb47

File tree

4 files changed

+122
-38
lines changed

4 files changed

+122
-38
lines changed

clippy_lints/src/reserve_after_initialization.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::higher::{get_vec_init_kind, VecInitKind};
33
use clippy_utils::path_to_local_id;
44
use clippy_utils::source::snippet;
5-
//use rustc_ast::LitKind;
65
use rustc_errors::Applicability;
76
use rustc_hir::def::Res;
87
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, Local, PatKind, QPath, Stmt, StmtKind};
@@ -16,7 +15,7 @@ declare_clippy_lint! {
1615
/// Informs the user about a more concise way to create a vector with a known capacity.
1716
///
1817
/// ### Why is this bad?
19-
/// The `Vec::with_capacity` constructor is easier to understand.
18+
/// The `Vec::with_capacity` constructor is less complex.
2019
///
2120
/// ### Example
2221
/// ```rust
@@ -51,14 +50,14 @@ impl VecReserveSearcher {
5150
return;
5251
}
5352

54-
let s = format!("{} = Vec::with_capacity({});", self.init_part, self.space_hint);
53+
let s = format!("{}Vec::with_capacity({});", self.init_part, self.space_hint);
5554

5655
span_lint_and_sugg(
5756
cx,
5857
RESERVE_AFTER_INITIALIZATION,
5958
self.err_span,
60-
"calls to `reverse` immediately after creation",
61-
"consider using `Vec::with_capacity(space_hint)`",
59+
"call to `reserve` immediately after creation",
60+
"consider using `Vec::with_capacity(/* Space hint */)`",
6261
s,
6362
Applicability::HasPlaceholders,
6463
);
@@ -72,7 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
7271

7372
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
7473
if let Some(init_expr) = local.init
75-
&& let PatKind::Binding(BindingAnnotation::MUT, id, _name, None) = local.pat.kind
74+
&& let PatKind::Binding(BindingAnnotation::MUT, id, _, None) = local.pat.kind
7675
&& !in_external_macro(cx.sess(), local.span)
7776
&& let Some(init) = get_vec_init_kind(cx, init_expr)
7877
&& !matches!(init, VecInitKind::WithExprCapacity(_))
@@ -81,12 +80,9 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
8180
self.searcher = Some(VecReserveSearcher {
8281
local_id: id,
8382
err_span: local.span,
84-
init_part: format!("let {}: {}",
85-
snippet(cx, local.pat.span, ""),
86-
match local.ty {
87-
Some(type_inference) => snippet(cx, type_inference.span, "").to_string(),
88-
None => String::new()
89-
}),
83+
init_part: snippet(cx, local.span.shrink_to_lo()
84+
.to(init_expr.span.source_callsite().shrink_to_lo()), "..")
85+
.into_owned(),
9086
space_hint: String::new()
9187
});
9288
}
@@ -96,17 +92,20 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
9692
if self.searcher.is_none()
9793
&& let ExprKind::Assign(left, right, _) = expr.kind
9894
&& let ExprKind::Path(QPath::Resolved(None, path)) = left.kind
99-
&& let [_name] = &path.segments
95+
&& let [_] = &path.segments
10096
&& let Res::Local(id) = path.res
10197
&& !in_external_macro(cx.sess(), expr.span)
10298
&& let Some(init) = get_vec_init_kind(cx, right)
103-
&& !matches!(init, VecInitKind::WithExprCapacity(_))
104-
&& !matches!(init, VecInitKind::WithConstCapacity(_))
99+
&& !matches!(init, VecInitKind::WithExprCapacity(_)
100+
| VecInitKind::WithConstCapacity(_)
101+
)
105102
{
106103
self.searcher = Some(VecReserveSearcher {
107104
local_id: id,
108105
err_span: expr.span,
109-
init_part: snippet(cx, left.span, "").to_string(),
106+
init_part: snippet(cx, left.span.shrink_to_lo()
107+
.to(right.span.source_callsite().shrink_to_lo()), "..")
108+
.into_owned(), // see `assign_expression` test
110109
space_hint: String::new()
111110
});
112111
}
@@ -115,14 +114,13 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
115114
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
116115
if let Some(searcher) = self.searcher.take() {
117116
if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind
118-
&& let ExprKind::MethodCall(name, self_arg, other_args, _) = expr.kind
119-
&& other_args.len() == 1
117+
&& let ExprKind::MethodCall(name, self_arg, [space_hint], _) = expr.kind
120118
&& path_to_local_id(self_arg, searcher.local_id)
121119
&& name.ident.as_str() == "reserve"
122120
{
123121
self.searcher = Some(VecReserveSearcher {
124122
err_span: searcher.err_span.to(stmt.span),
125-
space_hint: snippet(cx, other_args[0].span, "").to_string(),
123+
space_hint: snippet(cx, space_hint.span, "..").to_string(),
126124
.. searcher
127125
});
128126
} else {
Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,56 @@
11
#![warn(clippy::reserve_after_initialization)]
2+
#![no_main]
23

3-
fn main() {
4-
// Should lint
4+
// Should lint
5+
fn standard() {
56
let mut v1: Vec<usize> = Vec::with_capacity(10);
7+
}
68

7-
// Should lint
9+
// Should lint
10+
fn capacity_as_expr() {
811
let capacity = 10;
912
let mut v2: Vec<usize> = Vec::with_capacity(capacity);
13+
}
1014

11-
// Shouldn't lint
15+
// Shouldn't lint
16+
fn vec_init_with_argument() {
1217
let mut v3 = vec![1];
1318
v3.reserve(10);
19+
}
20+
21+
// Shouldn't lint
22+
fn called_with_capacity() {
23+
let _v4: Vec<usize> = Vec::with_capacity(10);
24+
}
1425

15-
// Shouldn't lint
16-
let mut v4: Vec<usize> = Vec::with_capacity(10);
26+
// Should lint
27+
fn assign_expression() {
28+
let mut v5: Vec<usize> = Vec::new();
29+
v5 = Vec::with_capacity(10);
1730
}
31+
32+
/*fn in_macros() {
33+
external! {
34+
// Should lint
35+
let mut v1: Vec<usize> = vec![];
36+
v1.reserve(10);
37+
38+
// Should lint
39+
let capacity = 10;
40+
let mut v2: Vec<usize> = vec![];
41+
v2.reserve(capacity);
42+
}
43+
44+
with_span! {
45+
span
46+
47+
// Should lint
48+
let mut v1: Vec<usize> = vec![];
49+
v1.reserve(10);
50+
51+
// Should lint
52+
let capacity = 10;
53+
let mut v2: Vec<usize> = vec![];
54+
v2.reserve(capacity);
55+
}
56+
}*/
Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,59 @@
11
#![warn(clippy::reserve_after_initialization)]
2+
#![no_main]
23

3-
fn main() {
4-
// Should lint
4+
// Should lint
5+
fn standard() {
56
let mut v1: Vec<usize> = vec![];
67
v1.reserve(10);
8+
}
79

8-
// Should lint
10+
// Should lint
11+
fn capacity_as_expr() {
912
let capacity = 10;
1013
let mut v2: Vec<usize> = vec![];
1114
v2.reserve(capacity);
15+
}
1216

13-
// Shouldn't lint
17+
// Shouldn't lint
18+
fn vec_init_with_argument() {
1419
let mut v3 = vec![1];
1520
v3.reserve(10);
21+
}
22+
23+
// Shouldn't lint
24+
fn called_with_capacity() {
25+
let _v4: Vec<usize> = Vec::with_capacity(10);
26+
}
1627

17-
// Shouldn't lint
18-
let mut v4: Vec<usize> = Vec::with_capacity(10);
28+
// Should lint
29+
fn assign_expression() {
30+
let mut v5: Vec<usize> = Vec::new();
31+
v5 = Vec::new();
32+
v5.reserve(10);
1933
}
34+
35+
/*fn in_macros() {
36+
external! {
37+
// Should lint
38+
let mut v1: Vec<usize> = vec![];
39+
v1.reserve(10);
40+
41+
// Should lint
42+
let capacity = 10;
43+
let mut v2: Vec<usize> = vec![];
44+
v2.reserve(capacity);
45+
}
46+
47+
with_span! {
48+
span
49+
50+
// Should lint
51+
let mut v1: Vec<usize> = vec![];
52+
v1.reserve(10);
53+
54+
// Should lint
55+
let capacity = 10;
56+
let mut v2: Vec<usize> = vec![];
57+
v2.reserve(capacity);
58+
}
59+
}*/
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
error: calls to `reverse` immediately after creation
2-
--> $DIR/reserve_after_initialization.rs:5:5
1+
error: call to `reserve` immediately after creation
2+
--> $DIR/reserve_after_initialization.rs:6:5
33
|
44
LL | / let mut v1: Vec<usize> = vec![];
55
LL | | v1.reserve(10);
6-
| |___________________^ help: consider using `Vec::with_capacity(space_hint)`: `let mut v1: Vec<usize> = Vec::with_capacity(10);`
6+
| |___________________^ help: consider using `Vec::with_capacity(/* Space hint */)`: `let mut v1: Vec<usize> = Vec::with_capacity(10);`
77
|
88
= note: `-D clippy::reserve-after-initialization` implied by `-D warnings`
99

10-
error: calls to `reverse` immediately after creation
11-
--> $DIR/reserve_after_initialization.rs:10:5
10+
error: call to `reserve` immediately after creation
11+
--> $DIR/reserve_after_initialization.rs:13:5
1212
|
1313
LL | / let mut v2: Vec<usize> = vec![];
1414
LL | | v2.reserve(capacity);
15-
| |_________________________^ help: consider using `Vec::with_capacity(space_hint)`: `let mut v2: Vec<usize> = Vec::with_capacity(capacity);`
15+
| |_________________________^ help: consider using `Vec::with_capacity(/* Space hint */)`: `let mut v2: Vec<usize> = Vec::with_capacity(capacity);`
1616

17-
error: aborting due to 2 previous errors
17+
error: call to `reserve` immediately after creation
18+
--> $DIR/reserve_after_initialization.rs:31:5
19+
|
20+
LL | / v5 = Vec::new();
21+
LL | | v5.reserve(10);
22+
| |___________________^ help: consider using `Vec::with_capacity(/* Space hint */)`: `v5 = Vec::with_capacity(10);`
23+
24+
error: aborting due to 3 previous errors
1825

0 commit comments

Comments
 (0)