Skip to content

Commit b345437

Browse files
committed
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]: rust-lang/rfcs#1193 Closes #27259
1 parent c85ba3e commit b345437

File tree

6 files changed

+97
-1
lines changed

6 files changed

+97
-1
lines changed

src/librustc/lint/context.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use lint::builtin;
3434
use util::nodemap::FnvHashMap;
3535

3636
use std::cell::RefCell;
37+
use std::cmp;
3738
use std::mem;
3839
use syntax::ast_util::IdVisitingOperation;
3940
use syntax::attr::AttrMetaMethods;
@@ -66,6 +67,9 @@ pub struct LintStore {
6667
/// Map of registered lint groups to what lints they expand to. The bool
6768
/// is true if the lint group was added by a plugin.
6869
lint_groups: FnvHashMap<&'static str, (Vec<LintId>, bool)>,
70+
71+
/// Maximum level a lint can be
72+
lint_cap: Option<Level>,
6973
}
7074

7175
/// The targed of the `by_name` map, which accounts for renaming/deprecation.
@@ -94,7 +98,10 @@ impl LintStore {
9498
}
9599
}
96100

97-
fn set_level(&mut self, lint: LintId, lvlsrc: LevelSource) {
101+
fn set_level(&mut self, lint: LintId, mut lvlsrc: LevelSource) {
102+
if let Some(cap) = self.lint_cap {
103+
lvlsrc.0 = cmp::min(lvlsrc.0, cap);
104+
}
98105
if lvlsrc.0 == Allow {
99106
self.levels.remove(&lint);
100107
} else {
@@ -109,6 +116,7 @@ impl LintStore {
109116
by_name: FnvHashMap(),
110117
levels: FnvHashMap(),
111118
lint_groups: FnvHashMap(),
119+
lint_cap: None,
112120
}
113121
}
114122

@@ -227,6 +235,13 @@ impl LintStore {
227235
}
228236
}
229237
}
238+
239+
self.lint_cap = sess.opts.lint_cap;
240+
if let Some(cap) = self.lint_cap {
241+
for level in self.levels.iter_mut().map(|p| &mut (p.1).0) {
242+
*level = cmp::min(*level, cap);
243+
}
244+
}
230245
}
231246
}
232247

src/librustc/session/config.rs

+12
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub struct Options {
8484
pub debug_assertions: bool,
8585
pub debuginfo: DebugInfoLevel,
8686
pub lint_opts: Vec<(String, lint::Level)>,
87+
pub lint_cap: Option<lint::Level>,
8788
pub describe_lints: bool,
8889
pub output_types: Vec<OutputType>,
8990
// This was mutable for rustpkg, which updates search paths based on the
@@ -203,6 +204,7 @@ pub fn basic_options() -> Options {
203204
optimize: No,
204205
debuginfo: NoDebugInfo,
205206
lint_opts: Vec::new(),
207+
lint_cap: None,
206208
describe_lints: false,
207209
output_types: Vec::new(),
208210
search_paths: SearchPaths::new(),
@@ -792,6 +794,9 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
792794
opt::multi("A", "allow", "Set lint allowed", "OPT"),
793795
opt::multi("D", "deny", "Set lint denied", "OPT"),
794796
opt::multi("F", "forbid", "Set lint forbidden", "OPT"),
797+
opt::multi("", "cap-lints", "Set the most restrictive lint level. \
798+
More restrictive lints are capped at this \
799+
level", "LEVEL"),
795800
opt::multi("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
796801
opt::flag("V", "version", "Print version info and exit"),
797802
opt::flag("v", "verbose", "Use verbose output"),
@@ -860,6 +865,12 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
860865
}
861866
}
862867

868+
let lint_cap = matches.opt_str("cap-lints").map(|cap| {
869+
lint::Level::from_str(&cap).unwrap_or_else(|| {
870+
early_error(&format!("unknown lint level: `{}`", cap))
871+
})
872+
});
873+
863874
let debugging_opts = build_debugging_options(matches);
864875

865876
let parse_only = debugging_opts.parse_only;
@@ -1023,6 +1034,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
10231034
optimize: opt_level,
10241035
debuginfo: debuginfo,
10251036
lint_opts: lint_opts,
1037+
lint_cap: lint_cap,
10261038
describe_lints: describe_lints,
10271039
output_types: output_types,
10281040
search_paths: search_paths,

src/test/compile-fail/bad-lint-cap.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --cap-lints test
12+
// error-pattern: unknown lint level: `test`
13+
14+
fn main() {}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --cap-lints deny
12+
13+
#![deny(warnings)]
14+
15+
use std::option; //~ ERROR
16+
17+
fn main() {}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --cap-lints warn
12+
13+
#![deny(warnings)]
14+
#![feature(rustc_attrs)]
15+
16+
use std::option; //~ WARN
17+
18+
#[rustc_error]
19+
fn main() {} //~ ERROR: compilation successful
20+

src/test/run-pass/lint-cap.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --cap-lints allow
12+
13+
#![deny(warnings)]
14+
15+
use std::option;
16+
17+
fn main() {}
18+

0 commit comments

Comments
 (0)