Skip to content

Commit

Permalink
Rollup merge of rust-lang#38085 - estebank:empty-import-list-fix-3801…
Browse files Browse the repository at this point in the history
…2, r=jseyfried

Warn when an import list is empty

For a given file

```rust
use std::*;
use std::{};
```

output the following warnings

```
warning: unused import: `use std::{};`, #[warn(unused_imports)] on by default
 --> file.rs:2:1
  |
2 | use std::{};
  | ^^^^^^^^^^^^

warning: unused import: `std::*;`, #[warn(unused_imports)] on by default
 --> file.rs:1:5
  |
1 | use std::*;
  |     ^^^^^^^
```
  • Loading branch information
GuillaumeGomez authored Dec 7, 2016
2 parents 7846610 + 58e70e7 commit 494f686
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/librustc_resolve/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ impl<'a, 'b> Visitor for UnusedImportCheckVisitor<'a, 'b> {
}

ViewPathList(_, ref list) => {
if list.len() == 0 {
self.unused_imports
.entry(item.id)
.or_insert_with(NodeMap)
.insert(item.id, item.span);
}
for i in list {
self.check_import(item.id, i.node.id, i.span);
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/compile-fail/issue-28388-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

// Prefix in imports with empty braces should be resolved and checked privacy, stability, etc.

use foo::{}; //~ ERROR failed to resolve. Maybe a missing `extern crate foo;`?
use foo::{};
//~^ ERROR failed to resolve. Maybe a missing `extern crate foo;`?
//~| NOTE foo

fn main() {}
3 changes: 2 additions & 1 deletion src/test/compile-fail/issue-28388-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod m {
mod n {}
}

use m::n::{}; //~ ERROR module `n` is private
use m::n::{};
//~^ ERROR module `n` is private

fn main() {}
3 changes: 2 additions & 1 deletion src/test/compile-fail/issue-28388-3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

extern crate lint_stability;

use lint_stability::UnstableStruct::{}; //~ ERROR use of unstable library feature 'test_feature'
use lint_stability::UnstableStruct::{};
//~^ ERROR use of unstable library feature 'test_feature'
use lint_stability::StableStruct::{}; // OK

fn main() {}
2 changes: 2 additions & 0 deletions src/test/compile-fail/lint-unused-imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use bar::c::cc as cal;

use std::mem::*; // shouldn't get errors for not using
// everything imported
use std::fmt::{};
//~^ ERROR unused import: `use std::fmt::{};`

// Should get errors for both 'Some' and 'None'
use std::option::Option::{Some, None};
Expand Down

0 comments on commit 494f686

Please sign in to comment.