1- use crate :: utils:: { is_type_diagnostic_item, match_def_path, paths, snippet, span_lint_and_sugg} ;
1+ use crate :: utils:: {
2+ is_type_diagnostic_item, match_def_path, path_to_local, path_to_local_id, paths, snippet, span_lint_and_sugg,
3+ } ;
24use if_chain:: if_chain;
35use rustc_ast:: ast:: LitKind ;
46use rustc_errors:: Applicability ;
5- use rustc_hir:: { BindingAnnotation , Block , Expr , ExprKind , Local , PatKind , QPath , Stmt , StmtKind } ;
7+ use rustc_hir:: { BindingAnnotation , Block , Expr , ExprKind , HirId , Local , PatKind , QPath , Stmt , StmtKind } ;
68use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
79use rustc_middle:: lint:: in_external_macro;
810use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
9- use rustc_span:: { symbol:: sym, Span , Symbol } ;
11+ use rustc_span:: { symbol:: sym, Span } ;
1012use std:: convert:: TryInto ;
1113
1214declare_clippy_lint ! {
@@ -45,8 +47,8 @@ enum VecInitKind {
4547 WithCapacity ( u64 ) ,
4648}
4749struct VecPushSearcher {
50+ local_id : HirId ,
4851 init : VecInitKind ,
49- name : Symbol ,
5052 lhs_is_local : bool ,
5153 lhs_span : Span ,
5254 err_span : Span ,
@@ -81,17 +83,20 @@ impl VecPushSearcher {
8183}
8284
8385impl LateLintPass < ' _ > for VecInitThenPush {
84- fn check_local ( & mut self , cx : & LateContext < ' tcx > , local : & ' tcx Local < ' tcx > ) {
86+ fn check_block ( & mut self , _ : & LateContext < ' tcx > , _ : & ' tcx Block < ' tcx > ) {
8587 self . searcher = None ;
88+ }
89+
90+ fn check_local ( & mut self , cx : & LateContext < ' tcx > , local : & ' tcx Local < ' tcx > ) {
8691 if_chain ! {
8792 if !in_external_macro( cx. sess( ) , local. span) ;
8893 if let Some ( init) = local. init;
89- if let PatKind :: Binding ( BindingAnnotation :: Mutable , _ , ident , None ) = local. pat. kind;
94+ if let PatKind :: Binding ( BindingAnnotation :: Mutable , id , _ , None ) = local. pat. kind;
9095 if let Some ( init_kind) = get_vec_init_kind( cx, init) ;
9196 then {
9297 self . searcher = Some ( VecPushSearcher {
98+ local_id: id,
9399 init: init_kind,
94- name: ident. name,
95100 lhs_is_local: true ,
96101 lhs_span: local. ty. map_or( local. pat. span, |t| local. pat. span. to( t. span) ) ,
97102 err_span: local. span,
@@ -106,13 +111,12 @@ impl LateLintPass<'_> for VecInitThenPush {
106111 if_chain ! {
107112 if !in_external_macro( cx. sess( ) , expr. span) ;
108113 if let ExprKind :: Assign ( left, right, _) = expr. kind;
109- if let ExprKind :: Path ( QPath :: Resolved ( _, path) ) = left. kind;
110- if let Some ( name) = path. segments. get( 0 ) ;
114+ if let Some ( id) = path_to_local( left) ;
111115 if let Some ( init_kind) = get_vec_init_kind( cx, right) ;
112116 then {
113117 self . searcher = Some ( VecPushSearcher {
118+ local_id: id,
114119 init: init_kind,
115- name: name. ident. name,
116120 lhs_is_local: false ,
117121 lhs_span: left. span,
118122 err_span: expr. span,
@@ -128,10 +132,8 @@ impl LateLintPass<'_> for VecInitThenPush {
128132 if_chain ! {
129133 if let StmtKind :: Expr ( expr) | StmtKind :: Semi ( expr) = stmt. kind;
130134 if let ExprKind :: MethodCall ( path, _, [ self_arg, _] , _) = expr. kind;
135+ if path_to_local_id( self_arg, searcher. local_id) ;
131136 if path. ident. name. as_str( ) == "push" ;
132- if let ExprKind :: Path ( QPath :: Resolved ( _, self_path) ) = self_arg. kind;
133- if let [ self_name] = self_path. segments;
134- if self_name. ident. name == searcher. name;
135137 then {
136138 self . searcher = Some ( VecPushSearcher {
137139 found: searcher. found + 1 ,
0 commit comments