Skip to content

Include attributes in removal span for unused imports. #87975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/imports/unused-import-issue-87973.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix
#![deny(unused_imports)]

// Check that attributes get removed too. See #87973.

//~^ ERROR unused import

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/imports/unused-import-issue-87973.rs
Original file line number Diff line number Diff line change
@@ -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() {}
14 changes: 14 additions & 0 deletions src/test/ui/imports/unused-import-issue-87973.stderr
Original file line number Diff line number Diff line change
@@ -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