@@ -50,11 +50,11 @@ struct VecReserveSearcher {
50
50
name : Symbol ,
51
51
err_span : Span ,
52
52
last_reserve_expr : HirId ,
53
- space_hint : usize ,
53
+ space_hint : Option < usize > ,
54
54
}
55
55
impl VecReserveSearcher {
56
56
fn display_err ( & self , cx : & LateContext < ' _ > ) {
57
- if self . space_hint == 0 {
57
+ if self . space_hint == Some ( 0 ) {
58
58
return ;
59
59
}
60
60
@@ -118,7 +118,16 @@ impl VecReserveSearcher {
118
118
s. push_str ( ": " ) ;
119
119
s. push_str ( & snippet ( cx, span, "_" ) ) ;
120
120
}
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
+ ) ;
122
131
123
132
span_lint_and_sugg (
124
133
cx,
@@ -151,7 +160,7 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
151
160
let_ty_span : local. ty . map ( |ty| ty. span ) ,
152
161
err_span : local. span ,
153
162
last_reserve_expr : init_expr. hir_id ,
154
- space_hint : 0
163
+ space_hint : Some ( 0 )
155
164
} ) ;
156
165
}
157
166
}
@@ -173,7 +182,7 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
173
182
name : name. ident . name ,
174
183
err_span : expr. span ,
175
184
last_reserve_expr : expr. hir_id ,
176
- space_hint : 0
185
+ space_hint : Some ( 0 )
177
186
} ) ;
178
187
}
179
188
}
@@ -183,17 +192,25 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
183
192
if let StmtKind :: Expr ( expr) | StmtKind :: Semi ( expr) = stmt. kind
184
193
&& let ExprKind :: MethodCall ( name, self_arg, other_args, _) = expr. kind
185
194
&& other_args. len ( ) == 1
186
- && let ExprKind :: Lit ( lit) = other_args[ 0 ] . kind
187
- && let LitKind :: Int ( space_hint, _) = lit. node
188
195
&& path_to_local_id ( self_arg, searcher. local_id )
189
196
&& name. ident . as_str ( ) == "reserve"
190
197
{
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
+ }
197
214
} else {
198
215
searcher. display_err ( cx) ;
199
216
}
0 commit comments