@@ -28,6 +28,9 @@ declare_clippy_lint! {
2828 /// let mut vec1 = Vec::with_capacity(len);
2929 /// vec1.resize(len, 0);
3030 ///
31+ /// let mut vec1 = Vec::with_capacity(len);
32+ /// vec1.resize(vec1.capacity(), 0);
33+ ///
3134 /// let mut vec2 = Vec::with_capacity(len);
3235 /// vec2.extend(repeat(0).take(len));
3336 ///
@@ -210,23 +213,20 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
210213
211214 /// Checks if the given expression is resizing a vector with 0
212215 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)
219220 // 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+ }
228229 }
229- }
230230 }
231231
232232 /// Returns `true` if give expression is `repeat(0).take(...)`
@@ -239,12 +239,15 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
239239 if let Some ( repeat_expr) = take_args. get( 0 ) ;
240240 if self . is_repeat_zero( repeat_expr) ;
241241
242- // Check that len expression is equals to `with_capacity` expression
243242 if let Some ( len_arg) = take_args. get( 1 ) ;
244- if SpanlessEq :: new( self . cx) . eq_expr( len_arg, self . vec_alloc. len_expr) ;
245243
246244 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+ }
248251 }
249252 }
250253
0 commit comments