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
+ } ;
2
4
use if_chain:: if_chain;
3
5
use rustc_ast:: ast:: LitKind ;
4
6
use 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 } ;
6
8
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
7
9
use rustc_middle:: lint:: in_external_macro;
8
10
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
9
- use rustc_span:: { symbol:: sym, Span , Symbol } ;
11
+ use rustc_span:: { symbol:: sym, Span } ;
10
12
use std:: convert:: TryInto ;
11
13
12
14
declare_clippy_lint ! {
@@ -45,8 +47,8 @@ enum VecInitKind {
45
47
WithCapacity ( u64 ) ,
46
48
}
47
49
struct VecPushSearcher {
50
+ local_id : HirId ,
48
51
init : VecInitKind ,
49
- name : Symbol ,
50
52
lhs_is_local : bool ,
51
53
lhs_span : Span ,
52
54
err_span : Span ,
@@ -86,12 +88,12 @@ impl LateLintPass<'_> for VecInitThenPush {
86
88
if_chain ! {
87
89
if !in_external_macro( cx. sess( ) , local. span) ;
88
90
if let Some ( init) = local. init;
89
- if let PatKind :: Binding ( BindingAnnotation :: Mutable , _ , ident , None ) = local. pat. kind;
91
+ if let PatKind :: Binding ( BindingAnnotation :: Mutable , id , _ , None ) = local. pat. kind;
90
92
if let Some ( init_kind) = get_vec_init_kind( cx, init) ;
91
93
then {
92
94
self . searcher = Some ( VecPushSearcher {
95
+ local_id: id,
93
96
init: init_kind,
94
- name: ident. name,
95
97
lhs_is_local: true ,
96
98
lhs_span: local. ty. map_or( local. pat. span, |t| local. pat. span. to( t. span) ) ,
97
99
err_span: local. span,
@@ -106,13 +108,12 @@ impl LateLintPass<'_> for VecInitThenPush {
106
108
if_chain ! {
107
109
if !in_external_macro( cx. sess( ) , expr. span) ;
108
110
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 ) ;
111
+ if let Some ( id) = path_to_local( left) ;
111
112
if let Some ( init_kind) = get_vec_init_kind( cx, right) ;
112
113
then {
113
114
self . searcher = Some ( VecPushSearcher {
115
+ local_id: id,
114
116
init: init_kind,
115
- name: name. ident. name,
116
117
lhs_is_local: false ,
117
118
lhs_span: left. span,
118
119
err_span: expr. span,
@@ -128,10 +129,8 @@ impl LateLintPass<'_> for VecInitThenPush {
128
129
if_chain ! {
129
130
if let StmtKind :: Expr ( expr) | StmtKind :: Semi ( expr) = stmt. kind;
130
131
if let ExprKind :: MethodCall ( path, _, [ self_arg, _] , _) = expr. kind;
132
+ if path_to_local_id( self_arg, searcher. local_id) ;
131
133
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;
135
134
then {
136
135
self . searcher = Some ( VecPushSearcher {
137
136
found: searcher. found + 1 ,
0 commit comments