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 bdde2a8 commit 74ed601
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 16 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 @@ -103,11 +103,21 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
);
}
}
BuiltinLintDiag::RedundantImport(spans, ident) => {
BuiltinLintDiag::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,
);
}
BuiltinLintDiag::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 BuiltinLintDiag {
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
4 changes: 1 addition & 3 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 @@ -1373,7 +1371,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
id,
import.span,
format!("the item `{source}` is imported redundantly"),
BuiltinLintDiag::RedundantImport(redundant_spans, source),
BuiltinLintDiag::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:9
|
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:9
|
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:5
|
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

2 changes: 1 addition & 1 deletion tests/ui/lint/unused/issue-59896.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
2 changes: 1 addition & 1 deletion tests/ui/lint/use-redundant/use-redundant-glob.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ warning: the item `Foo` is imported redundantly
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
Expand Up @@ -2,7 +2,7 @@ warning: the item `Some` is imported redundantly
--> $DIR/use-redundant-prelude-rust-2015.rs:5:5
|
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 @@ -17,7 +17,7 @@ warning: the item `None` is imported redundantly
--> $DIR/use-redundant-prelude-rust-2015.rs:6:5
|
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
Expand All @@ -26,7 +26,7 @@ warning: the item `Ok` is imported redundantly
--> $DIR/use-redundant-prelude-rust-2015.rs:8:5
|
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
Expand All @@ -35,7 +35,7 @@ warning: the item `Err` is imported redundantly
--> $DIR/use-redundant-prelude-rust-2015.rs:9:5
|
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
Expand Up @@ -2,7 +2,7 @@ warning: the item `TryFrom` is imported redundantly
--> $DIR/use-redundant-prelude-rust-2021.rs:5:5
|
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 @@ -17,7 +17,7 @@ warning: the item `TryInto` is imported redundantly
--> $DIR/use-redundant-prelude-rust-2021.rs:6:5
|
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
2 changes: 1 addition & 1 deletion tests/ui/lint/use-redundant/use-redundant.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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 74ed601

Please sign in to comment.