提交 b345437c 编写于 作者: A Alex Crichton

rustc: Add a --cap-lints flag to the compiler

This commit is an implementation of [RFC 1193][rfc] which adds the ability to
the compiler to cap the lint level for the entire compilation session. This flag
will ensure that no lints will go above this level, and Cargo will primarily use
this flag passing `--cap-lints allow` to all upstream dependencies.

[rfc]: https://github.com/rust-lang/rfcs/pull/1193

Closes #27259
上级 c85ba3e9
......@@ -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<LintId>, bool)>,
/// Maximum level a lint can be
lint_cap: Option<Level>,
}
/// 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);
}
}
}
}
......
......@@ -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<lint::Level>,
pub describe_lints: bool,
pub output_types: Vec<OutputType>,
// 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<RustcOptGroup> {
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,
......
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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() {}
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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() {}
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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
// 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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() {}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册