@@ -2,7 +2,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22use clippy_utils:: higher:: { get_vec_init_kind, VecInitKind } ;
33use clippy_utils:: path_to_local_id;
44use clippy_utils:: source:: snippet;
5- //use rustc_ast::LitKind;
65use rustc_errors:: Applicability ;
76use rustc_hir:: def:: Res ;
87use rustc_hir:: { BindingAnnotation , Block , Expr , ExprKind , HirId , Local , PatKind , QPath , Stmt , StmtKind } ;
@@ -16,7 +15,7 @@ declare_clippy_lint! {
1615 /// Informs the user about a more concise way to create a vector with a known capacity.
1716 ///
1817 /// ### Why is this bad?
19- /// The `Vec::with_capacity` constructor is easier to understand .
18+ /// The `Vec::with_capacity` constructor is less complex .
2019 ///
2120 /// ### Example
2221 /// ```rust
@@ -51,14 +50,14 @@ impl VecReserveSearcher {
5150 return ;
5251 }
5352
54- let s = format ! ( "{} = Vec::with_capacity({});" , self . init_part, self . space_hint) ;
53+ let s = format ! ( "{}Vec::with_capacity({});" , self . init_part, self . space_hint) ;
5554
5655 span_lint_and_sugg (
5756 cx,
5857 RESERVE_AFTER_INITIALIZATION ,
5958 self . err_span ,
60- "calls to `reverse ` immediately after creation" ,
61- "consider using `Vec::with_capacity(space_hint )`" ,
59+ "call to `reserve ` immediately after creation" ,
60+ "consider using `Vec::with_capacity(/* Space hint */ )`" ,
6261 s,
6362 Applicability :: HasPlaceholders ,
6463 ) ;
@@ -72,7 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
7271
7372 fn check_local ( & mut self , cx : & LateContext < ' tcx > , local : & ' tcx Local < ' tcx > ) {
7473 if let Some ( init_expr) = local. init
75- && let PatKind :: Binding ( BindingAnnotation :: MUT , id, _name , None ) = local. pat . kind
74+ && let PatKind :: Binding ( BindingAnnotation :: MUT , id, _ , None ) = local. pat . kind
7675 && !in_external_macro ( cx. sess ( ) , local. span )
7776 && let Some ( init) = get_vec_init_kind ( cx, init_expr)
7877 && !matches ! ( init, VecInitKind :: WithExprCapacity ( _) )
@@ -81,12 +80,9 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
8180 self . searcher = Some ( VecReserveSearcher {
8281 local_id : id,
8382 err_span : local. span ,
84- init_part : format ! ( "let {}: {}" ,
85- snippet( cx, local. pat. span, "" ) ,
86- match local. ty {
87- Some ( type_inference) => snippet( cx, type_inference. span, "" ) . to_string( ) ,
88- None => String :: new( )
89- } ) ,
83+ init_part : snippet ( cx, local. span . shrink_to_lo ( )
84+ . to ( init_expr. span . source_callsite ( ) . shrink_to_lo ( ) ) , ".." )
85+ . into_owned ( ) ,
9086 space_hint : String :: new ( )
9187 } ) ;
9288 }
@@ -96,17 +92,20 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
9692 if self . searcher . is_none ( )
9793 && let ExprKind :: Assign ( left, right, _) = expr. kind
9894 && let ExprKind :: Path ( QPath :: Resolved ( None , path) ) = left. kind
99- && let [ _name ] = & path. segments
95+ && let [ _ ] = & path. segments
10096 && let Res :: Local ( id) = path. res
10197 && !in_external_macro ( cx. sess ( ) , expr. span )
10298 && let Some ( init) = get_vec_init_kind ( cx, right)
103- && !matches ! ( init, VecInitKind :: WithExprCapacity ( _) )
104- && !matches ! ( init, VecInitKind :: WithConstCapacity ( _) )
99+ && !matches ! ( init, VecInitKind :: WithExprCapacity ( _)
100+ | VecInitKind :: WithConstCapacity ( _)
101+ )
105102 {
106103 self . searcher = Some ( VecReserveSearcher {
107104 local_id : id,
108105 err_span : expr. span ,
109- init_part : snippet ( cx, left. span , "" ) . to_string ( ) ,
106+ init_part : snippet ( cx, left. span . shrink_to_lo ( )
107+ . to ( right. span . source_callsite ( ) . shrink_to_lo ( ) ) , ".." )
108+ . into_owned ( ) , // see `assign_expression` test
110109 space_hint : String :: new ( )
111110 } ) ;
112111 }
@@ -115,14 +114,13 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
115114 fn check_stmt ( & mut self , cx : & LateContext < ' tcx > , stmt : & ' tcx Stmt < ' _ > ) {
116115 if let Some ( searcher) = self . searcher . take ( ) {
117116 if let StmtKind :: Expr ( expr) | StmtKind :: Semi ( expr) = stmt. kind
118- && let ExprKind :: MethodCall ( name, self_arg, other_args, _) = expr. kind
119- && other_args. len ( ) == 1
117+ && let ExprKind :: MethodCall ( name, self_arg, [ space_hint] , _) = expr. kind
120118 && path_to_local_id ( self_arg, searcher. local_id )
121119 && name. ident . as_str ( ) == "reserve"
122120 {
123121 self . searcher = Some ( VecReserveSearcher {
124122 err_span : searcher. err_span . to ( stmt. span ) ,
125- space_hint : snippet ( cx, other_args [ 0 ] . span , "" ) . to_string ( ) ,
123+ space_hint : snippet ( cx, space_hint . span , ".. " ) . to_string ( ) ,
126124 .. searcher
127125 } ) ;
128126 } else {
0 commit comments