diff options
| author | bors <bors@rust-lang.org> | 2021-08-15 07:40:53 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-08-15 07:40:53 +0000 |
| commit | dfe5fd0902b55546dc38a15f01effbb248e8b222 (patch) | |
| tree | f5ac86bea5b36c6105acea80f5dd984b46b10df7 | |
| parent | 40db258731a912325de612a1fc06d14e6df959ab (diff) | |
| parent | 8dc8649bd71c9c3ab51473753e5869392ae54d60 (diff) | |
| download | rust-dfe5fd0902b55546dc38a15f01effbb248e8b222.tar.gz rust-dfe5fd0902b55546dc38a15f01effbb248e8b222.zip | |
Auto merge of #87975 - m-ou-se:unused-import-attributes, r=nagisa
Include attributes in removal span for unused imports. Fixes https://github.com/rust-lang/rust/issues/87973
| -rw-r--r-- | compiler/rustc_resolve/src/check_unused.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/imports/unused-import-issue-87973.fixed | 8 | ||||
| -rw-r--r-- | src/test/ui/imports/unused-import-issue-87973.rs | 11 | ||||
| -rw-r--r-- | src/test/ui/imports/unused-import-issue-87973.stderr | 14 |
4 files changed, 34 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index 89ce89b2e9a..9f4f2b82f60 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -98,7 +98,7 @@ impl<'a, 'b> UnusedImportCheckVisitor<'a, 'b> { impl<'a, 'b> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b> { fn visit_item(&mut self, item: &'a ast::Item) { - self.item_span = item.span; + self.item_span = item.span_with_attributes(); // Ignore is_public import statements because there's no way to be sure // whether they're used or not. Also ignore imports with a dummy span diff --git a/src/test/ui/imports/unused-import-issue-87973.fixed b/src/test/ui/imports/unused-import-issue-87973.fixed new file mode 100644 index 00000000000..5c194d18aca --- /dev/null +++ b/src/test/ui/imports/unused-import-issue-87973.fixed @@ -0,0 +1,8 @@ +// run-rustfix +#![deny(unused_imports)] + +// Check that attributes get removed too. See #87973. + +//~^ ERROR unused import + +fn main() {} diff --git a/src/test/ui/imports/unused-import-issue-87973.rs b/src/test/ui/imports/unused-import-issue-87973.rs new file mode 100644 index 00000000000..c31f0f9796e --- /dev/null +++ b/src/test/ui/imports/unused-import-issue-87973.rs @@ -0,0 +1,11 @@ +// run-rustfix +#![deny(unused_imports)] + +// Check that attributes get removed too. See #87973. +#[deprecated] +#[allow(unsafe_code)] +#[cfg(not(foo))] +use std::fs; +//~^ ERROR unused import + +fn main() {} diff --git a/src/test/ui/imports/unused-import-issue-87973.stderr b/src/test/ui/imports/unused-import-issue-87973.stderr new file mode 100644 index 00000000000..81b7ea79191 --- /dev/null +++ b/src/test/ui/imports/unused-import-issue-87973.stderr @@ -0,0 +1,14 @@ +error: unused import: `std::fs` + --> $DIR/unused-import-issue-87973.rs:8:5 + | +LL | use std::fs; + | ^^^^^^^ + | +note: the lint level is defined here + --> $DIR/unused-import-issue-87973.rs:2:9 + | +LL | #![deny(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: aborting due to previous error + |
