Skip to content

Commit 681e6dd

Browse files
committed
Fix using the #[]-style attribute for unused imports
1 parent 7e08d8f commit 681e6dd

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

src/librustc/middle/resolve.rs

Lines changed: 13 additions & 21 deletions
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

@@ -5293,7 +5289,7 @@ pub impl Resolver {
52935289
import_resolution.span != dummy_sp() &&
52945290
import_resolution.privacy != Public {
52955291
import_resolution.state.warned = true;
5296-
match self.unused_import_lint_level {
5292+
match self.unused_import_lint_level(module_) {
52975293
warn => {
52985294
self.session.span_warn(copy import_resolution.span,
52995295
~"unused import");
@@ -5302,11 +5298,7 @@ pub impl Resolver {
53025298
self.session.span_err(copy import_resolution.span,
53035299
~"unused import");
53045300
}
5305-
allow => {
5306-
self.session.span_bug(copy import_resolution.span,
5307-
~"shouldn't be here if lint \
5308-
is allowed");
5309-
}
5301+
allow => ()
53105302
}
53115303
}
53125304
}

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

Lines changed: 6 additions & 1 deletion
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

@@ -39,6 +39,11 @@ mod bar {
3939
use foo::Square; //~ ERROR unused import
4040
pub fn cc(p: Point) -> int { return 2 * (p.x + p.y); }
4141
}
42+
43+
#[allow(unused_imports)]
44+
mod foo {
45+
use core::cmp::Eq;
46+
}
4247
}
4348

4449
fn main() {

0 commit comments

Comments
 (0)