@@ -132,15 +132,17 @@ fn unused_crates_lint<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) {
132
132
133
133
// If the crate is fully unused, we suggest removing it altogether.
134
134
// We do this in any edition.
135
- if let Some ( & span) = unused_extern_crates. get ( & extern_crate. def_id ) {
136
- assert_eq ! ( extern_crate. def_id. krate, LOCAL_CRATE ) ;
137
- let hir_id = tcx. hir . definitions ( ) . def_index_to_hir_id ( extern_crate. def_id . index ) ;
138
- let id = tcx. hir . hir_to_node_id ( hir_id) ;
139
- let msg = "unused extern crate" ;
140
- tcx. struct_span_lint_node ( lint, id, span, msg)
141
- . span_suggestion_short ( span, "remove it" , "" . to_string ( ) )
142
- . emit ( ) ;
143
- continue ;
135
+ if extern_crate. warn_if_unused {
136
+ if let Some ( & span) = unused_extern_crates. get ( & extern_crate. def_id ) {
137
+ assert_eq ! ( extern_crate. def_id. krate, LOCAL_CRATE ) ;
138
+ let hir_id = tcx. hir . definitions ( ) . def_index_to_hir_id ( extern_crate. def_id . index ) ;
139
+ let id = tcx. hir . hir_to_node_id ( hir_id) ;
140
+ let msg = "unused extern crate" ;
141
+ tcx. struct_span_lint_node ( lint, id, span, msg)
142
+ . span_suggestion_short ( span, "remove it" , "" . to_string ( ) )
143
+ . emit ( ) ;
144
+ continue ;
145
+ }
144
146
}
145
147
146
148
// If we are not in Rust 2018 edition, then we don't make any further
@@ -192,6 +194,10 @@ struct ExternCrateToLint {
192
194
/// crate_name`), and -- perhaps surprisingly -- this stores the
193
195
/// *original* name (`item.name` will contain the new name)
194
196
orig_name : Option < ast:: Name > ,
197
+
198
+ /// if `false`, the original name started with `_`, so we shouldn't lint
199
+ /// about it going unused (but we should still emit idiom lints).
200
+ warn_if_unused : bool ,
195
201
}
196
202
197
203
impl < ' a , ' tcx , ' v > ItemLikeVisitor < ' v > for CollectExternCrateVisitor < ' a , ' tcx > {
@@ -203,6 +209,7 @@ impl<'a, 'tcx, 'v> ItemLikeVisitor<'v> for CollectExternCrateVisitor<'a, 'tcx> {
203
209
def_id : extern_crate_def_id,
204
210
span : item. span ,
205
211
orig_name,
212
+ warn_if_unused : !item. name . as_str ( ) . starts_with ( '_' ) ,
206
213
}
207
214
) ;
208
215
}
0 commit comments