@@ -26,6 +26,9 @@ declare_clippy_lint! {
2626 /// let mut vec1 = Vec::with_capacity(len);
2727 /// vec1.resize(len, 0);
2828 ///
29+ /// let mut vec1 = Vec::with_capacity(len);
30+ /// vec1.resize(vec1.capacity(), 0);
31+ ///
2932 /// let mut vec2 = Vec::with_capacity(len);
3033 /// vec2.extend(repeat(0).take(len));
3134 /// ```
@@ -211,23 +214,20 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
211214
212215 /// Checks if the given expression is resizing a vector with 0
213216 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)
220221 // 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+ }
229230 }
230- }
231231 }
232232
233233 /// Returns `true` if give expression is `repeat(0).take(...)`
@@ -240,12 +240,15 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
240240 if let Some ( repeat_expr) = take_args. get( 0 ) ;
241241 if self . is_repeat_zero( repeat_expr) ;
242242
243- // Check that len expression is equals to `with_capacity` expression
244243 if let Some ( len_arg) = take_args. get( 1 ) ;
245- if SpanlessEq :: new( self . cx) . eq_expr( len_arg, self . vec_alloc. len_expr) ;
246244
247245 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+ }
249252 }
250253 }
251254
0 commit comments