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