Skip to content

Commit a8f07dc

Browse files
committed
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.
2 parents 0ded562 + df48147 commit a8f07dc

File tree

5 files changed

+29
-27
lines changed

5 files changed

+29
-27
lines changed

doc/rust.md

-1
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,6 @@ The following are examples of structure expressions:
16111611
# struct Point { x: float, y: float }
16121612
# struct TuplePoint(float, float);
16131613
# mod game { pub struct User { name: &str, age: uint, score: uint } }
1614-
# use game;
16151614
Point {x: 10f, y: 20f};
16161615
TuplePoint(10f, 20f);
16171616
let u = game::User {name: "Joe", age: 35u, score: 100_000};

doc/tutorial-tasks.md

-2
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ Here is the function that implements the child task:
468468

469469
~~~~
470470
# use std::comm::DuplexStream;
471-
# use comm::{Port, Chan};
472471
fn stringifier(channel: &DuplexStream<~str, uint>) {
473472
let mut value: uint;
474473
loop {
@@ -491,7 +490,6 @@ Here is the code for the parent task:
491490

492491
~~~~
493492
# use std::comm::DuplexStream;
494-
# use comm::{Port, Chan};
495493
# use task::spawn;
496494
# fn stringifier(channel: &DuplexStream<~str, uint>) {
497495
# let mut value: uint;

doc/tutorial.md

+2
Original file line numberDiff line numberDiff line change
@@ -2270,7 +2270,9 @@ fn chicken_farmer() {
22702270
// The same, but name it `my_chicken`
22712271
use my_chicken = farm::chicken;
22722272
...
2273+
# my_chicken();
22732274
}
2275+
# chicken();
22742276
# }
22752277
~~~
22762278

src/librustc/middle/resolve.rs

+18-23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use metadata::cstore::find_extern_mod_stmt_cnum;
1919
use metadata::decoder::{def_like, dl_def, dl_field, dl_impl};
2020
use middle::lang_items::LanguageItems;
2121
use middle::lint::{deny, allow, forbid, level, unused_imports, warn};
22+
use middle::lint::{get_lint_level, get_lint_settings_level};
2223
use middle::pat_util::{pat_bindings};
2324

2425
use core::cmp;
@@ -508,16 +509,6 @@ pub impl Module {
508509
}
509510
}
510511
511-
pub fn unused_import_lint_level(session: Session) -> level {
512-
for session.opts.lint_opts.each |lint_option_pair| {
513-
let (lint_type, lint_level) = *lint_option_pair;
514-
if lint_type == unused_imports {
515-
return lint_level;
516-
}
517-
}
518-
return allow;
519-
}
520-
521512
// Records a possibly-private type definition.
522513
pub struct TypeNsDef {
523514
privacy: Privacy,
@@ -770,8 +761,6 @@ pub fn Resolver(session: Session,
770761
771762
graph_root: graph_root,
772763
773-
unused_import_lint_level: unused_import_lint_level(session),
774-
775764
trait_info: @HashMap(),
776765
structs: @HashMap(),
777766
@@ -816,8 +805,6 @@ pub struct Resolver {
816805
817806
graph_root: @mut NameBindings,
818807
819-
unused_import_lint_level: level,
820-
821808
trait_info: @HashMap<def_id,@HashMap<ident,()>>,
822809
structs: @HashMap<def_id,()>,
823810
@@ -5232,8 +5219,17 @@ pub impl Resolver {
52325219
// resolve data structures.
52335220
//
52345221

5222+
fn unused_import_lint_level(@mut self, m: @mut Module) -> level {
5223+
let settings = self.session.lint_settings;
5224+
match m.def_id {
5225+
Some(def) => get_lint_settings_level(settings, unused_imports,
5226+
def.node, def.node),
5227+
None => get_lint_level(settings.default_settings, unused_imports)
5228+
}
5229+
}
5230+
52355231
fn check_for_unused_imports_if_necessary(@mut self) {
5236-
if self.unused_import_lint_level == allow {
5232+
if self.unused_import_lint_level(self.current_module) == allow {
52375233
return;
52385234
}
52395235

@@ -5285,12 +5281,15 @@ pub impl Resolver {
52855281
for module_.import_resolutions.each_value |&import_resolution| {
52865282
// Ignore dummy spans for things like automatically injected
52875283
// imports for the prelude, and also don't warn about the same
5288-
// import statement being unused more than once.
5284+
// import statement being unused more than once. Furthermore, if
5285+
// the import is public, then we can't be sure whether it's unused
5286+
// or not so don't warn about it.
52895287
if !import_resolution.state.used &&
52905288
!import_resolution.state.warned &&
5291-
import_resolution.span != dummy_sp() {
5289+
import_resolution.span != dummy_sp() &&
5290+
import_resolution.privacy != Public {
52925291
import_resolution.state.warned = true;
5293-
match self.unused_import_lint_level {
5292+
match self.unused_import_lint_level(module_) {
52945293
warn => {
52955294
self.session.span_warn(copy import_resolution.span,
52965295
~"unused import");
@@ -5299,11 +5298,7 @@ pub impl Resolver {
52995298
self.session.span_err(copy import_resolution.span,
53005299
~"unused import");
53015300
}
5302-
allow => {
5303-
self.session.span_bug(copy import_resolution.span,
5304-
~"shouldn't be here if lint \
5305-
is allowed");
5306-
}
5301+
allow => ()
53075302
}
53085303
}
53095304
}

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags: -D unused-imports
11+
#[deny(unused_imports)];
1212

1313
use cal = bar::c::cc;
1414

@@ -31,11 +31,19 @@ mod foo {
3131
}
3232

3333
mod bar {
34+
// Don't ignore on 'pub use' because we're not sure if it's used or not
35+
pub use core::cmp::Eq;
36+
3437
pub mod c {
3538
use foo::Point;
3639
use foo::Square; //~ ERROR unused import
3740
pub fn cc(p: Point) -> int { return 2 * (p.x + p.y); }
3841
}
42+
43+
#[allow(unused_imports)]
44+
mod foo {
45+
use core::cmp::Eq;
46+
}
3947
}
4048

4149
fn main() {

0 commit comments

Comments
 (0)