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 23f52db

Browse files
committedMar 5, 2024
Fix redundant import errors for preload extern crate
1 parent 0decdac commit 23f52db

17 files changed

+124
-27
lines changed
 

‎compiler/rustc_lint/src/context/diagnostics.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,21 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiagnostics, diag:
108108
);
109109
}
110110
}
111-
BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
111+
BuiltinLintDiagnostics::RedundantImport(spans, ident, import_span) => {
112112
for (span, is_imported) in spans {
113113
let introduced = if is_imported { "imported" } else { "defined" };
114-
diag.span_label(span, format!("the item `{ident}` is already {introduced} here"));
114+
let span_msg = if span.is_dummy() { "by prelude" } else { "here" };
115+
diag.span_label(
116+
span,
117+
format!("the item `{ident}` is already {introduced} {span_msg}"),
118+
);
115119
}
120+
diag.span_suggestion(
121+
import_span,
122+
"remove this import",
123+
"",
124+
Applicability::MachineApplicable,
125+
);
116126
}
117127
BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span) => {
118128
stability::deprecation_suggestion(diag, "macro", suggestion, span)

‎compiler/rustc_lint_defs/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ pub enum BuiltinLintDiagnostics {
577577
ElidedLifetimesInPaths(usize, Span, bool, Span),
578578
UnknownCrateTypes(Span, String, String),
579579
UnusedImports(String, Vec<(Span, String)>, Option<Span>),
580-
RedundantImport(Vec<(Span, bool)>, Ident),
580+
RedundantImport(Vec<(Span, bool)>, Ident, Span),
581581
DeprecatedMacro(Option<Symbol>, Span),
582582
MissingAbi(Span, Abi),
583583
UnusedDocComment(Span),

‎compiler/rustc_resolve/src/imports.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1334,9 +1334,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13341334
}
13351335

13361336
let mut is_redundant = true;
1337-
13381337
let mut redundant_span = PerNS { value_ns: None, type_ns: None, macro_ns: None };
1339-
13401338
self.per_ns(|this, ns| {
13411339
if is_redundant && let Ok(binding) = source_bindings[ns].get() {
13421340
if binding.res() == Res::Err {
@@ -1371,9 +1369,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13711369
self.lint_buffer.buffer_lint_with_diagnostic(
13721370
UNUSED_IMPORTS,
13731371
id,
1374-
import.span,
1372+
import.use_span,
13751373
format!("the item `{source}` is imported redundantly"),
1376-
BuiltinLintDiagnostics::RedundantImport(redundant_spans, source),
1374+
BuiltinLintDiagnostics::RedundantImport(redundant_spans, source, import.use_span),
13771375
);
13781376
}
13791377
}
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,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 imported redundantly
10+
aux_issue_121915::item();
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: the item `aux_issue_121915` is imported redundantly
2+
--> $DIR/redundant-import-issue-121915-2015.rs:8:5
3+
|
4+
LL | extern crate aux_issue_121915;
5+
| ------------------------------ the item `aux_issue_121915` is already imported here
6+
...
7+
LL | use aux_issue_121915;
8+
| ^^^^^^^^^^^^^^^^^^^^^ help: remove this import
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/redundant-import-issue-121915-2015.rs:6:8
12+
|
13+
LL | #[deny(unused_imports)]
14+
| ^^^^^^^^^^^^^^
15+
16+
error: aborting due to 1 previous error
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ compile-flags: --extern aux_issue_121915 --edition 2018
2+
//@ aux-build: aux-issue-121915.rs
3+
4+
#[deny(unused_imports)]
5+
fn main() {
6+
use aux_issue_121915;
7+
//~^ ERROR the item `aux_issue_121915` is imported redundantly
8+
aux_issue_121915::item();
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: the item `aux_issue_121915` is imported redundantly
2+
--> $DIR/redundant-import-issue-121915.rs:6:5
3+
|
4+
LL | use aux_issue_121915;
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| the item `aux_issue_121915` is already defined by prelude
8+
| help: remove this import
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/redundant-import-issue-121915.rs:4:8
12+
|
13+
LL | #[deny(unused_imports)]
14+
| ^^^^^^^^^^^^^^
15+
16+
error: aborting due to 1 previous error
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ run-rustfix
2+
//@ compile-flags: --edition 2021
3+
#[deny(unused_imports)]
4+
//~^ ERROR the item `TryFrom` is imported redundantly
5+
6+
fn main() {
7+
let _ = u32::try_from(5i32);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ run-rustfix
2+
//@ compile-flags: --edition 2021
3+
#[deny(unused_imports)]
4+
use std::convert::TryFrom;
5+
//~^ ERROR the item `TryFrom` is imported redundantly
6+
7+
fn main() {
8+
let _ = u32::try_from(5i32);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: the item `TryFrom` is imported redundantly
2+
--> $DIR/suggest-remove-issue-121315.rs:4:1
3+
|
4+
LL | use std::convert::TryFrom;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
6+
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
7+
|
8+
= note: the item `TryFrom` is already defined here
9+
|
10+
note: the lint level is defined here
11+
--> $DIR/suggest-remove-issue-121315.rs:3:8
12+
|
13+
LL | #[deny(unused_imports)]
14+
| ^^^^^^^^^^^^^^
15+
16+
error: aborting due to 1 previous error
17+

‎tests/ui/lint/unused/issue-59896.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: the item `S` is imported redundantly
2-
--> $DIR/issue-59896.rs:6:9
2+
--> $DIR/issue-59896.rs:6:5
33
|
44
LL | struct S;
55
| --------- the item `S` is already defined here
66
...
77
LL | use S;
8-
| ^
8+
| ^^^^^^ help: remove this import
99
|
1010
note: the lint level is defined here
1111
--> $DIR/issue-59896.rs:1:9

‎tests/ui/lint/use-redundant/use-redundant-glob-parent.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
warning: the item `Foo` is imported redundantly
2-
--> $DIR/use-redundant-glob-parent.rs:12:9
2+
--> $DIR/use-redundant-glob-parent.rs:12:5
33
|
44
LL | use bar::*;
55
| ------ the item `Foo` is already imported here
66
...
77
LL | use bar::Foo;
8-
| ^^^^^^^^
8+
| ^^^^^^^^^^^^^ help: remove this import
99
|
1010
note: the lint level is defined here
1111
--> $DIR/use-redundant-glob-parent.rs:2:9

‎tests/ui/lint/use-redundant/use-redundant-glob.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
warning: the item `Foo` is imported redundantly
2-
--> $DIR/use-redundant-glob.rs:11:9
2+
--> $DIR/use-redundant-glob.rs:11:5
33
|
44
LL | use bar::*;
55
| ------ the item `Foo` is already imported here
66
LL | use bar::Foo;
7-
| ^^^^^^^^
7+
| ^^^^^^^^^^^^^ help: remove this import
88
|
99
note: the lint level is defined here
1010
--> $DIR/use-redundant-glob.rs:2:9

‎tests/ui/lint/use-redundant/use-redundant-prelude-rust-2015.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: the item `Some` is imported redundantly
2-
--> $DIR/use-redundant-prelude-rust-2015.rs:5:5
2+
--> $DIR/use-redundant-prelude-rust-2015.rs:5:1
33
|
44
LL | use std::option::Option::Some;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
66
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
77
|
88
= note: the item `Some` is already defined here
@@ -14,28 +14,28 @@ LL | #![warn(unused_imports)]
1414
| ^^^^^^^^^^^^^^
1515

1616
warning: the item `None` is imported redundantly
17-
--> $DIR/use-redundant-prelude-rust-2015.rs:6:5
17+
--> $DIR/use-redundant-prelude-rust-2015.rs:6:1
1818
|
1919
LL | use std::option::Option::None;
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
2121
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
2222
|
2323
= note: the item `None` is already defined here
2424

2525
warning: the item `Ok` is imported redundantly
26-
--> $DIR/use-redundant-prelude-rust-2015.rs:8:5
26+
--> $DIR/use-redundant-prelude-rust-2015.rs:8:1
2727
|
2828
LL | use std::result::Result::Ok;
29-
| ^^^^^^^^^^^^^^^^^^^^^^^
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
3030
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
3131
|
3232
= note: the item `Ok` is already defined here
3333

3434
warning: the item `Err` is imported redundantly
35-
--> $DIR/use-redundant-prelude-rust-2015.rs:9:5
35+
--> $DIR/use-redundant-prelude-rust-2015.rs:9:1
3636
|
3737
LL | use std::result::Result::Err;
38-
| ^^^^^^^^^^^^^^^^^^^^^^^^
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
3939
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
4040
|
4141
= note: the item `Err` is already defined here

‎tests/ui/lint/use-redundant/use-redundant-prelude-rust-2021.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
warning: the item `TryFrom` is imported redundantly
2-
--> $DIR/use-redundant-prelude-rust-2021.rs:5:5
2+
--> $DIR/use-redundant-prelude-rust-2021.rs:5:1
33
|
44
LL | use std::convert::TryFrom;
5-
| ^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
66
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
77
|
88
= note: the item `TryFrom` is already defined here
@@ -14,10 +14,10 @@ LL | #![warn(unused_imports)]
1414
| ^^^^^^^^^^^^^^
1515

1616
warning: the item `TryInto` is imported redundantly
17-
--> $DIR/use-redundant-prelude-rust-2021.rs:6:5
17+
--> $DIR/use-redundant-prelude-rust-2021.rs:6:1
1818
|
1919
LL | use std::convert::TryInto;
20-
| ^^^^^^^^^^^^^^^^^^^^^
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
2121
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
2222
|
2323
= note: the item `TryInto` is already defined here

‎tests/ui/lint/use-redundant/use-redundant.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ LL | use m2::*;
1717
| ^^^^^
1818

1919
warning: the item `Bar` is imported redundantly
20-
--> $DIR/use-redundant.rs:21:9
20+
--> $DIR/use-redundant.rs:21:5
2121
|
2222
LL | use crate::foo::Bar;
2323
| --------------- the item `Bar` is already imported here
2424
...
2525
LL | use crate::foo::Bar;
26-
| ^^^^^^^^^^^^^^^
26+
| ^^^^^^^^^^^^^^^^^^^^ help: remove this import
2727

2828
warning: 3 warnings emitted
2929

0 commit comments

Comments
 (0)
Please sign in to comment.