Skip to content

Commit 58e70e7

Browse files
committed
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::*; | ^^^^^^^ ```
1 parent ebeee0e commit 58e70e7

File tree

5 files changed

+15
-3
lines changed

5 files changed

+15
-3
lines changed

src/librustc_resolve/check_unused.rs

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ impl<'a, 'b> Visitor for UnusedImportCheckVisitor<'a, 'b> {
103103
}
104104

105105
ViewPathList(_, ref list) => {
106+
if list.len() == 0 {
107+
self.unused_imports
108+
.entry(item.id)
109+
.or_insert_with(NodeMap)
110+
.insert(item.id, item.span);
111+
}
106112
for i in list {
107113
self.check_import(item.id, i.node.id, i.span);
108114
}

src/test/compile-fail/issue-28388-1.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

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

13-
use foo::{}; //~ ERROR failed to resolve. Maybe a missing `extern crate foo;`?
13+
use foo::{};
14+
//~^ ERROR failed to resolve. Maybe a missing `extern crate foo;`?
15+
//~| NOTE foo
1416

1517
fn main() {}

src/test/compile-fail/issue-28388-2.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod m {
1414
mod n {}
1515
}
1616

17-
use m::n::{}; //~ ERROR module `n` is private
17+
use m::n::{};
18+
//~^ ERROR module `n` is private
1819

1920
fn main() {}

src/test/compile-fail/issue-28388-3.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
extern crate lint_stability;
1616

17-
use lint_stability::UnstableStruct::{}; //~ ERROR use of unstable library feature 'test_feature'
17+
use lint_stability::UnstableStruct::{};
18+
//~^ ERROR use of unstable library feature 'test_feature'
1819
use lint_stability::StableStruct::{}; // OK
1920

2021
fn main() {}

src/test/compile-fail/lint-unused-imports.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use bar::c::cc as cal;
1515

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

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

0 commit comments

Comments
 (0)