Skip to content

Commit 388eed3

Browse files
lhtmarijnh
authored andcommitted
rustc: Add a flag '--warn-unused-imports'
Followup of issue #889
1 parent fe6484d commit 388eed3

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

man/rustc.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ Build a test harness.
123123
.TP
124124
\fB--stack-growth\fR:
125125
\fBEXPERIMENTAL\fR. Perform stack growth checks.
126+
.TP
127+
\fB--warn-unused-imports\fR:
128+
Warn about unnecessary imports.
126129
.SH "BUGS"
127130
See \fBhttps://github.com/graydon/rust/issues\fR for a list of known bugs.
128131
.SH "AUTHOR"

src/comp/driver/rustc.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ options:
274274
--test build test harness
275275
--gc garbage collect shared data (experimental/temporary)
276276
--stack-growth perform stack checks (experimental)
277+
--warn-unused-imports
278+
warn about unnecessary imports
277279
278280
");
279281
}
@@ -397,6 +399,7 @@ fn build_session_options(match: getopts::match)
397399
let test = opt_present(match, "test");
398400
let do_gc = opt_present(match, "gc");
399401
let stack_growth = opt_present(match, "stack-growth");
402+
let warn_unused_imports = opt_present(match, "warn-unused-imports");
400403
let sopts: @session::options =
401404
@{library: library,
402405
static: static,
@@ -417,7 +420,8 @@ fn build_session_options(match: getopts::match)
417420
no_trans: no_trans,
418421
do_gc: do_gc,
419422
stack_growth: stack_growth,
420-
no_asm_comments: no_asm_comments};
423+
no_asm_comments: no_asm_comments,
424+
warn_unused_imports: warn_unused_imports};
421425
ret sopts;
422426
}
423427

@@ -457,7 +461,8 @@ fn opts() -> [getopts::opt] {
457461
optmulti("cfg"), optflag("test"),
458462
optflag("lib"), optflag("static"), optflag("gc"),
459463
optflag("stack-growth"),
460-
optflag("no-asm-comments")];
464+
optflag("no-asm-comments"),
465+
optflag("warn-unused-imports")];
461466
}
462467

463468
fn build_output_filenames(ifile: str, ofile: option::t<str>,

src/comp/driver/session.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ type options =
4343
no_trans: bool,
4444
do_gc: bool,
4545
stack_growth: bool,
46-
no_asm_comments: bool};
46+
no_asm_comments: bool,
47+
warn_unused_imports: bool};
4748

4849
type crate_metadata = {name: str, data: [u8]};
4950

src/comp/middle/resolve.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ fn resolve_crate(sess: session, amap: ast_map::map, crate: @ast::crate) ->
139139
check_for_collisions(e, *crate);
140140
check_bad_exports(e);
141141
resolve_names(e, crate);
142-
check_unused_imports(e);
142+
if sess.get_opts().warn_unused_imports {
143+
check_unused_imports(e);
144+
}
143145
ret {def_map: e.def_map, ext_map: e.ext_map};
144146
}
145147

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// error-pattern:unused import
2+
// compile-flags:--warn-unused-imports
23
import cal = bar::c::cc;
34

45
mod foo {

0 commit comments

Comments
 (0)