From 388eed383f05dbc322d5808faf7d52091d0c0fca Mon Sep 17 00:00:00 2001 From: Haitao Li Date: Thu, 17 Nov 2011 12:23:43 +0800 Subject: [PATCH] rustc: Add a flag '--warn-unused-imports' Followup of issue #889 --- man/rustc.1 | 3 +++ src/comp/driver/rustc.rs | 9 +++++++-- src/comp/driver/session.rs | 3 ++- src/comp/middle/resolve.rs | 4 +++- src/test/compile-fail/unused-imports-warn.rs | 1 + 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/man/rustc.1 b/man/rustc.1 index 36ddea96ef4..55b9a3d5743 100644 --- a/man/rustc.1 +++ b/man/rustc.1 @@ -123,6 +123,9 @@ Build a test harness. .TP \fB--stack-growth\fR: \fBEXPERIMENTAL\fR. Perform stack growth checks. +.TP +\fB--warn-unused-imports\fR: +Warn about unnecessary imports. .SH "BUGS" See \fBhttps://github.com/graydon/rust/issues\fR for a list of known bugs. .SH "AUTHOR" diff --git a/src/comp/driver/rustc.rs b/src/comp/driver/rustc.rs index d42cd85ea08..1a684c1bf6c 100644 --- a/src/comp/driver/rustc.rs +++ b/src/comp/driver/rustc.rs @@ -274,6 +274,8 @@ options: --test build test harness --gc garbage collect shared data (experimental/temporary) --stack-growth perform stack checks (experimental) + --warn-unused-imports + warn about unnecessary imports "); } @@ -397,6 +399,7 @@ fn build_session_options(match: getopts::match) let test = opt_present(match, "test"); let do_gc = opt_present(match, "gc"); let stack_growth = opt_present(match, "stack-growth"); + let warn_unused_imports = opt_present(match, "warn-unused-imports"); let sopts: @session::options = @{library: library, static: static, @@ -417,7 +420,8 @@ fn build_session_options(match: getopts::match) no_trans: no_trans, do_gc: do_gc, stack_growth: stack_growth, - no_asm_comments: no_asm_comments}; + no_asm_comments: no_asm_comments, + warn_unused_imports: warn_unused_imports}; ret sopts; } @@ -457,7 +461,8 @@ fn opts() -> [getopts::opt] { optmulti("cfg"), optflag("test"), optflag("lib"), optflag("static"), optflag("gc"), optflag("stack-growth"), - optflag("no-asm-comments")]; + optflag("no-asm-comments"), + optflag("warn-unused-imports")]; } fn build_output_filenames(ifile: str, ofile: option::t, diff --git a/src/comp/driver/session.rs b/src/comp/driver/session.rs index c5708551ad9..4d7c53790ea 100644 --- a/src/comp/driver/session.rs +++ b/src/comp/driver/session.rs @@ -43,7 +43,8 @@ type options = no_trans: bool, do_gc: bool, stack_growth: bool, - no_asm_comments: bool}; + no_asm_comments: bool, + warn_unused_imports: bool}; type crate_metadata = {name: str, data: [u8]}; diff --git a/src/comp/middle/resolve.rs b/src/comp/middle/resolve.rs index 7ee6471660d..21b10280b0d 100644 --- a/src/comp/middle/resolve.rs +++ b/src/comp/middle/resolve.rs @@ -139,7 +139,9 @@ fn resolve_crate(sess: session, amap: ast_map::map, crate: @ast::crate) -> check_for_collisions(e, *crate); check_bad_exports(e); resolve_names(e, crate); - check_unused_imports(e); + if sess.get_opts().warn_unused_imports { + check_unused_imports(e); + } ret {def_map: e.def_map, ext_map: e.ext_map}; } diff --git a/src/test/compile-fail/unused-imports-warn.rs b/src/test/compile-fail/unused-imports-warn.rs index 88e42e0490a..f3ed5131455 100644 --- a/src/test/compile-fail/unused-imports-warn.rs +++ b/src/test/compile-fail/unused-imports-warn.rs @@ -1,4 +1,5 @@ // error-pattern:unused import +// compile-flags:--warn-unused-imports import cal = bar::c::cc; mod foo {