11use clippy_utils:: diagnostics:: span_lint_and_sugg;
2- use clippy_utils:: is_test_module_or_function ;
2+ use clippy_utils:: is_in_test ;
33use clippy_utils:: source:: { snippet, snippet_with_applicability} ;
44use rustc_data_structures:: fx:: FxHashSet ;
55use rustc_errors:: Applicability ;
@@ -100,15 +100,13 @@ declare_clippy_lint! {
100100#[ derive( Default ) ]
101101pub struct WildcardImports {
102102 warn_on_all : bool ,
103- test_modules_deep : u32 ,
104103 allowed_segments : FxHashSet < String > ,
105104}
106105
107106impl WildcardImports {
108107 pub fn new ( warn_on_all : bool , allowed_wildcard_imports : FxHashSet < String > ) -> Self {
109108 Self {
110109 warn_on_all,
111- test_modules_deep : 0 ,
112110 allowed_segments : allowed_wildcard_imports,
113111 }
114112 }
@@ -122,15 +120,12 @@ impl LateLintPass<'_> for WildcardImports {
122120 return ;
123121 }
124122
125- if is_test_module_or_function ( cx. tcx , item) {
126- self . test_modules_deep = self . test_modules_deep . saturating_add ( 1 ) ;
127- }
128123 let module = cx. tcx . parent_module_from_def_id ( item. owner_id . def_id ) ;
129124 if cx. tcx . visibility ( item. owner_id . def_id ) != ty:: Visibility :: Restricted ( module. to_def_id ( ) ) {
130125 return ;
131126 }
132127 if let ItemKind :: Use ( use_path, UseKind :: Glob ) = & item. kind
133- && ( self . warn_on_all || !self . check_exceptions ( item, use_path. segments ) )
128+ && ( self . warn_on_all || !self . check_exceptions ( cx , item, use_path. segments ) )
134129 && let used_imports = cx. tcx . names_imported_by_glob_use ( item. owner_id . def_id )
135130 && !used_imports. is_empty ( ) // Already handled by `unused_imports`
136131 && !used_imports. contains ( & kw:: Underscore )
@@ -180,20 +175,14 @@ impl LateLintPass<'_> for WildcardImports {
180175 span_lint_and_sugg ( cx, lint, span, message, "try" , sugg, applicability) ;
181176 }
182177 }
183-
184- fn check_item_post ( & mut self , cx : & LateContext < ' _ > , item : & Item < ' _ > ) {
185- if is_test_module_or_function ( cx. tcx , item) {
186- self . test_modules_deep = self . test_modules_deep . saturating_sub ( 1 ) ;
187- }
188- }
189178}
190179
191180impl WildcardImports {
192- fn check_exceptions ( & self , item : & Item < ' _ > , segments : & [ PathSegment < ' _ > ] ) -> bool {
181+ fn check_exceptions ( & self , cx : & LateContext < ' _ > , item : & Item < ' _ > , segments : & [ PathSegment < ' _ > ] ) -> bool {
193182 item. span . from_expansion ( )
194183 || is_prelude_import ( segments)
195- || ( is_super_only_import ( segments) && self . test_modules_deep > 0 )
196184 || is_allowed_via_config ( segments, & self . allowed_segments )
185+ || ( is_super_only_import ( segments) && is_in_test ( cx. tcx , item. hir_id ( ) ) )
197186 }
198187}
199188
0 commit comments