1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
2
use clippy_utils:: is_test_module_or_function;
3
3
use clippy_utils:: source:: { snippet, snippet_with_applicability} ;
4
+ use rustc_data_structures:: fx:: FxHashSet ;
4
5
use rustc_errors:: Applicability ;
5
6
use rustc_hir:: def:: { DefKind , Res } ;
6
7
use rustc_hir:: { Item , ItemKind , PathSegment , UseKind } ;
@@ -100,13 +101,15 @@ declare_clippy_lint! {
100
101
pub struct WildcardImports {
101
102
warn_on_all : bool ,
102
103
test_modules_deep : u32 ,
104
+ ignored_segments : Vec < String > ,
103
105
}
104
106
105
107
impl WildcardImports {
106
- pub fn new ( warn_on_all : bool ) -> Self {
108
+ pub fn new ( warn_on_all : bool , ignored_wildcard_imports : Vec < String > ) -> Self {
107
109
Self {
108
110
warn_on_all,
109
111
test_modules_deep : 0 ,
112
+ ignored_segments : ignored_wildcard_imports,
110
113
}
111
114
}
112
115
}
@@ -190,6 +193,7 @@ impl WildcardImports {
190
193
item. span . from_expansion ( )
191
194
|| is_prelude_import ( segments)
192
195
|| ( is_super_only_import ( segments) && self . test_modules_deep > 0 )
196
+ || is_ignored_via_config ( segments, & self . ignored_segments )
193
197
}
194
198
}
195
199
@@ -198,10 +202,20 @@ impl WildcardImports {
198
202
fn is_prelude_import ( segments : & [ PathSegment < ' _ > ] ) -> bool {
199
203
segments
200
204
. iter ( )
201
- . any ( |ps| ps. ident . name . as_str ( ) . contains ( sym:: prelude. as_str ( ) ) )
205
+ . any ( |ps| ps. ident . as_str ( ) . contains ( sym:: prelude. as_str ( ) ) )
202
206
}
203
207
204
208
// Allow "super::*" imports in tests.
205
209
fn is_super_only_import ( segments : & [ PathSegment < ' _ > ] ) -> bool {
206
210
segments. len ( ) == 1 && segments[ 0 ] . ident . name == kw:: Super
207
211
}
212
+
213
+ // Allow skipping imports containing user configured segments,
214
+ // i.e. "...::utils::...::*" if user put `ignored-wildcard-imports = ["utils"]` in `Clippy.toml`
215
+ fn is_ignored_via_config ( segments : & [ PathSegment < ' _ > ] , ignored_segments : & [ String ] ) -> bool {
216
+ let segments_set: FxHashSet < & str > = segments. iter ( ) . map ( |s| s. ident . as_str ( ) ) . collect ( ) ;
217
+ let ignored_set: FxHashSet < & str > = ignored_segments. iter ( ) . map ( String :: as_str) . collect ( ) ;
218
+ // segment matching need to be exact instead of using 'contains', in case user unintentionaly put
219
+ // a single character in the config thus skipping most of the warnings.
220
+ segments_set. intersection ( & ignored_set) . next ( ) . is_some ( )
221
+ }
0 commit comments