From 9d01db1966a3ab073691eb8e5203e36624b9f992 Mon Sep 17 00:00:00 2001 From: Jakub Bukaj Date: Mon, 24 Nov 2014 14:53:12 -0500 Subject: [PATCH] Do not print any warnings if '-A warnings' is specified on the command line --- src/librustc/session/config.rs | 34 ++++++++++++++++++++++++++++++++++ src/librustc/session/mod.rs | 22 +++++++++++++++++++--- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 8b4918b6db0..3c2dbae665f 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -948,4 +948,38 @@ mod test { assert!(test_items.next().is_some()); assert!(test_items.next().is_none()); } + + #[test] + fn test_can_print_warnings() { + { + let matches = getopts(&[ + "-Awarnings".to_string() + ], optgroups().as_slice()).unwrap(); + let registry = diagnostics::registry::Registry::new(&[]); + let sessopts = build_session_options(&matches); + let sess = build_session(sessopts, None, registry); + assert!(!sess.can_print_warnings); + } + + { + let matches = getopts(&[ + "-Awarnings".to_string(), + "-Dwarnings".to_string() + ], optgroups().as_slice()).unwrap(); + let registry = diagnostics::registry::Registry::new(&[]); + let sessopts = build_session_options(&matches); + let sess = build_session(sessopts, None, registry); + assert!(sess.can_print_warnings); + } + + { + let matches = getopts(&[ + "-Adead_code".to_string() + ], optgroups().as_slice()).unwrap(); + let registry = diagnostics::registry::Registry::new(&[]); + let sessopts = build_session_options(&matches); + let sess = build_session(sessopts, None, registry); + assert!(sess.can_print_warnings); + } + } } diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 89f6cda64d9..047e5985569 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -54,6 +54,8 @@ pub struct Session { /// The maximum recursion limit for potentially infinitely recursive /// operations such as auto-dereference and monomorphization. pub recursion_limit: Cell, + + pub can_print_warnings: bool } impl Session { @@ -82,13 +84,19 @@ impl Session { self.diagnostic().handler().abort_if_errors() } pub fn span_warn(&self, sp: Span, msg: &str) { - self.diagnostic().span_warn(sp, msg) + if self.can_print_warnings { + self.diagnostic().span_warn(sp, msg) + } } pub fn span_warn_with_code(&self, sp: Span, msg: &str, code: &str) { - self.diagnostic().span_warn_with_code(sp, msg, code) + if self.can_print_warnings { + self.diagnostic().span_warn_with_code(sp, msg, code) + } } pub fn warn(&self, msg: &str) { - self.diagnostic().handler().warn(msg) + if self.can_print_warnings { + self.diagnostic().handler().warn(msg) + } } pub fn opt_span_warn(&self, opt_sp: Option, msg: &str) { match opt_sp { @@ -247,6 +255,13 @@ pub fn build_session_(sopts: config::Options, } ); + let can_print_warnings = sopts.lint_opts + .iter() + .filter(|&&(ref key, _)| key.as_slice() == "warnings") + .map(|&(_, ref level)| *level != lint::Allow) + .last() + .unwrap_or(true); + let sess = Session { target: target_cfg, opts: sopts, @@ -265,6 +280,7 @@ pub fn build_session_(sopts: config::Options, crate_metadata: RefCell::new(Vec::new()), features: RefCell::new(feature_gate::Features::new()), recursion_limit: Cell::new(64), + can_print_warnings: can_print_warnings }; sess.lint_store.borrow_mut().register_builtin(Some(&sess));