@@ -2,7 +2,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
2
2
use clippy_utils:: higher:: { get_vec_init_kind, VecInitKind } ;
3
3
use clippy_utils:: path_to_local_id;
4
4
use clippy_utils:: source:: snippet;
5
- //use rustc_ast::LitKind;
6
5
use rustc_errors:: Applicability ;
7
6
use rustc_hir:: def:: Res ;
8
7
use rustc_hir:: { BindingAnnotation , Block , Expr , ExprKind , HirId , Local , PatKind , QPath , Stmt , StmtKind } ;
@@ -16,7 +15,7 @@ declare_clippy_lint! {
16
15
/// Informs the user about a more concise way to create a vector with a known capacity.
17
16
///
18
17
/// ### Why is this bad?
19
- /// The `Vec::with_capacity` constructor is easier to understand .
18
+ /// The `Vec::with_capacity` constructor is less complex .
20
19
///
21
20
/// ### Example
22
21
/// ```rust
@@ -51,14 +50,14 @@ impl VecReserveSearcher {
51
50
return ;
52
51
}
53
52
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) ;
55
54
56
55
span_lint_and_sugg (
57
56
cx,
58
57
RESERVE_AFTER_INITIALIZATION ,
59
58
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 */ )`" ,
62
61
s,
63
62
Applicability :: HasPlaceholders ,
64
63
) ;
@@ -72,7 +71,7 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
72
71
73
72
fn check_local ( & mut self , cx : & LateContext < ' tcx > , local : & ' tcx Local < ' tcx > ) {
74
73
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
76
75
&& !in_external_macro ( cx. sess ( ) , local. span )
77
76
&& let Some ( init) = get_vec_init_kind ( cx, init_expr)
78
77
&& !matches ! ( init, VecInitKind :: WithExprCapacity ( _) )
@@ -81,12 +80,9 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
81
80
self . searcher = Some ( VecReserveSearcher {
82
81
local_id : id,
83
82
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 ( ) ,
90
86
space_hint : String :: new ( )
91
87
} ) ;
92
88
}
@@ -96,17 +92,20 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
96
92
if self . searcher . is_none ( )
97
93
&& let ExprKind :: Assign ( left, right, _) = expr. kind
98
94
&& let ExprKind :: Path ( QPath :: Resolved ( None , path) ) = left. kind
99
- && let [ _name ] = & path. segments
95
+ && let [ _ ] = & path. segments
100
96
&& let Res :: Local ( id) = path. res
101
97
&& !in_external_macro ( cx. sess ( ) , expr. span )
102
98
&& 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
+ )
105
102
{
106
103
self . searcher = Some ( VecReserveSearcher {
107
104
local_id : id,
108
105
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
110
109
space_hint : String :: new ( )
111
110
} ) ;
112
111
}
@@ -115,14 +114,13 @@ impl<'tcx> LateLintPass<'tcx> for ReserveAfterInitialization {
115
114
fn check_stmt ( & mut self , cx : & LateContext < ' tcx > , stmt : & ' tcx Stmt < ' _ > ) {
116
115
if let Some ( searcher) = self . searcher . take ( ) {
117
116
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
120
118
&& path_to_local_id ( self_arg, searcher. local_id )
121
119
&& name. ident . as_str ( ) == "reserve"
122
120
{
123
121
self . searcher = Some ( VecReserveSearcher {
124
122
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 ( ) ,
126
124
.. searcher
127
125
} ) ;
128
126
} else {
0 commit comments