Skip to content

Commit 8a24ddf

Browse files
committed
Be more specific when flagging imports that are redundant due to the extern prelude
1 parent 5dbaafd commit 8a24ddf

9 files changed

+108
-13
lines changed

compiler/rustc_lint/src/context/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
105105
BuiltinLintDiag::RedundantImport(spans, ident) => {
106106
for (span, is_imported) in spans {
107107
let introduced = if is_imported { "imported" } else { "defined" };
108-
let span_msg = if span.is_dummy() { "by prelude" } else { "here" };
108+
let span_msg = if span.is_dummy() { "by the extern prelude" } else { "here" };
109109
diag.span_label(
110110
span,
111111
format!("the item `{ident}` is already {introduced} {span_msg}"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Check that we detect imports that are redundant due to the extern prelude
2+
// and that we emit a reasonable diagnostic.
3+
// issue: rust-lang/rust#121915
4+
//~^^^ NOTE the item `aux_issue_121915` is already defined by the extern prelude
5+
6+
// See also the discussion in <https://github.com/rust-lang/rust/pull/122954>.
7+
8+
//@ compile-flags: --extern aux_issue_121915 --edition 2018
9+
//@ aux-build: aux-issue-121915.rs
10+
11+
#[deny(unused_imports)]
12+
//~^ NOTE the lint level is defined here
13+
fn main() {
14+
use aux_issue_121915;
15+
//~^ ERROR the item `aux_issue_121915` is imported redundantly
16+
aux_issue_121915::item();
17+
}

tests/ui/imports/redundant-import-issue-121915.stderr tests/ui/imports/redundant-import-extern-prelude.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: the item `aux_issue_121915` is imported redundantly
2-
--> $DIR/redundant-import-issue-121915.rs:6:9
2+
--> $DIR/redundant-import-extern-prelude.rs:14:9
33
|
44
LL | use aux_issue_121915;
5-
| ^^^^^^^^^^^^^^^^ the item `aux_issue_121915` is already defined by prelude
5+
| ^^^^^^^^^^^^^^^^ the item `aux_issue_121915` is already defined by the extern prelude
66
|
77
note: the lint level is defined here
8-
--> $DIR/redundant-import-issue-121915.rs:4:8
8+
--> $DIR/redundant-import-extern-prelude.rs:11:8
99
|
1010
LL | #[deny(unused_imports)]
1111
| ^^^^^^^^^^^^^^

tests/ui/imports/redundant-import-issue-121915.rs

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Check that we detect imports (of built-in attributes) that are redundant due to
2+
// the language prelude and that we emit a reasonable diagnostic.
3+
//~^^ NOTE the item `allow` is already defined by the extern prelude
4+
5+
// Note that we use the term "extern prelude" in the label even though "language prelude"
6+
// would be more correct. However, it's not worth special-casing this.
7+
8+
// See also the discussion in <https://github.com/rust-lang/rust/pull/122954>.
9+
10+
//@ edition: 2018
11+
12+
#![deny(unused_imports)]
13+
//~^ NOTE the lint level is defined here
14+
15+
use allow; //~ ERROR the item `allow` is imported redundantly
16+
17+
#[allow(unused)]
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the item `allow` is imported redundantly
2+
--> $DIR/redundant-import-lang-prelude-attr.rs:15:5
3+
|
4+
LL | use allow;
5+
| ^^^^^ the item `allow` is already defined by the extern prelude
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/redundant-import-lang-prelude-attr.rs:12:9
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,18 @@
1+
// Check that we detect imports that are redundant due to the language prelude
2+
// and that we emit a reasonable diagnostic.
3+
//~^^ NOTE the item `u8` is already defined by the extern prelude
4+
5+
// Note that we use the term "extern prelude" in the label even though "language prelude"
6+
// would be more correct. However, it's not worth special-casing this.
7+
8+
// See also the discussion in <https://github.com/rust-lang/rust/pull/122954>.
9+
10+
#![deny(unused_imports)]
11+
//~^ NOTE the lint level is defined here
12+
13+
use std::primitive::u8;
14+
//~^ ERROR the item `u8` is imported redundantly
15+
16+
const _: u8 = 0;
17+
18+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: the item `u8` is imported redundantly
2+
--> $DIR/redundant-import-lang-prelude.rs:13:5
3+
|
4+
LL | use std::primitive::u8;
5+
| ^^^^^^^^^^^^^^^^^^ the item `u8` is already defined by the extern prelude
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/redundant-import-lang-prelude.rs:10:9
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,23 @@
1+
// This test demonstrates that we currently don't make an effort to detect
2+
// imports made redundant by the `#[macro_use]` prelude.
3+
// See also the discussion in <https://github.com/rust-lang/rust/pull/122954>.
4+
5+
//@ check-pass
6+
//@ aux-build:two_macros.rs
7+
#![deny(unused_imports)]
8+
9+
#[macro_use]
10+
extern crate two_macros;
11+
12+
// This import is actually redundant due to the `#[macro_use]` above.
13+
use two_macros::n;
14+
15+
// We intentionally reference two items from the `#[macro_use]`'d crate because
16+
// if we were to reference only item `n`, we would flag the `#[macro_use]`
17+
// attribute as redundant which would be correct of course.
18+
// That's interesting on its own -- we prefer "blaming" the `#[macro_use]`
19+
// over the import (here, `use two_macros::n`) when it comes to redundancy.
20+
n!();
21+
m!();
22+
23+
fn main() {}

0 commit comments

Comments
 (0)