@@ -24,10 +24,8 @@ use if_chain::if_chain;
24
24
use matches:: matches;
25
25
use rustc:: hir;
26
26
use rustc:: hir:: def:: Def ;
27
- use rustc:: hir:: def_id:: CrateNum ;
28
27
use rustc:: hir:: def_id:: { DefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
29
28
use rustc:: hir:: intravisit:: { NestedVisitorMap , Visitor } ;
30
- use rustc:: hir:: map:: { DefPathData , DisambiguatedDefPathData } ;
31
29
use rustc:: hir:: Node ;
32
30
use rustc:: hir:: * ;
33
31
use rustc:: lint:: { LateContext , Level , Lint , LintContext } ;
@@ -43,7 +41,7 @@ use rustc_errors::Applicability;
43
41
use syntax:: ast:: { self , LitKind } ;
44
42
use syntax:: attr;
45
43
use syntax:: source_map:: { Span , DUMMY_SP } ;
46
- use syntax:: symbol:: { keywords, LocalInternedString , Symbol } ;
44
+ use syntax:: symbol:: { keywords, Symbol } ;
47
45
48
46
use crate :: reexport:: * ;
49
47
@@ -95,135 +93,6 @@ pub fn in_macro(span: Span) -> bool {
95
93
span. ctxt ( ) . outer ( ) . expn_info ( ) . is_some ( )
96
94
}
97
95
98
- /// Used to store the absolute path to a type.
99
- ///
100
- /// See `match_def_path` for usage.
101
- pub struct AbsolutePathPrinter < ' a , ' tcx > {
102
- pub tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
103
- }
104
-
105
- use rustc:: ty:: print:: Printer ;
106
-
107
- #[ allow( clippy:: diverging_sub_expression) ]
108
- impl < ' tcx > Printer < ' tcx , ' tcx > for AbsolutePathPrinter < ' _ , ' tcx > {
109
- type Error = !;
110
-
111
- type Path = Vec < LocalInternedString > ;
112
- type Region = ( ) ;
113
- type Type = ( ) ;
114
- type DynExistential = ( ) ;
115
-
116
- fn tcx < ' a > ( & ' a self ) -> TyCtxt < ' a , ' tcx , ' tcx > {
117
- self . tcx
118
- }
119
-
120
- fn print_region ( self , _region : ty:: Region < ' _ > ) -> Result < Self :: Region , Self :: Error > {
121
- Ok ( ( ) )
122
- }
123
-
124
- fn print_type ( self , _ty : Ty < ' tcx > ) -> Result < Self :: Type , Self :: Error > {
125
- Ok ( ( ) )
126
- }
127
-
128
- fn print_dyn_existential (
129
- self ,
130
- _predicates : & ' tcx ty:: List < ty:: ExistentialPredicate < ' tcx > > ,
131
- ) -> Result < Self :: DynExistential , Self :: Error > {
132
- Ok ( ( ) )
133
- }
134
-
135
- fn path_crate ( self , cnum : CrateNum ) -> Result < Self :: Path , Self :: Error > {
136
- Ok ( vec ! [ self . tcx. original_crate_name( cnum) . as_str( ) ] )
137
- }
138
-
139
- fn path_qualified (
140
- self ,
141
- self_ty : Ty < ' tcx > ,
142
- trait_ref : Option < ty:: TraitRef < ' tcx > > ,
143
- ) -> Result < Self :: Path , Self :: Error > {
144
- if trait_ref. is_none ( ) {
145
- if let ty:: Adt ( def, substs) = self_ty. sty {
146
- return self . print_def_path ( def. did , substs) ;
147
- }
148
- }
149
-
150
- // This shouldn't ever be needed, but just in case:
151
- Ok ( vec ! [ match trait_ref {
152
- Some ( trait_ref) => Symbol :: intern( & format!( "{:?}" , trait_ref) ) . as_str( ) ,
153
- None => Symbol :: intern( & format!( "<{}>" , self_ty) ) . as_str( ) ,
154
- } ] )
155
- }
156
-
157
- fn path_append_impl (
158
- self ,
159
- print_prefix : impl FnOnce ( Self ) -> Result < Self :: Path , Self :: Error > ,
160
- _disambiguated_data : & DisambiguatedDefPathData ,
161
- self_ty : Ty < ' tcx > ,
162
- trait_ref : Option < ty:: TraitRef < ' tcx > > ,
163
- ) -> Result < Self :: Path , Self :: Error > {
164
- let mut path = print_prefix ( self ) ?;
165
-
166
- // This shouldn't ever be needed, but just in case:
167
- path. push ( match trait_ref {
168
- Some ( trait_ref) => Symbol :: intern ( & format ! ( "<impl {} for {}>" , trait_ref, self_ty) ) . as_str ( ) ,
169
- None => Symbol :: intern ( & format ! ( "<impl {}>" , self_ty) ) . as_str ( ) ,
170
- } ) ;
171
-
172
- Ok ( path)
173
- }
174
-
175
- fn path_append (
176
- self ,
177
- print_prefix : impl FnOnce ( Self ) -> Result < Self :: Path , Self :: Error > ,
178
- disambiguated_data : & DisambiguatedDefPathData ,
179
- ) -> Result < Self :: Path , Self :: Error > {
180
- let mut path = print_prefix ( self ) ?;
181
-
182
- // Skip `::{{constructor}}` on tuple/unit structs.
183
- if let DefPathData :: Ctor = disambiguated_data. data {
184
- return Ok ( path) ;
185
- }
186
-
187
- path. push ( disambiguated_data. data . as_interned_str ( ) . as_str ( ) ) ;
188
- Ok ( path)
189
- }
190
-
191
- fn path_generic_args (
192
- self ,
193
- print_prefix : impl FnOnce ( Self ) -> Result < Self :: Path , Self :: Error > ,
194
- _args : & [ Kind < ' tcx > ] ,
195
- ) -> Result < Self :: Path , Self :: Error > {
196
- print_prefix ( self )
197
- }
198
- }
199
-
200
- /// Checks if a `DefId`'s path matches the given absolute type path usage.
201
- ///
202
- /// # Examples
203
- /// ```rust,ignore
204
- /// match_def_path(cx.tcx, id, &["core", "option", "Option"])
205
- /// ```
206
- ///
207
- /// See also the `paths` module.
208
- pub fn match_def_path < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , def_id : DefId , path : & [ & str ] ) -> bool {
209
- let names = get_def_path ( tcx, def_id) ;
210
-
211
- names. len ( ) == path. len ( ) && names. into_iter ( ) . zip ( path. iter ( ) ) . all ( |( a, & b) | * a == * b)
212
- }
213
-
214
- /// Gets the absolute path of `def_id` as a vector of `&str`.
215
- ///
216
- /// # Examples
217
- /// ```rust,ignore
218
- /// let def_path = get_def_path(tcx, def_id);
219
- /// if let &["core", "option", "Option"] = &def_path[..] {
220
- /// // The given `def_id` is that of an `Option` type
221
- /// };
222
- /// ```
223
- pub fn get_def_path < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , def_id : DefId ) -> Vec < LocalInternedString > {
224
- AbsolutePathPrinter { tcx } . print_def_path ( def_id, & [ ] ) . unwrap ( )
225
- }
226
-
227
96
/// Checks if type is struct, enum or union type with the given def path.
228
97
pub fn match_type ( cx : & LateContext < ' _ , ' _ > , ty : Ty < ' _ > , path : & [ & str ] ) -> bool {
229
98
match ty. sty {
0 commit comments