Skip to content

Commit e221be8

Browse files
Add command line lint manipulation in rustdoc
1 parent 2ea922a commit e221be8

File tree

3 files changed

+58
-20
lines changed

3 files changed

+58
-20
lines changed

src/librustc/session/config.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,29 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> ast::CrateConfig {
17471747
.collect::<ast::CrateConfig>()
17481748
}
17491749

1750+
pub fn get_cmd_lint_options(matches: &getopts::Matches,
1751+
error_format: ErrorOutputType)
1752+
-> (Vec<(String, lint::Level)>, bool, Option<lint::Level>) {
1753+
let mut lint_opts = vec![];
1754+
let mut describe_lints = false;
1755+
1756+
for &level in &[lint::Allow, lint::Warn, lint::Deny, lint::Forbid] {
1757+
for lint_name in matches.opt_strs(level.as_str()) {
1758+
if lint_name == "help" {
1759+
describe_lints = true;
1760+
} else {
1761+
lint_opts.push((lint_name.replace("-", "_"), level));
1762+
}
1763+
}
1764+
}
1765+
1766+
let lint_cap = matches.opt_str("cap-lints").map(|cap| {
1767+
lint::Level::from_str(&cap)
1768+
.unwrap_or_else(|| early_error(error_format, &format!("unknown lint level: `{}`", cap)))
1769+
});
1770+
(lint_opts, describe_lints, lint_cap)
1771+
}
1772+
17501773
pub fn build_session_options_and_crate_config(
17511774
matches: &getopts::Matches,
17521775
) -> (Options, ast::CrateConfig) {
@@ -1824,23 +1847,7 @@ pub fn build_session_options_and_crate_config(
18241847
let crate_types = parse_crate_types_from_list(unparsed_crate_types)
18251848
.unwrap_or_else(|e| early_error(error_format, &e[..]));
18261849

1827-
let mut lint_opts = vec![];
1828-
let mut describe_lints = false;
1829-
1830-
for &level in &[lint::Allow, lint::Warn, lint::Deny, lint::Forbid] {
1831-
for lint_name in matches.opt_strs(level.as_str()) {
1832-
if lint_name == "help" {
1833-
describe_lints = true;
1834-
} else {
1835-
lint_opts.push((lint_name.replace("-", "_"), level));
1836-
}
1837-
}
1838-
}
1839-
1840-
let lint_cap = matches.opt_str("cap-lints").map(|cap| {
1841-
lint::Level::from_str(&cap)
1842-
.unwrap_or_else(|| early_error(error_format, &format!("unknown lint level: `{}`", cap)))
1843-
});
1850+
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
18441851

18451852
let mut debugging_opts = build_debugging_options(matches, error_format);
18461853

src/librustdoc/core.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ pub fn run_core(search_paths: SearchPaths,
178178
force_unstable_if_unmarked: bool,
179179
edition: Edition,
180180
cg: CodegenOptions,
181-
error_format: ErrorOutputType) -> (clean::Crate, RenderInfo)
181+
error_format: ErrorOutputType,
182+
cmd_lints: Vec<(String, lint::Level)>,
183+
lint_cap: Option<lint::Level>,
184+
describe_lints: bool) -> (clean::Crate, RenderInfo)
182185
{
183186
// Parse, resolve, and typecheck the given crate.
184187

@@ -200,6 +203,7 @@ pub fn run_core(search_paths: SearchPaths,
200203
Some((lint.name_lower(), lint::Allow))
201204
}
202205
})
206+
.chain(cmd_lints.into_iter())
203207
.collect::<Vec<_>>();
204208

205209
let host_triple = TargetTriple::from_triple(config::host_triple());
@@ -213,7 +217,7 @@ pub fn run_core(search_paths: SearchPaths,
213217
} else {
214218
vec![]
215219
},
216-
lint_cap: Some(lint::Forbid),
220+
lint_cap: Some(lint_cap.unwrap_or_else(|| lint::Forbid)),
217221
cg,
218222
externs,
219223
target_triple: triple.unwrap_or(host_triple),
@@ -226,6 +230,7 @@ pub fn run_core(search_paths: SearchPaths,
226230
},
227231
error_format,
228232
edition,
233+
describe_lints,
229234
..config::basic_options()
230235
};
231236
driver::spawn_thread_pool(sessopts, move |sessopts| {

src/librustdoc/lib.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use rustc::session::search_paths::SearchPaths;
6868
use rustc::session::config::{ErrorOutputType, RustcOptGroup, Externs, CodegenOptions};
6969
use rustc::session::config::{nightly_options, build_codegen_options};
7070
use rustc_target::spec::TargetTriple;
71+
use rustc::session::config::get_cmd_lint_options;
7172

7273
#[macro_use]
7374
pub mod externalfiles;
@@ -304,6 +305,28 @@ pub fn opts() -> Vec<RustcOptGroup> {
304305
"disable-minification",
305306
"Disable minification applied on JS files")
306307
}),
308+
unstable("warn", |o| {
309+
o.optmulti("W", "warn", "Set lint warnings", "OPT")
310+
}),
311+
unstable("allow", |o| {
312+
o.optmulti("A", "allow", "Set lint allowed", "OPT")
313+
}),
314+
unstable("deny", |o| {
315+
o.optmulti("D", "deny", "Set lint denied", "OPT")
316+
}),
317+
unstable("forbid", |o| {
318+
o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
319+
}),
320+
unstable("cap-lints", |o| {
321+
o.optmulti(
322+
"",
323+
"cap-lints",
324+
"Set the most restrictive lint level. \
325+
More restrictive lints are capped at this \
326+
level. By default, it is at `forbid` level.",
327+
"LEVEL",
328+
)
329+
}),
307330
]
308331
}
309332

@@ -636,6 +659,8 @@ where R: 'static + Send,
636659
*x == "force-unstable-if-unmarked"
637660
});
638661

662+
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
663+
639664
let (tx, rx) = channel();
640665

641666
rustc_driver::monitor(move || syntax::with_globals(move || {
@@ -644,7 +669,8 @@ where R: 'static + Send,
644669
let (mut krate, renderinfo) =
645670
core::run_core(paths, cfgs, externs, Input::File(cratefile), triple, maybe_sysroot,
646671
display_warnings, crate_name.clone(),
647-
force_unstable_if_unmarked, edition, cg, error_format);
672+
force_unstable_if_unmarked, edition, cg, error_format,
673+
lint_opts, lint_cap, describe_lints);
648674

649675
info!("finished with rustc");
650676

0 commit comments

Comments
 (0)