Skip to content

Commit 00f4972

Browse files
committed
Auto merge of #51732 - GuillaumeGomez:cmd-line-lint-rustdoc, r=QuietMisdreavus
Add command line lint manipulation in rustdoc Fixes #50082. r? @QuietMisdreavus
2 parents 2d5a295 + e221be8 commit 00f4972

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;
@@ -308,6 +309,28 @@ pub fn opts() -> Vec<RustcOptGroup> {
308309
"disable-minification",
309310
"Disable minification applied on JS files")
310311
}),
312+
unstable("warn", |o| {
313+
o.optmulti("W", "warn", "Set lint warnings", "OPT")
314+
}),
315+
unstable("allow", |o| {
316+
o.optmulti("A", "allow", "Set lint allowed", "OPT")
317+
}),
318+
unstable("deny", |o| {
319+
o.optmulti("D", "deny", "Set lint denied", "OPT")
320+
}),
321+
unstable("forbid", |o| {
322+
o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
323+
}),
324+
unstable("cap-lints", |o| {
325+
o.optmulti(
326+
"",
327+
"cap-lints",
328+
"Set the most restrictive lint level. \
329+
More restrictive lints are capped at this \
330+
level. By default, it is at `forbid` level.",
331+
"LEVEL",
332+
)
333+
}),
311334
]
312335
}
313336

@@ -640,6 +663,8 @@ where R: 'static + Send,
640663
*x == "force-unstable-if-unmarked"
641664
});
642665

666+
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
667+
643668
let (tx, rx) = channel();
644669

645670
rustc_driver::monitor(move || syntax::with_globals(move || {
@@ -648,7 +673,8 @@ where R: 'static + Send,
648673
let (mut krate, renderinfo) =
649674
core::run_core(paths, cfgs, externs, Input::File(cratefile), triple, maybe_sysroot,
650675
display_warnings, crate_name.clone(),
651-
force_unstable_if_unmarked, edition, cg, error_format);
676+
force_unstable_if_unmarked, edition, cg, error_format,
677+
lint_opts, lint_cap, describe_lints);
652678

653679
info!("finished with rustc");
654680

0 commit comments

Comments
 (0)