Skip to content

Commit

Permalink
Fix redundant import errors for preload extern crate
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Mar 5, 2024
1 parent 0decdac commit 23f52db
Show file tree
Hide file tree
Showing 17 changed files with 124 additions and 27 deletions.
14 changes: 12 additions & 2 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,21 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiagnostics, diag:
);
}
}
BuiltinLintDiagnostics::RedundantImport(spans, ident) => {
BuiltinLintDiagnostics::RedundantImport(spans, ident, import_span) => {
for (span, is_imported) in spans {
let introduced = if is_imported { "imported" } else { "defined" };
diag.span_label(span, format!("the item `{ident}` is already {introduced} here"));
let span_msg = if span.is_dummy() { "by prelude" } else { "here" };
diag.span_label(
span,
format!("the item `{ident}` is already {introduced} {span_msg}"),
);
}
diag.span_suggestion(
import_span,
"remove this import",
"",
Applicability::MachineApplicable,
);
}
BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span) => {
stability::deprecation_suggestion(diag, "macro", suggestion, span)
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ pub enum BuiltinLintDiagnostics {
ElidedLifetimesInPaths(usize, Span, bool, Span),
UnknownCrateTypes(Span, String, String),
UnusedImports(String, Vec<(Span, String)>, Option<Span>),
RedundantImport(Vec<(Span, bool)>, Ident),
RedundantImport(Vec<(Span, bool)>, Ident, Span),
DeprecatedMacro(Option<Symbol>, Span),
MissingAbi(Span, Abi),
UnusedDocComment(Span),
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,9 +1334,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}

let mut is_redundant = true;

let mut redundant_span = PerNS { value_ns: None, type_ns: None, macro_ns: None };

self.per_ns(|this, ns| {
if is_redundant && let Ok(binding) = source_bindings[ns].get() {
if binding.res() == Res::Err {
Expand Down Expand Up @@ -1371,9 +1369,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
self.lint_buffer.buffer_lint_with_diagnostic(
UNUSED_IMPORTS,
id,
import.span,
import.use_span,
format!("the item `{source}` is imported redundantly"),
BuiltinLintDiagnostics::RedundantImport(redundant_spans, source),
BuiltinLintDiagnostics::RedundantImport(redundant_spans, source, import.use_span),
);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui/imports/auxiliary/aux-issue-121915.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn item() {}
11 changes: 11 additions & 0 deletions tests/ui/imports/redundant-import-issue-121915-2015.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ compile-flags: --extern aux_issue_121915 --edition 2015
//@ aux-build: aux-issue-121915.rs

extern crate aux_issue_121915;

#[deny(unused_imports)]
fn main() {
use aux_issue_121915;
//~^ ERROR the item `aux_issue_121915` is imported redundantly
aux_issue_121915::item();
}
17 changes: 17 additions & 0 deletions tests/ui/imports/redundant-import-issue-121915-2015.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: the item `aux_issue_121915` is imported redundantly
--> $DIR/redundant-import-issue-121915-2015.rs:8:5
|
LL | extern crate aux_issue_121915;
| ------------------------------ the item `aux_issue_121915` is already imported here
...
LL | use aux_issue_121915;
| ^^^^^^^^^^^^^^^^^^^^^ help: remove this import
|
note: the lint level is defined here
--> $DIR/redundant-import-issue-121915-2015.rs:6:8
|
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

9 changes: 9 additions & 0 deletions tests/ui/imports/redundant-import-issue-121915.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ compile-flags: --extern aux_issue_121915 --edition 2018
//@ aux-build: aux-issue-121915.rs

#[deny(unused_imports)]
fn main() {
use aux_issue_121915;
//~^ ERROR the item `aux_issue_121915` is imported redundantly
aux_issue_121915::item();
}
17 changes: 17 additions & 0 deletions tests/ui/imports/redundant-import-issue-121915.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: the item `aux_issue_121915` is imported redundantly
--> $DIR/redundant-import-issue-121915.rs:6:5
|
LL | use aux_issue_121915;
| ^^^^^^^^^^^^^^^^^^^^^
| |
| the item `aux_issue_121915` is already defined by prelude
| help: remove this import
|
note: the lint level is defined here
--> $DIR/redundant-import-issue-121915.rs:4:8
|
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

8 changes: 8 additions & 0 deletions tests/ui/imports/suggest-remove-issue-121315.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@ run-rustfix
//@ compile-flags: --edition 2021
#[deny(unused_imports)]
//~^ ERROR the item `TryFrom` is imported redundantly

fn main() {
let _ = u32::try_from(5i32);
}
9 changes: 9 additions & 0 deletions tests/ui/imports/suggest-remove-issue-121315.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ run-rustfix
//@ compile-flags: --edition 2021
#[deny(unused_imports)]
use std::convert::TryFrom;
//~^ ERROR the item `TryFrom` is imported redundantly

fn main() {
let _ = u32::try_from(5i32);
}
17 changes: 17 additions & 0 deletions tests/ui/imports/suggest-remove-issue-121315.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: the item `TryFrom` is imported redundantly
--> $DIR/suggest-remove-issue-121315.rs:4:1
|
LL | use std::convert::TryFrom;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
|
= note: the item `TryFrom` is already defined here
|
note: the lint level is defined here
--> $DIR/suggest-remove-issue-121315.rs:3:8
|
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

4 changes: 2 additions & 2 deletions tests/ui/lint/unused/issue-59896.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error: the item `S` is imported redundantly
--> $DIR/issue-59896.rs:6:9
--> $DIR/issue-59896.rs:6:5
|
LL | struct S;
| --------- the item `S` is already defined here
...
LL | use S;
| ^
| ^^^^^^ help: remove this import
|
note: the lint level is defined here
--> $DIR/issue-59896.rs:1:9
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/lint/use-redundant/use-redundant-glob-parent.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
warning: the item `Foo` is imported redundantly
--> $DIR/use-redundant-glob-parent.rs:12:9
--> $DIR/use-redundant-glob-parent.rs:12:5
|
LL | use bar::*;
| ------ the item `Foo` is already imported here
...
LL | use bar::Foo;
| ^^^^^^^^
| ^^^^^^^^^^^^^ help: remove this import
|
note: the lint level is defined here
--> $DIR/use-redundant-glob-parent.rs:2:9
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/lint/use-redundant/use-redundant-glob.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
warning: the item `Foo` is imported redundantly
--> $DIR/use-redundant-glob.rs:11:9
--> $DIR/use-redundant-glob.rs:11:5
|
LL | use bar::*;
| ------ the item `Foo` is already imported here
LL | use bar::Foo;
| ^^^^^^^^
| ^^^^^^^^^^^^^ help: remove this import
|
note: the lint level is defined here
--> $DIR/use-redundant-glob.rs:2:9
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
warning: the item `Some` is imported redundantly
--> $DIR/use-redundant-prelude-rust-2015.rs:5:5
--> $DIR/use-redundant-prelude-rust-2015.rs:5:1
|
LL | use std::option::Option::Some;
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
|
= note: the item `Some` is already defined here
Expand All @@ -14,28 +14,28 @@ LL | #![warn(unused_imports)]
| ^^^^^^^^^^^^^^

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

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

warning: the item `Err` is imported redundantly
--> $DIR/use-redundant-prelude-rust-2015.rs:9:5
--> $DIR/use-redundant-prelude-rust-2015.rs:9:1
|
LL | use std::result::Result::Err;
| ^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
|
= note: the item `Err` is already defined here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
warning: the item `TryFrom` is imported redundantly
--> $DIR/use-redundant-prelude-rust-2021.rs:5:5
--> $DIR/use-redundant-prelude-rust-2021.rs:5:1
|
LL | use std::convert::TryFrom;
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
|
= note: the item `TryFrom` is already defined here
Expand All @@ -14,10 +14,10 @@ LL | #![warn(unused_imports)]
| ^^^^^^^^^^^^^^

warning: the item `TryInto` is imported redundantly
--> $DIR/use-redundant-prelude-rust-2021.rs:6:5
--> $DIR/use-redundant-prelude-rust-2021.rs:6:1
|
LL | use std::convert::TryInto;
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this import
--> $SRC_DIR/std/src/prelude/mod.rs:LL:COL
|
= note: the item `TryInto` is already defined here
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/lint/use-redundant/use-redundant.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ LL | use m2::*;
| ^^^^^

warning: the item `Bar` is imported redundantly
--> $DIR/use-redundant.rs:21:9
--> $DIR/use-redundant.rs:21:5
|
LL | use crate::foo::Bar;
| --------------- the item `Bar` is already imported here
...
LL | use crate::foo::Bar;
| ^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^ help: remove this import

warning: 3 warnings emitted

0 comments on commit 23f52db

Please sign in to comment.