Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 660f184

Browse files
authoredFeb 27, 2023
Rollup merge of #108363 - cjgillot:unused-crate, r=WaffleLapkin
Move the unused extern crate check back to the resolver. It doesn't have anything to do in `rustc_hir_typeck`.
2 parents 2375d7f + 40bde99 commit 660f184

File tree

13 files changed

+186
-196
lines changed

13 files changed

+186
-196
lines changed
 

‎compiler/rustc_hir_analysis/locales/en-US.ftl‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,6 @@ hir_analysis_manual_implementation =
6262
6363
hir_analysis_substs_on_overridden_impl = could not resolve substs on overridden impl
6464
65-
hir_analysis_unused_extern_crate =
66-
unused extern crate
67-
.suggestion = remove it
68-
69-
hir_analysis_extern_crate_not_idiomatic =
70-
`extern crate` is not idiomatic in the new edition
71-
.suggestion = convert it to a `{$msg_code}`
72-
7365
hir_analysis_trait_object_declared_with_no_traits =
7466
at least one trait is required for an object type
7567
.alias_span = this alias does not contain a trait
Lines changed: 1 addition & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
use crate::errors::{ExternCrateNotIdiomatic, UnusedExternCrate};
2-
use rustc_data_structures::fx::FxHashMap;
31
use rustc_data_structures::unord::UnordSet;
4-
use rustc_hir as hir;
52
use rustc_hir::def::DefKind;
6-
use rustc_hir::def_id::{DefId, LocalDefId};
3+
use rustc_hir::def_id::LocalDefId;
74
use rustc_middle::ty::TyCtxt;
85
use rustc_session::lint;
9-
use rustc_span::{Span, Symbol};
106

117
pub fn check_crate(tcx: TyCtxt<'_>) {
128
let mut used_trait_imports: UnordSet<LocalDefId> = Default::default();
@@ -43,131 +39,4 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
4339
|lint| lint,
4440
);
4541
}
46-
47-
unused_crates_lint(tcx);
48-
}
49-
50-
fn unused_crates_lint(tcx: TyCtxt<'_>) {
51-
let lint = lint::builtin::UNUSED_EXTERN_CRATES;
52-
53-
// Collect first the crates that are completely unused. These we
54-
// can always suggest removing (no matter which edition we are
55-
// in).
56-
let unused_extern_crates: FxHashMap<LocalDefId, Span> = tcx
57-
.maybe_unused_extern_crates(())
58-
.iter()
59-
.filter(|&&(def_id, _)| {
60-
tcx.extern_mod_stmt_cnum(def_id).map_or(true, |cnum| {
61-
!tcx.is_compiler_builtins(cnum)
62-
&& !tcx.is_panic_runtime(cnum)
63-
&& !tcx.has_global_allocator(cnum)
64-
&& !tcx.has_panic_handler(cnum)
65-
})
66-
})
67-
.cloned()
68-
.collect();
69-
70-
// Collect all the extern crates (in a reliable order).
71-
let mut crates_to_lint = vec![];
72-
73-
for id in tcx.hir().items() {
74-
if matches!(tcx.def_kind(id.owner_id), DefKind::ExternCrate) {
75-
let item = tcx.hir().item(id);
76-
if let hir::ItemKind::ExternCrate(orig_name) = item.kind {
77-
crates_to_lint.push(ExternCrateToLint {
78-
def_id: item.owner_id.to_def_id(),
79-
span: item.span,
80-
orig_name,
81-
warn_if_unused: !item.ident.as_str().starts_with('_'),
82-
});
83-
}
84-
}
85-
}
86-
87-
let extern_prelude = &tcx.resolutions(()).extern_prelude;
88-
89-
for extern_crate in &crates_to_lint {
90-
let def_id = extern_crate.def_id.expect_local();
91-
let item = tcx.hir().expect_item(def_id);
92-
93-
// If the crate is fully unused, we suggest removing it altogether.
94-
// We do this in any edition.
95-
if extern_crate.warn_if_unused {
96-
if let Some(&span) = unused_extern_crates.get(&def_id) {
97-
// Removal suggestion span needs to include attributes (Issue #54400)
98-
let id = tcx.hir().local_def_id_to_hir_id(def_id);
99-
let span_with_attrs = tcx
100-
.hir()
101-
.attrs(id)
102-
.iter()
103-
.map(|attr| attr.span)
104-
.fold(span, |acc, attr_span| acc.to(attr_span));
105-
106-
tcx.emit_spanned_lint(lint, id, span, UnusedExternCrate { span: span_with_attrs });
107-
continue;
108-
}
109-
}
110-
111-
// If we are not in Rust 2018 edition, then we don't make any further
112-
// suggestions.
113-
if !tcx.sess.rust_2018() {
114-
continue;
115-
}
116-
117-
// If the extern crate isn't in the extern prelude,
118-
// there is no way it can be written as a `use`.
119-
let orig_name = extern_crate.orig_name.unwrap_or(item.ident.name);
120-
if !extern_prelude.get(&orig_name).map_or(false, |from_item| !from_item) {
121-
continue;
122-
}
123-
124-
// If the extern crate is renamed, then we cannot suggest replacing it with a use as this
125-
// would not insert the new name into the prelude, where other imports in the crate may be
126-
// expecting it.
127-
if extern_crate.orig_name.is_some() {
128-
continue;
129-
}
130-
131-
let id = tcx.hir().local_def_id_to_hir_id(def_id);
132-
// If the extern crate has any attributes, they may have funky
133-
// semantics we can't faithfully represent using `use` (most
134-
// notably `#[macro_use]`). Ignore it.
135-
if !tcx.hir().attrs(id).is_empty() {
136-
continue;
137-
}
138-
139-
let base_replacement = match extern_crate.orig_name {
140-
Some(orig_name) => format!("use {} as {};", orig_name, item.ident.name),
141-
None => format!("use {};", item.ident.name),
142-
};
143-
let vis = tcx.sess.source_map().span_to_snippet(item.vis_span).unwrap_or_default();
144-
let add_vis = |to| if vis.is_empty() { to } else { format!("{} {}", vis, to) };
145-
tcx.emit_spanned_lint(
146-
lint,
147-
id,
148-
extern_crate.span,
149-
ExternCrateNotIdiomatic {
150-
span: extern_crate.span,
151-
msg_code: add_vis("use".to_string()),
152-
suggestion_code: add_vis(base_replacement),
153-
},
154-
);
155-
}
156-
}
157-
158-
struct ExternCrateToLint {
159-
/// `DefId` of the extern crate
160-
def_id: DefId,
161-
162-
/// span from the item
163-
span: Span,
164-
165-
/// if `Some`, then this is renamed (`extern crate orig_name as
166-
/// crate_name`), and -- perhaps surprisingly -- this stores the
167-
/// *original* name (`item.name` will contain the new name)
168-
orig_name: Option<Symbol>,
169-
170-
/// if `false`, the original name started with `_`, so we shouldn't lint
171-
/// about it going unused (but we should still emit idiom lints).
172-
warn_if_unused: bool,
17342
}

‎compiler/rustc_hir_analysis/src/errors.rs‎

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_errors::{
55
error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, Handler, IntoDiagnostic,
66
MultiSpan,
77
};
8-
use rustc_macros::{Diagnostic, LintDiagnostic};
8+
use rustc_macros::Diagnostic;
99
use rustc_middle::ty::Ty;
1010
use rustc_span::{symbol::Ident, Span, Symbol};
1111

@@ -247,26 +247,6 @@ pub struct SubstsOnOverriddenImpl {
247247
pub span: Span,
248248
}
249249

250-
#[derive(LintDiagnostic)]
251-
#[diag(hir_analysis_unused_extern_crate)]
252-
pub struct UnusedExternCrate {
253-
#[suggestion(applicability = "machine-applicable", code = "")]
254-
pub span: Span,
255-
}
256-
257-
#[derive(LintDiagnostic)]
258-
#[diag(hir_analysis_extern_crate_not_idiomatic)]
259-
pub struct ExternCrateNotIdiomatic {
260-
#[suggestion(
261-
style = "short",
262-
applicability = "machine-applicable",
263-
code = "{suggestion_code}"
264-
)]
265-
pub span: Span,
266-
pub msg_code: String,
267-
pub suggestion_code: String,
268-
}
269-
270250
#[derive(Diagnostic)]
271251
#[diag(hir_analysis_const_impl_for_non_const_trait)]
272252
pub struct ConstImplForNonConstTrait {

‎compiler/rustc_lint/src/context.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,23 @@ pub trait LintContext: Sized {
893893
BuiltinLintDiagnostics::ByteSliceInPackedStructWithDerive => {
894894
db.help("consider implementing the trait by hand, or remove the `packed` attribute");
895895
}
896+
BuiltinLintDiagnostics::UnusedExternCrate { removal_span }=> {
897+
db.span_suggestion(
898+
removal_span,
899+
"remove it",
900+
"",
901+
Applicability::MachineApplicable,
902+
);
903+
}
904+
BuiltinLintDiagnostics::ExternCrateNotIdiomatic { vis_span, ident_span }=> {
905+
let suggestion_span = vis_span.between(ident_span);
906+
db.span_suggestion_verbose(
907+
suggestion_span,
908+
"convert it to a `use`",
909+
if vis_span.is_empty() { "use " } else { " use " },
910+
Applicability::MachineApplicable,
911+
);
912+
}
896913
}
897914
// Rewrap `db`, and pass control to the user.
898915
decorate(db)

‎compiler/rustc_lint_defs/src/lib.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,13 @@ pub enum BuiltinLintDiagnostics {
522522
is_formatting_arg: bool,
523523
},
524524
ByteSliceInPackedStructWithDerive,
525+
UnusedExternCrate {
526+
removal_span: Span,
527+
},
528+
ExternCrateNotIdiomatic {
529+
vis_span: Span,
530+
ident_span: Span,
531+
},
525532
}
526533

527534
/// Lints that are buffered up early on in the `Session` before the

‎compiler/rustc_middle/src/query/mod.rs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,9 +1830,6 @@ rustc_queries! {
18301830
query maybe_unused_trait_imports(_: ()) -> &'tcx FxIndexSet<LocalDefId> {
18311831
desc { "fetching potentially unused trait imports" }
18321832
}
1833-
query maybe_unused_extern_crates(_: ()) -> &'tcx [(LocalDefId, Span)] {
1834-
desc { "looking up all possibly unused extern crates" }
1835-
}
18361833
query names_imported_by_glob_use(def_id: LocalDefId) -> &'tcx FxHashSet<Symbol> {
18371834
desc { |tcx| "finding names imported by glob use for `{}`", tcx.def_path_str(def_id.to_def_id()) }
18381835
}

‎compiler/rustc_middle/src/ty/context.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,8 +2487,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
24872487
|tcx, id| tcx.resolutions(()).reexport_map.get(&id).map(|v| &v[..]);
24882488
providers.maybe_unused_trait_imports =
24892489
|tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports;
2490-
providers.maybe_unused_extern_crates =
2491-
|tcx, ()| &tcx.resolutions(()).maybe_unused_extern_crates[..];
24922490
providers.names_imported_by_glob_use = |tcx, id| {
24932491
tcx.arena.alloc(tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default())
24942492
};

‎compiler/rustc_middle/src/ty/mod.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,8 @@ pub struct ResolverGlobalCtxt {
165165
pub effective_visibilities: EffectiveVisibilities,
166166
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
167167
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
168-
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
169168
pub reexport_map: FxHashMap<LocalDefId, Vec<ModChild>>,
170169
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
171-
/// Extern prelude entries. The value is `true` if the entry was introduced
172-
/// via `extern crate` item and not `--extern` option or compiler built-in.
173-
pub extern_prelude: FxHashMap<Symbol, bool>,
174170
pub main_def: Option<MainDefinition>,
175171
pub trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,
176172
/// A list of proc macro LocalDefIds, written out in the order in which

‎compiler/rustc_resolve/src/check_unused.rs‎

Lines changed: 121 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ use crate::Resolver;
2929

3030
use rustc_ast as ast;
3131
use rustc_ast::visit::{self, Visitor};
32-
use rustc_data_structures::fx::FxIndexMap;
32+
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
3333
use rustc_data_structures::unord::UnordSet;
3434
use rustc_errors::{pluralize, MultiSpan};
35-
use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_IMPORTS};
35+
use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES, UNUSED_IMPORTS};
3636
use rustc_session::lint::BuiltinLintDiagnostics;
37+
use rustc_span::symbol::Ident;
3738
use rustc_span::{Span, DUMMY_SP};
3839

3940
struct UnusedImport<'a> {
@@ -53,11 +54,28 @@ struct UnusedImportCheckVisitor<'a, 'b, 'tcx> {
5354
r: &'a mut Resolver<'b, 'tcx>,
5455
/// All the (so far) unused imports, grouped path list
5556
unused_imports: FxIndexMap<ast::NodeId, UnusedImport<'a>>,
57+
extern_crate_items: Vec<ExternCrateToLint>,
5658
base_use_tree: Option<&'a ast::UseTree>,
5759
base_id: ast::NodeId,
5860
item_span: Span,
5961
}
6062

63+
struct ExternCrateToLint {
64+
id: ast::NodeId,
65+
/// Span from the item
66+
span: Span,
67+
/// Span to use to suggest complete removal.
68+
span_with_attributes: Span,
69+
/// Span of the visibility, if any.
70+
vis_span: Span,
71+
/// Whether the item has attrs.
72+
has_attrs: bool,
73+
/// Name used to refer to the crate.
74+
ident: Ident,
75+
/// Whether the statement renames the crate `extern crate orig_name as new_name;`.
76+
renames: bool,
77+
}
78+
6179
impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
6280
// We have information about whether `use` (import) items are actually
6381
// used now. If an import is not used at all, we signal a lint error.
@@ -96,18 +114,27 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
96114

97115
impl<'a, 'b, 'tcx> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b, 'tcx> {
98116
fn visit_item(&mut self, item: &'a ast::Item) {
99-
self.item_span = item.span_with_attributes();
100-
101-
// Ignore is_public import statements because there's no way to be sure
102-
// whether they're used or not. Also ignore imports with a dummy span
103-
// because this means that they were generated in some fashion by the
104-
// compiler and we don't need to consider them.
105-
if let ast::ItemKind::Use(..) = item.kind {
106-
if item.vis.kind.is_pub() || item.span.is_dummy() {
107-
return;
117+
match item.kind {
118+
// Ignore is_public import statements because there's no way to be sure
119+
// whether they're used or not. Also ignore imports with a dummy span
120+
// because this means that they were generated in some fashion by the
121+
// compiler and we don't need to consider them.
122+
ast::ItemKind::Use(..) if item.vis.kind.is_pub() || item.span.is_dummy() => return,
123+
ast::ItemKind::ExternCrate(orig_name) => {
124+
self.extern_crate_items.push(ExternCrateToLint {
125+
id: item.id,
126+
span: item.span,
127+
vis_span: item.vis.span,
128+
span_with_attributes: item.span_with_attributes(),
129+
has_attrs: !item.attrs.is_empty(),
130+
ident: item.ident,
131+
renames: orig_name.is_some(),
132+
});
108133
}
134+
_ => {}
109135
}
110136

137+
self.item_span = item.span_with_attributes();
111138
visit::walk_item(self, item);
112139
}
113140

@@ -224,6 +251,9 @@ fn calc_unused_spans(
224251

225252
impl Resolver<'_, '_> {
226253
pub(crate) fn check_unused(&mut self, krate: &ast::Crate) {
254+
let tcx = self.tcx;
255+
let mut maybe_unused_extern_crates = FxHashMap::default();
256+
227257
for import in self.potentially_unused_imports.iter() {
228258
match import.kind {
229259
_ if import.used.get()
@@ -246,7 +276,14 @@ impl Resolver<'_, '_> {
246276
}
247277
ImportKind::ExternCrate { id, .. } => {
248278
let def_id = self.local_def_id(id);
249-
self.maybe_unused_extern_crates.push((def_id, import.span));
279+
if self.extern_crate_map.get(&def_id).map_or(true, |&cnum| {
280+
!tcx.is_compiler_builtins(cnum)
281+
&& !tcx.is_panic_runtime(cnum)
282+
&& !tcx.has_global_allocator(cnum)
283+
&& !tcx.has_panic_handler(cnum)
284+
}) {
285+
maybe_unused_extern_crates.insert(id, import.span);
286+
}
250287
}
251288
ImportKind::MacroUse => {
252289
let msg = "unused `#[macro_use]` import";
@@ -259,6 +296,7 @@ impl Resolver<'_, '_> {
259296
let mut visitor = UnusedImportCheckVisitor {
260297
r: self,
261298
unused_imports: Default::default(),
299+
extern_crate_items: Default::default(),
262300
base_use_tree: None,
263301
base_id: ast::DUMMY_NODE_ID,
264302
item_span: DUMMY_SP,
@@ -290,7 +328,7 @@ impl Resolver<'_, '_> {
290328
let ms = MultiSpan::from_spans(spans.clone());
291329
let mut span_snippets = spans
292330
.iter()
293-
.filter_map(|s| match visitor.r.tcx.sess.source_map().span_to_snippet(*s) {
331+
.filter_map(|s| match tcx.sess.source_map().span_to_snippet(*s) {
294332
Ok(s) => Some(format!("`{}`", s)),
295333
_ => None,
296334
})
@@ -317,7 +355,7 @@ impl Resolver<'_, '_> {
317355
// If we are in the `--test` mode, suppress a help that adds the `#[cfg(test)]`
318356
// attribute; however, if not, suggest adding the attribute. There is no way to
319357
// retrieve attributes here because we do not have a `TyCtxt` yet.
320-
let test_module_span = if visitor.r.tcx.sess.opts.test {
358+
let test_module_span = if tcx.sess.opts.test {
321359
None
322360
} else {
323361
let parent_module = visitor.r.get_nearest_non_block_module(
@@ -346,5 +384,74 @@ impl Resolver<'_, '_> {
346384
BuiltinLintDiagnostics::UnusedImports(fix_msg.into(), fixes, test_module_span),
347385
);
348386
}
387+
388+
for extern_crate in visitor.extern_crate_items {
389+
let warn_if_unused = !extern_crate.ident.name.as_str().starts_with('_');
390+
391+
// If the crate is fully unused, we suggest removing it altogether.
392+
// We do this in any edition.
393+
if warn_if_unused {
394+
if let Some(&span) = maybe_unused_extern_crates.get(&extern_crate.id) {
395+
visitor.r.lint_buffer.buffer_lint_with_diagnostic(
396+
UNUSED_EXTERN_CRATES,
397+
extern_crate.id,
398+
span,
399+
"unused extern crate",
400+
BuiltinLintDiagnostics::UnusedExternCrate {
401+
removal_span: extern_crate.span_with_attributes,
402+
},
403+
);
404+
continue;
405+
}
406+
}
407+
408+
// If we are not in Rust 2018 edition, then we don't make any further
409+
// suggestions.
410+
if !tcx.sess.rust_2018() {
411+
continue;
412+
}
413+
414+
// If the extern crate has any attributes, they may have funky
415+
// semantics we can't faithfully represent using `use` (most
416+
// notably `#[macro_use]`). Ignore it.
417+
if extern_crate.has_attrs {
418+
continue;
419+
}
420+
421+
// If the extern crate is renamed, then we cannot suggest replacing it with a use as this
422+
// would not insert the new name into the prelude, where other imports in the crate may be
423+
// expecting it.
424+
if extern_crate.renames {
425+
continue;
426+
}
427+
428+
// If the extern crate isn't in the extern prelude,
429+
// there is no way it can be written as a `use`.
430+
if !visitor
431+
.r
432+
.extern_prelude
433+
.get(&extern_crate.ident)
434+
.map_or(false, |entry| !entry.introduced_by_item)
435+
{
436+
continue;
437+
}
438+
439+
let vis_span = extern_crate
440+
.vis_span
441+
.find_ancestor_inside(extern_crate.span)
442+
.unwrap_or(extern_crate.vis_span);
443+
let ident_span = extern_crate
444+
.ident
445+
.span
446+
.find_ancestor_inside(extern_crate.span)
447+
.unwrap_or(extern_crate.ident.span);
448+
visitor.r.lint_buffer.buffer_lint_with_diagnostic(
449+
UNUSED_EXTERN_CRATES,
450+
extern_crate.id,
451+
extern_crate.span,
452+
"`extern crate` is not idiomatic in the new edition",
453+
BuiltinLintDiagnostics::ExternCrateNotIdiomatic { vis_span, ident_span },
454+
);
455+
}
349456
}
350457
}

‎compiler/rustc_resolve/src/lib.rs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,6 @@ pub struct Resolver<'a, 'tcx> {
946946
has_pub_restricted: bool,
947947
used_imports: FxHashSet<NodeId>,
948948
maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
949-
maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
950949

951950
/// Privacy errors are delayed until the end in order to deduplicate them.
952951
privacy_errors: Vec<PrivacyError<'a>>,
@@ -1284,7 +1283,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12841283
has_pub_restricted: false,
12851284
used_imports: FxHashSet::default(),
12861285
maybe_unused_trait_imports: Default::default(),
1287-
maybe_unused_extern_crates: Vec::new(),
12881286

12891287
privacy_errors: Vec::new(),
12901288
ambiguity_errors: Vec::new(),
@@ -1400,7 +1398,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14001398
let extern_crate_map = self.extern_crate_map;
14011399
let reexport_map = self.reexport_map;
14021400
let maybe_unused_trait_imports = self.maybe_unused_trait_imports;
1403-
let maybe_unused_extern_crates = self.maybe_unused_extern_crates;
14041401
let glob_map = self.glob_map;
14051402
let main_def = self.main_def;
14061403
let confused_type_with_std_module = self.confused_type_with_std_module;
@@ -1414,12 +1411,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14141411
reexport_map,
14151412
glob_map,
14161413
maybe_unused_trait_imports,
1417-
maybe_unused_extern_crates,
1418-
extern_prelude: self
1419-
.extern_prelude
1420-
.iter()
1421-
.map(|(ident, entry)| (ident.name, entry.introduced_by_item))
1422-
.collect(),
14231414
main_def,
14241415
trait_impls: self.trait_impls,
14251416
proc_macros,

‎tests/ui/rust-2018/remove-extern-crate.fixed‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern crate alloc;
2323
fn main() {
2424
another_name::mem::drop(3);
2525
another::foo();
26+
with_visibility::foo();
2627
remove_extern_crate::foo!();
2728
bar!();
2829
alloc::vec![5];
@@ -37,3 +38,12 @@ mod another {
3738
remove_extern_crate::foo!();
3839
}
3940
}
41+
42+
mod with_visibility {
43+
pub use core; //~ WARNING `extern crate` is not idiomatic
44+
45+
pub fn foo() {
46+
core::mem::drop(4);
47+
remove_extern_crate::foo!();
48+
}
49+
}

‎tests/ui/rust-2018/remove-extern-crate.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern crate alloc;
2323
fn main() {
2424
another_name::mem::drop(3);
2525
another::foo();
26+
with_visibility::foo();
2627
remove_extern_crate::foo!();
2728
bar!();
2829
alloc::vec![5];
@@ -37,3 +38,12 @@ mod another {
3738
remove_extern_crate::foo!();
3839
}
3940
}
41+
42+
mod with_visibility {
43+
pub extern crate core; //~ WARNING `extern crate` is not idiomatic
44+
45+
pub fn foo() {
46+
core::mem::drop(4);
47+
remove_extern_crate::foo!();
48+
}
49+
}

‎tests/ui/rust-2018/remove-extern-crate.stderr‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,26 @@ LL | #![warn(rust_2018_idioms)]
1212
= note: `#[warn(unused_extern_crates)]` implied by `#[warn(rust_2018_idioms)]`
1313

1414
warning: `extern crate` is not idiomatic in the new edition
15-
--> $DIR/remove-extern-crate.rs:32:5
15+
--> $DIR/remove-extern-crate.rs:33:5
1616
|
1717
LL | extern crate core;
18-
| ^^^^^^^^^^^^^^^^^^ help: convert it to a `use`
18+
| ^^^^^^^^^^^^^^^^^^
19+
|
20+
help: convert it to a `use`
21+
|
22+
LL | use core;
23+
| ~~~
24+
25+
warning: `extern crate` is not idiomatic in the new edition
26+
--> $DIR/remove-extern-crate.rs:43:5
27+
|
28+
LL | pub extern crate core;
29+
| ^^^^^^^^^^^^^^^^^^^^^^
30+
|
31+
help: convert it to a `use`
32+
|
33+
LL | pub use core;
34+
| ~~~
1935

20-
warning: 2 warnings emitted
36+
warning: 3 warnings emitted
2137

0 commit comments

Comments
 (0)
Please sign in to comment.