Skip to content

Commit

Permalink
Be more specific when flagging imports that are redundant due to the …
Browse files Browse the repository at this point in the history
…extern prelude
  • Loading branch information
fmease committed Apr 8, 2024
1 parent 5dbaafd commit bfffbd0
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 13 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
BuiltinLintDiag::RedundantImport(spans, ident) => {
for (span, is_imported) in spans {
let introduced = if is_imported { "imported" } else { "defined" };
let span_msg = if span.is_dummy() { "by prelude" } else { "here" };
let span_msg = if span.is_dummy() { "by the extern prelude" } else { "here" };
diag.span_label(
span,
format!("the item `{ident}` is already {introduced} {span_msg}"),
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/imports/redundant-import-extern-prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Check that we detect imports that are redundant due to the extern prelude
// and that we emit a reasonable diagnostic.
// issue: rust-lang/rust#121915
//~^^^ NOTE the item `aux_issue_121915` is already defined by the extern prelude

// See also the discussion in <https://github.com/rust-lang/rust/pull/122954>.

//@ compile-flags: --extern aux_issue_121915 --edition 2018
//@ aux-build: aux-issue-121915.rs

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

This file was deleted.

18 changes: 18 additions & 0 deletions tests/ui/imports/redundant-import-lang-prelude-attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Check that we detect imports (of built-in attributes) that are redundant due to
// the language prelude and that we emit a reasonable diagnostic.
//~^^ NOTE the item `allow` is already defined by the extern prelude

// Note that we use the term "extern prelude" in the label even though "language prelude"
// would be more correct. However, it's not worth special-casing this.

// See also the discussion in <https://github.com/rust-lang/rust/pull/122954>.

//@ edition: 2018

#![deny(unused_imports)]
//~^ NOTE the lint level is defined here

use allow; //~ ERROR the item `allow` is imported redundantly

#[allow(unused)]
fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/imports/redundant-import-lang-prelude-attr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: the item `allow` is imported redundantly
--> $DIR/redundant-import-lang-prelude-attr.rs:15:5
|
LL | use allow;
| ^^^^^ the item `allow` is already defined by the extern prelude
|
note: the lint level is defined here
--> $DIR/redundant-import-lang-prelude-attr.rs:12:9
|
LL | #![deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

18 changes: 18 additions & 0 deletions tests/ui/imports/redundant-import-lang-prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Check that we detect imports that are redundant due to the language prelude
// and that we emit a reasonable diagnostic.
//~^^ NOTE the item `u8` is already defined by the extern prelude

// Note that we use the term "extern prelude" in the label even though "language prelude"
// would be more correct. However, it's not worth special-casing this.

// See also the discussion in <https://github.com/rust-lang/rust/pull/122954>.

#![deny(unused_imports)]
//~^ NOTE the lint level is defined here

use std::primitive::u8;
//~^ ERROR the item `u8` is imported redundantly

const _: u8 = 0;

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/imports/redundant-import-lang-prelude.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: the item `u8` is imported redundantly
--> $DIR/redundant-import-lang-prelude.rs:13:5
|
LL | use std::primitive::u8;
| ^^^^^^^^^^^^^^^^^^ the item `u8` is already defined by the extern prelude
|
note: the lint level is defined here
--> $DIR/redundant-import-lang-prelude.rs:10:9
|
LL | #![deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

23 changes: 23 additions & 0 deletions tests/ui/imports/redundant-import-undetected-macro-use-prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This test demonstrates that we currently don't make an effort to detect
// imports made redundant by the `#[macro_use]` prelude.
// See also the discussion in <https://github.com/rust-lang/rust/pull/122954>.

//@ check-pass
//@ aux-build:two_macros.rs
#![deny(unused_imports)]

#[macro_use]
extern crate two_macros;

// This import is actually redundant due to the `#[macro_use]` above.
use two_macros::n;

// We intentionally reference two items from the `#[macro_use]` because if
// were to reference only item `n`, we would flag the `#[macro_use]` attribute
// as redundant which would be correct of course.
// That's interesting on its own -- we prefer "blaming" the `#[macro_use]`
// over the import (here, `use two_macros::n`) when it comes to redundancy.
n!();
m!();

fn main() {}

0 comments on commit bfffbd0

Please sign in to comment.