Skip to content

Commit 68fc2d9

Browse files
committed
Auto merge of #27260 - alexcrichton:cap-lints, r=nrc
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
2 parents 8d432fb + b345437 commit 68fc2d9

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(),
@@ -794,6 +796,9 @@ pub fn rustc_short_optgroups() -> Vec<RustcOptGroup> {
794796
opt::multi("A", "allow", "Set lint allowed", "OPT"),
795797
opt::multi("D", "deny", "Set lint denied", "OPT"),
796798
opt::multi("F", "forbid", "Set lint forbidden", "OPT"),
799+
opt::multi("", "cap-lints", "Set the most restrictive lint level. \
800+
More restrictive lints are capped at this \
801+
level", "LEVEL"),
797802
opt::multi("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
798803
opt::flag("V", "version", "Print version info and exit"),
799804
opt::flag("v", "verbose", "Use verbose output"),
@@ -862,6 +867,12 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
862867
}
863868
}
864869

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

867878
let parse_only = debugging_opts.parse_only;
@@ -1025,6 +1036,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
10251036
optimize: opt_level,
10261037
debuginfo: debuginfo,
10271038
lint_opts: lint_opts,
1039+
lint_cap: lint_cap,
10281040
describe_lints: describe_lints,
10291041
output_types: output_types,
10301042
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)