diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index 6d29a1031c6bc9859bd976c90afa2de89f416f5c..40ee023b7e86110c0ce68ba2a3d3c391c20805ee 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -34,6 +34,7 @@ use util::nodemap::FnvHashMap; use std::cell::RefCell; +use std::cmp; use std::mem; use syntax::ast_util::IdVisitingOperation; use syntax::attr::AttrMetaMethods; @@ -66,6 +67,9 @@ pub struct LintStore { /// Map of registered lint groups to what lints they expand to. The bool /// is true if the lint group was added by a plugin. lint_groups: FnvHashMap<&'static str, (Vec, bool)>, + + /// Maximum level a lint can be + lint_cap: Option, } /// The targed of the `by_name` map, which accounts for renaming/deprecation. @@ -94,7 +98,10 @@ fn get_level_source(&self, lint: LintId) -> LevelSource { } } - fn set_level(&mut self, lint: LintId, lvlsrc: LevelSource) { + fn set_level(&mut self, lint: LintId, mut lvlsrc: LevelSource) { + if let Some(cap) = self.lint_cap { + lvlsrc.0 = cmp::min(lvlsrc.0, cap); + } if lvlsrc.0 == Allow { self.levels.remove(&lint); } else { @@ -109,6 +116,7 @@ pub fn new() -> LintStore { by_name: FnvHashMap(), levels: FnvHashMap(), lint_groups: FnvHashMap(), + lint_cap: None, } } @@ -227,6 +235,13 @@ pub fn process_command_line(&mut self, sess: &Session) { } } } + + self.lint_cap = sess.opts.lint_cap; + if let Some(cap) = self.lint_cap { + for level in self.levels.iter_mut().map(|p| &mut (p.1).0) { + *level = cmp::min(*level, cap); + } + } } } diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index c6ce3a22d9b1c87cae3f3face29a9c151a529ea6..c14a0595d5ac4ce79df49126f771b6f875a98814 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -84,6 +84,7 @@ pub struct Options { pub debug_assertions: bool, pub debuginfo: DebugInfoLevel, pub lint_opts: Vec<(String, lint::Level)>, + pub lint_cap: Option, pub describe_lints: bool, pub output_types: Vec, // This was mutable for rustpkg, which updates search paths based on the @@ -203,6 +204,7 @@ pub fn basic_options() -> Options { optimize: No, debuginfo: NoDebugInfo, lint_opts: Vec::new(), + lint_cap: None, describe_lints: false, output_types: Vec::new(), search_paths: SearchPaths::new(), @@ -792,6 +794,9 @@ pub fn rustc_short_optgroups() -> Vec { opt::multi("A", "allow", "Set lint allowed", "OPT"), opt::multi("D", "deny", "Set lint denied", "OPT"), opt::multi("F", "forbid", "Set lint forbidden", "OPT"), + opt::multi("", "cap-lints", "Set the most restrictive lint level. \ + More restrictive lints are capped at this \ + level", "LEVEL"), opt::multi("C", "codegen", "Set a codegen option", "OPT[=VALUE]"), opt::flag("V", "version", "Print version info and exit"), opt::flag("v", "verbose", "Use verbose output"), @@ -860,6 +865,12 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { } } + let lint_cap = matches.opt_str("cap-lints").map(|cap| { + lint::Level::from_str(&cap).unwrap_or_else(|| { + early_error(&format!("unknown lint level: `{}`", cap)) + }) + }); + let debugging_opts = build_debugging_options(matches); let parse_only = debugging_opts.parse_only; @@ -1023,6 +1034,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { optimize: opt_level, debuginfo: debuginfo, lint_opts: lint_opts, + lint_cap: lint_cap, describe_lints: describe_lints, output_types: output_types, search_paths: search_paths, diff --git a/src/test/compile-fail/bad-lint-cap.rs b/src/test/compile-fail/bad-lint-cap.rs new file mode 100644 index 0000000000000000000000000000000000000000..cb9c347af603c1e42e57170808a54ee94915ea8f --- /dev/null +++ b/src/test/compile-fail/bad-lint-cap.rs @@ -0,0 +1,14 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: --cap-lints test +// error-pattern: unknown lint level: `test` + +fn main() {} diff --git a/src/test/compile-fail/bad-lint-cap2.rs b/src/test/compile-fail/bad-lint-cap2.rs new file mode 100644 index 0000000000000000000000000000000000000000..5a97d7b1a79a186ad2a88f136a52f023ed2fc779 --- /dev/null +++ b/src/test/compile-fail/bad-lint-cap2.rs @@ -0,0 +1,17 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: --cap-lints deny + +#![deny(warnings)] + +use std::option; //~ ERROR + +fn main() {} diff --git a/src/test/compile-fail/bad-lint-cap3.rs b/src/test/compile-fail/bad-lint-cap3.rs new file mode 100644 index 0000000000000000000000000000000000000000..e03ba6ecb6440fd278bd82ddc9084770ab0ff3cb --- /dev/null +++ b/src/test/compile-fail/bad-lint-cap3.rs @@ -0,0 +1,20 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: --cap-lints warn + +#![deny(warnings)] +#![feature(rustc_attrs)] + +use std::option; //~ WARN + +#[rustc_error] +fn main() {} //~ ERROR: compilation successful + diff --git a/src/test/run-pass/lint-cap.rs b/src/test/run-pass/lint-cap.rs new file mode 100644 index 0000000000000000000000000000000000000000..003a99f746a59b18f451ca47919dd2bf004edcf9 --- /dev/null +++ b/src/test/run-pass/lint-cap.rs @@ -0,0 +1,18 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: --cap-lints allow + +#![deny(warnings)] + +use std::option; + +fn main() {} +