about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-02-26 15:45:43 -0800
committerbors <bors@rust-lang.org>2013-02-26 15:45:43 -0800
commita8f07dc9df24af3cf06171bfde1a68b3058e2ec5 (patch)
treeb5ff82ea19138fc03f9786c761008dbfd2d53866
parent0ded562e8364244c9c0da1d5f4dc97cd900b4fe4 (diff)
parentdf481473dae024e522afd669646fa779d97c5e2d (diff)
downloadrust-a8f07dc9df24af3cf06171bfde1a68b3058e2ec5.tar.gz
rust-a8f07dc9df24af3cf06171bfde1a68b3058e2ec5.zip
auto merge of #5104 : alexcrichton/rust/fix-unused-import-pub, r=catamorphism
The first commit fixes warnings about `pub use` imports because it can't be known whether those are actually used or not.

The second commit fixes using `#[level(unused_imports)]` style control over the emission of warnings. Before it looked like it only worked as a command-line flag.
-rw-r--r--doc/rust.md1
-rw-r--r--doc/tutorial-tasks.md2
-rw-r--r--doc/tutorial.md2
-rw-r--r--src/librustc/middle/resolve.rs41
-rw-r--r--src/test/compile-fail/unused-imports-warn.rs10
5 files changed, 29 insertions, 27 deletions
diff --git a/doc/rust.md b/doc/rust.md
index 9a3d087f3d7..a4e4e4cdf24 100644
--- a/doc/rust.md
+++ b/doc/rust.md
@@ -1611,7 +1611,6 @@ The following are examples of structure expressions:
 # struct Point { x: float, y: float }
 # struct TuplePoint(float, float);
 # mod game { pub struct User { name: &str, age: uint, score: uint } }
-# use game;
 Point {x: 10f, y: 20f};
 TuplePoint(10f, 20f);
 let u = game::User {name: "Joe", age: 35u, score: 100_000};
diff --git a/doc/tutorial-tasks.md b/doc/tutorial-tasks.md
index c0f9a376270..22d0ff8bf78 100644
--- a/doc/tutorial-tasks.md
+++ b/doc/tutorial-tasks.md
@@ -468,7 +468,6 @@ Here is the function that implements the child task:
 
 ~~~~
 # use std::comm::DuplexStream;
-# use comm::{Port, Chan};
 fn stringifier(channel: &DuplexStream<~str, uint>) {
     let mut value: uint;
     loop {
@@ -491,7 +490,6 @@ Here is the code for the parent task:
 
 ~~~~
 # use std::comm::DuplexStream;
-# use comm::{Port, Chan};
 # use task::spawn;
 # fn stringifier(channel: &DuplexStream<~str, uint>) {
 #     let mut value: uint;
diff --git a/doc/tutorial.md b/doc/tutorial.md
index cd683490a59..98ec9d1f580 100644
--- a/doc/tutorial.md
+++ b/doc/tutorial.md
@@ -2270,7 +2270,9 @@ fn chicken_farmer() {
     // The same, but name it `my_chicken`
     use my_chicken = farm::chicken;
     ...
+# my_chicken();
 }
+# chicken();
 # }
 ~~~
 
diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs
index 689d6ca40ee..7058f802bc4 100644
--- a/src/librustc/middle/resolve.rs
+++ b/src/librustc/middle/resolve.rs
@@ -19,6 +19,7 @@ use metadata::cstore::find_extern_mod_stmt_cnum;
 use metadata::decoder::{def_like, dl_def, dl_field, dl_impl};
 use middle::lang_items::LanguageItems;
 use middle::lint::{deny, allow, forbid, level, unused_imports, warn};
+use middle::lint::{get_lint_level, get_lint_settings_level};
 use middle::pat_util::{pat_bindings};
 
 use core::cmp;
@@ -508,16 +509,6 @@ pub impl Module {
     }
 }
 
-pub fn unused_import_lint_level(session: Session) -> level {
-    for session.opts.lint_opts.each |lint_option_pair| {
-        let (lint_type, lint_level) = *lint_option_pair;
-        if lint_type == unused_imports {
-            return lint_level;
-        }
-    }
-    return allow;
-}
-
 // Records a possibly-private type definition.
 pub struct TypeNsDef {
     privacy: Privacy,
@@ -770,8 +761,6 @@ pub fn Resolver(session: Session,
 
         graph_root: graph_root,
 
-        unused_import_lint_level: unused_import_lint_level(session),
-
         trait_info: @HashMap(),
         structs: @HashMap(),
 
@@ -816,8 +805,6 @@ pub struct Resolver {
 
     graph_root: @mut NameBindings,
 
-    unused_import_lint_level: level,
-
     trait_info: @HashMap<def_id,@HashMap<ident,()>>,
     structs: @HashMap<def_id,()>,
 
@@ -5232,8 +5219,17 @@ pub impl Resolver {
     // resolve data structures.
     //
 
+    fn unused_import_lint_level(@mut self, m: @mut Module) -> level {
+        let settings = self.session.lint_settings;
+        match m.def_id {
+            Some(def) => get_lint_settings_level(settings, unused_imports,
+                                                 def.node, def.node),
+            None => get_lint_level(settings.default_settings, unused_imports)
+        }
+    }
+
     fn check_for_unused_imports_if_necessary(@mut self) {
-        if self.unused_import_lint_level == allow {
+        if self.unused_import_lint_level(self.current_module) == allow {
             return;
         }
 
@@ -5285,12 +5281,15 @@ pub impl Resolver {
         for module_.import_resolutions.each_value |&import_resolution| {
             // Ignore dummy spans for things like automatically injected
             // imports for the prelude, and also don't warn about the same
-            // import statement being unused more than once.
+            // import statement being unused more than once. Furthermore, if
+            // the import is public, then we can't be sure whether it's unused
+            // or not so don't warn about it.
             if !import_resolution.state.used &&
                     !import_resolution.state.warned &&
-                    import_resolution.span != dummy_sp() {
+                    import_resolution.span != dummy_sp() &&
+                    import_resolution.privacy != Public {
                 import_resolution.state.warned = true;
-                match self.unused_import_lint_level {
+                match self.unused_import_lint_level(module_) {
                     warn => {
                         self.session.span_warn(copy import_resolution.span,
                                                ~"unused import");
@@ -5299,11 +5298,7 @@ pub impl Resolver {
                       self.session.span_err(copy import_resolution.span,
                                             ~"unused import");
                     }
-                    allow => {
-                      self.session.span_bug(copy import_resolution.span,
-                                            ~"shouldn't be here if lint \
-                                              is allowed");
-                    }
+                    allow => ()
                 }
             }
         }
diff --git a/src/test/compile-fail/unused-imports-warn.rs b/src/test/compile-fail/unused-imports-warn.rs
index 6dcdb413f88..7756f96b470 100644
--- a/src/test/compile-fail/unused-imports-warn.rs
+++ b/src/test/compile-fail/unused-imports-warn.rs
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// compile-flags: -D unused-imports
+#[deny(unused_imports)];
 
 use cal = bar::c::cc;
 
@@ -31,11 +31,19 @@ mod foo {
 }
 
 mod bar {
+    // Don't ignore on 'pub use' because we're not sure if it's used or not
+    pub use core::cmp::Eq;
+
     pub mod c {
         use foo::Point;
         use foo::Square; //~ ERROR unused import
         pub fn cc(p: Point) -> int { return 2 * (p.x + p.y); }
     }
+
+    #[allow(unused_imports)]
+    mod foo {
+        use core::cmp::Eq;
+    }
 }
 
 fn main() {