From 58e70e7b14fcf58ea4ee0cedd198cd1ab5ececa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 30 Nov 2016 00:07:31 -0800 Subject: [PATCH] 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::*; | ^^^^^^^ ``` --- src/librustc_resolve/check_unused.rs | 6 ++++++ src/test/compile-fail/issue-28388-1.rs | 4 +++- src/test/compile-fail/issue-28388-2.rs | 3 ++- src/test/compile-fail/issue-28388-3.rs | 3 ++- src/test/compile-fail/lint-unused-imports.rs | 2 ++ 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/librustc_resolve/check_unused.rs b/src/librustc_resolve/check_unused.rs index 492c5e695bbbb..5db622a4e7da0 100644 --- a/src/librustc_resolve/check_unused.rs +++ b/src/librustc_resolve/check_unused.rs @@ -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); } diff --git a/src/test/compile-fail/issue-28388-1.rs b/src/test/compile-fail/issue-28388-1.rs index bee05cd53133a..ed7851ec0f157 100644 --- a/src/test/compile-fail/issue-28388-1.rs +++ b/src/test/compile-fail/issue-28388-1.rs @@ -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() {} diff --git a/src/test/compile-fail/issue-28388-2.rs b/src/test/compile-fail/issue-28388-2.rs index 837dc67c804eb..4ed5bfab06f02 100644 --- a/src/test/compile-fail/issue-28388-2.rs +++ b/src/test/compile-fail/issue-28388-2.rs @@ -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() {} diff --git a/src/test/compile-fail/issue-28388-3.rs b/src/test/compile-fail/issue-28388-3.rs index 0cb669f5f8fb4..4baaa16e772da 100644 --- a/src/test/compile-fail/issue-28388-3.rs +++ b/src/test/compile-fail/issue-28388-3.rs @@ -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() {} diff --git a/src/test/compile-fail/lint-unused-imports.rs b/src/test/compile-fail/lint-unused-imports.rs index 5b1c04946a40b..f6f7c210f466a 100644 --- a/src/test/compile-fail/lint-unused-imports.rs +++ b/src/test/compile-fail/lint-unused-imports.rs @@ -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};