@@ -28,6 +28,9 @@ declare_clippy_lint! {
28
28
/// let mut vec1 = Vec::with_capacity(len);
29
29
/// vec1.resize(len, 0);
30
30
///
31
+ /// let mut vec1 = Vec::with_capacity(len);
32
+ /// vec1.resize(vec1.capacity(), 0);
33
+ ///
31
34
/// let mut vec2 = Vec::with_capacity(len);
32
35
/// vec2.extend(repeat(0).take(len));
33
36
///
@@ -210,23 +213,20 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
210
213
211
214
/// Checks if the given expression is resizing a vector with 0
212
215
fn search_slow_resize_filling ( & mut self , expr : & ' tcx Expr < ' _ > ) {
213
- if_chain ! {
214
- if self . initialization_found;
215
- if let ExprKind :: MethodCall ( path, [ self_arg, len_arg, fill_arg] , _) = expr. kind;
216
- if path_to_local_id( self_arg, self . vec_alloc. local_id) ;
217
- if path. ident. name == sym!( resize) ;
218
-
216
+ if self . initialization_found
217
+ && let ExprKind :: MethodCall ( path, [ self_arg, len_arg, fill_arg] , _) = expr. kind
218
+ && path_to_local_id ( self_arg, self . vec_alloc . local_id )
219
+ && path. ident . name == sym ! ( resize)
219
220
// Check that is filled with 0
220
- if let ExprKind :: Lit ( ref lit) = fill_arg. kind;
221
- if let LitKind :: Int ( 0 , _) = lit. node;
222
-
223
- // Check that len expression is equals to `with_capacity` expression
224
- if SpanlessEq :: new ( self . cx ) . eq_expr ( len_arg , self . vec_alloc . len_expr ) ;
225
-
226
- then {
227
- self . slow_expression = Some ( InitializationType :: Resize ( expr ) ) ;
221
+ && let ExprKind :: Lit ( ref lit) = fill_arg. kind
222
+ && let LitKind :: Int ( 0 , _) = lit. node {
223
+ // Check that len expression is equals to `with_capacity` expression
224
+ if SpanlessEq :: new ( self . cx ) . eq_expr ( len_arg , self . vec_alloc . len_expr ) {
225
+ self . slow_expression = Some ( InitializationType :: Resize ( expr ) ) ;
226
+ } else if let ExprKind :: MethodCall ( path , _ , _ ) = len_arg . kind && path . ident . as_str ( ) == "capacity" {
227
+ self . slow_expression = Some ( InitializationType :: Resize ( expr ) ) ;
228
+ }
228
229
}
229
- }
230
230
}
231
231
232
232
/// Returns `true` if give expression is `repeat(0).take(...)`
@@ -239,12 +239,15 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
239
239
if let Some ( repeat_expr) = take_args. get( 0 ) ;
240
240
if self . is_repeat_zero( repeat_expr) ;
241
241
242
- // Check that len expression is equals to `with_capacity` expression
243
242
if let Some ( len_arg) = take_args. get( 1 ) ;
244
- if SpanlessEq :: new( self . cx) . eq_expr( len_arg, self . vec_alloc. len_expr) ;
245
243
246
244
then {
247
- return true ;
245
+ // Check that len expression is equals to `with_capacity` expression
246
+ if SpanlessEq :: new( self . cx) . eq_expr( len_arg, self . vec_alloc. len_expr) {
247
+ return true ;
248
+ } else if let ExprKind :: MethodCall ( path, _, _) = len_arg. kind && path. ident. as_str( ) == "capacity" {
249
+ return true ;
250
+ }
248
251
}
249
252
}
250
253
0 commit comments