Skip to content

Commit a1bf23f

Browse files
committed
Added more use cases
1 parent 073149a commit a1bf23f

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

clippy_lints/src/reserve_after_initialization.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ struct VecReserveSearcher {
5050
name: Symbol,
5151
err_span: Span,
5252
last_reserve_expr: HirId,
53-
space_hint: usize,
53+
space_hint: Option<usize>,
5454
}
5555
impl VecReserveSearcher {
5656
fn display_err(&self, cx: &LateContext<'_>) {
57-
if self.space_hint == 0 {
57+
if self.space_hint == Some(0) {
5858
return;
5959
}
6060

@@ -118,7 +118,16 @@ impl VecReserveSearcher {
118118
s.push_str(": ");
119119
s.push_str(&snippet(cx, span, "_"));
120120
}
121-
s.push_str(format!(" = Vec::with_capacity({});", self.space_hint).as_str());
121+
s.push_str(
122+
format!(
123+
" = Vec::with_capacity({});",
124+
match self.space_hint {
125+
None => "..".to_string(),
126+
Some(hint) => hint.to_string(),
127+
}
128+
)
129+
.as_str(),
130+
);
122131

123132
span_lint_and_sugg(
124133
cx,
@@ -151,7 +160,7 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
151160
let_ty_span: local.ty.map(|ty| ty.span),
152161
err_span: local.span,
153162
last_reserve_expr: init_expr.hir_id,
154-
space_hint: 0
163+
space_hint: Some(0)
155164
});
156165
}
157166
}
@@ -173,7 +182,7 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
173182
name: name.ident.name,
174183
err_span: expr.span,
175184
last_reserve_expr: expr.hir_id,
176-
space_hint: 0
185+
space_hint: Some(0)
177186
});
178187
}
179188
}
@@ -183,17 +192,25 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
183192
if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind
184193
&& let ExprKind::MethodCall(name, self_arg, other_args, _) = expr.kind
185194
&& other_args.len() == 1
186-
&& let ExprKind::Lit(lit) = other_args[0].kind
187-
&& let LitKind::Int(space_hint, _) = lit.node
188195
&& path_to_local_id(self_arg, searcher.local_id)
189196
&& name.ident.as_str() == "reserve"
190197
{
191-
self.searcher = Some(VecReserveSearcher {
192-
err_span: searcher.err_span.to(stmt.span),
193-
last_reserve_expr: expr.hir_id,
194-
space_hint: space_hint as usize,
195-
.. searcher
196-
});
198+
if let ExprKind::Lit(lit) = other_args[0].kind
199+
&& let LitKind::Int(space_hint, _) = lit.node {
200+
self.searcher = Some(VecReserveSearcher {
201+
err_span: searcher.err_span.to(stmt.span),
202+
last_reserve_expr: expr.hir_id,
203+
space_hint: Some(space_hint as usize), // the expression is an int, so we'll display the good amount as a hint
204+
.. searcher
205+
});
206+
} else {
207+
self.searcher = Some(VecReserveSearcher {
208+
err_span: searcher.err_span.to(stmt.span),
209+
last_reserve_expr: expr.hir_id,
210+
space_hint: None, // the expression isn't an int, so we'll display ".." as hint
211+
.. searcher
212+
});
213+
}
197214
} else {
198215
searcher.display_err(cx);
199216
}

0 commit comments

Comments
 (0)