Skip to content

Commit

Permalink
rustc: Add a --cap-lints flag to the compiler
Browse files Browse the repository at this point in the history
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 rust-lang#27259
  • Loading branch information
alexcrichton committed Jul 29, 2015
1 parent c85ba3e commit b345437
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/librustc/lint/context.rs
Expand Up @@ -34,6 +34,7 @@ use lint::builtin;
use util::nodemap::FnvHashMap;

use std::cell::RefCell;
use std::cmp;
use std::mem;
use syntax::ast_util::IdVisitingOperation;
use syntax::attr::AttrMetaMethods;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -94,7 +98,10 @@ impl LintStore {
}
}

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 {
Expand All @@ -109,6 +116,7 @@ impl LintStore {
by_name: FnvHashMap(),
levels: FnvHashMap(),
lint_groups: FnvHashMap(),
lint_cap: None,
}
}

Expand Down Expand Up @@ -227,6 +235,13 @@ impl LintStore {
}
}
}

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);
}
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/librustc/session/config.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions 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 <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() {}
17 changes: 17 additions & 0 deletions 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 <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() {}
20 changes: 20 additions & 0 deletions 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 <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

18 changes: 18 additions & 0 deletions 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 <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() {}

0 comments on commit b345437

Please sign in to comment.