Skip to content

Commit 6985c4b

Browse files
committed
Fix redundant import errors for preload extern crate
1 parent 0decdac commit 6985c4b

11 files changed

+107
-3
lines changed

compiler/rustc_lint/src/context/diagnostics.rs

+3
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiagnostics, diag:
114114
diag.span_label(span, format!("the item `{ident}` is already {introduced} here"));
115115
}
116116
}
117+
BuiltinLintDiagnostics::RedundantImportRemove(span) => {
118+
diag.span_suggestion(span, "remove this import", "", Applicability::MachineApplicable);
119+
}
117120
BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span) => {
118121
stability::deprecation_suggestion(diag, "macro", suggestion, span)
119122
}

compiler/rustc_lint_defs/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ pub enum BuiltinLintDiagnostics {
578578
UnknownCrateTypes(Span, String, String),
579579
UnusedImports(String, Vec<(Span, String)>, Option<Span>),
580580
RedundantImport(Vec<(Span, bool)>, Ident),
581+
RedundantImportRemove(Span),
581582
DeprecatedMacro(Option<Symbol>, Span),
582583
MissingAbi(Span, Abi),
583584
UnusedDocComment(Span),

compiler/rustc_resolve/src/imports.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13361336
let mut is_redundant = true;
13371337

13381338
let mut redundant_span = PerNS { value_ns: None, type_ns: None, macro_ns: None };
1339-
13401339
self.per_ns(|this, ns| {
13411340
if is_redundant && let Ok(binding) = source_bindings[ns].get() {
13421341
if binding.res() == Res::Err {
@@ -1368,12 +1367,24 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13681367
let mut redundant_spans: Vec<_> = redundant_span.present_items().collect();
13691368
redundant_spans.sort();
13701369
redundant_spans.dedup();
1370+
let is_preload = self.extern_prelude_get(target, false);
1371+
let (msg, diag) = if is_preload.is_some() {
1372+
(
1373+
"already exists in the extern prelude",
1374+
BuiltinLintDiagnostics::RedundantImportRemove(import.use_span),
1375+
)
1376+
} else {
1377+
(
1378+
"imported redundantly",
1379+
BuiltinLintDiagnostics::RedundantImport(redundant_spans, source),
1380+
)
1381+
};
13711382
self.lint_buffer.buffer_lint_with_diagnostic(
13721383
UNUSED_IMPORTS,
13731384
id,
13741385
import.span,
1375-
format!("the item `{source}` is imported redundantly"),
1376-
BuiltinLintDiagnostics::RedundantImport(redundant_spans, source),
1386+
format!("the item `{source}` is {msg}"),
1387+
diag,
13771388
);
13781389
}
13791390
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn item() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the item `aux_issue_121915` is already exists in the extern prelude
2+
--> $DIR/redundant-import-issue-121915-2015.rs:10:9
3+
|
4+
LL | use aux_issue_121915;
5+
| ^^^^^^^^^^^^^^^^ help: remove this import
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/redundant-import-issue-121915-2015.rs:8:8
9+
|
10+
LL | #[deny(unused_imports)]
11+
| ^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ compile-flags: --extern aux_issue_121915 --edition 2015
2+
//@ aux-build: aux-issue-121915.rs
3+
4+
extern crate aux_issue_121915;
5+
6+
#[deny(unused_imports)]
7+
fn main() {
8+
use aux_issue_121915;
9+
//~^ ERROR the item `aux_issue_121915` is already exists in the extern prelude
10+
aux_issue_121915::item();
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the item `aux_issue_121915` is already exists in the extern prelude
2+
--> $DIR/redundant-import-issue-121915-2015.rs:8:9
3+
|
4+
LL | use aux_issue_121915;
5+
| ----^^^^^^^^^^^^^^^^- help: remove this import
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/redundant-import-issue-121915-2015.rs:6:8
9+
|
10+
LL | #[deny(unused_imports)]
11+
| ^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0432]: unresolved import `aux_issue_121915`
2+
--> $DIR/redundant-import-issue-121915.rs:10:9
3+
|
4+
LL | use aux_issue_121915;
5+
| ^^^^^^^^^^^^^^^^ no `aux_issue_121915` in the root
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0432`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the item `aux_issue_121915` is already exists in the extern prelude
2+
--> $DIR/redundant-import-issue-121915.rs:9:9
3+
|
4+
LL | use aux_issue_121915;
5+
| ----^^^^^^^^^^^^^^^^- help: remove this import
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/redundant-import-issue-121915.rs:7:8
9+
|
10+
LL | #[deny(unused_imports)]
11+
| ^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the item `aux_issue_121915` is already exists in the extern prelude
2+
--> $DIR/redundant-import-issue-121915.rs:9:9
3+
|
4+
LL | use aux_issue_121915;
5+
| ----^^^^^^^^^^^^^^^^- help: remove this import
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/redundant-import-issue-121915.rs:7:8
9+
|
10+
LL | #[deny(unused_imports)]
11+
| ^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ revisions: edition2018 edition2021
2+
//@ [edition2018] edition:2018
3+
//@ [edition2021] edition:2021
4+
//@ compile-flags: --extern aux_issue_121915
5+
//@ aux-build: aux-issue-121915.rs
6+
7+
#[deny(unused_imports)]
8+
fn main() {
9+
use aux_issue_121915;
10+
//~^ ERROR the item `aux_issue_121915` is already exists in the extern prelude
11+
aux_issue_121915::item();
12+
}

0 commit comments

Comments
 (0)