Skip to content

Add command line lint manipulation in rustdoc #51732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,29 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> ast::CrateConfig {
.collect::<ast::CrateConfig>()
}

pub fn get_cmd_lint_options(matches: &getopts::Matches,
error_format: ErrorOutputType)
-> (Vec<(String, lint::Level)>, bool, Option<lint::Level>) {
let mut lint_opts = vec![];
let mut describe_lints = false;

for &level in &[lint::Allow, lint::Warn, lint::Deny, lint::Forbid] {
for lint_name in matches.opt_strs(level.as_str()) {
if lint_name == "help" {
describe_lints = true;
} else {
lint_opts.push((lint_name.replace("-", "_"), level));
}
}
}

let lint_cap = matches.opt_str("cap-lints").map(|cap| {
lint::Level::from_str(&cap)
.unwrap_or_else(|| early_error(error_format, &format!("unknown lint level: `{}`", cap)))
});
(lint_opts, describe_lints, lint_cap)
}

pub fn build_session_options_and_crate_config(
matches: &getopts::Matches,
) -> (Options, ast::CrateConfig) {
Expand Down Expand Up @@ -1824,23 +1847,7 @@ pub fn build_session_options_and_crate_config(
let crate_types = parse_crate_types_from_list(unparsed_crate_types)
.unwrap_or_else(|e| early_error(error_format, &e[..]));

let mut lint_opts = vec![];
let mut describe_lints = false;

for &level in &[lint::Allow, lint::Warn, lint::Deny, lint::Forbid] {
for lint_name in matches.opt_strs(level.as_str()) {
if lint_name == "help" {
describe_lints = true;
} else {
lint_opts.push((lint_name.replace("-", "_"), level));
}
}
}

let lint_cap = matches.opt_str("cap-lints").map(|cap| {
lint::Level::from_str(&cap)
.unwrap_or_else(|| early_error(error_format, &format!("unknown lint level: `{}`", cap)))
});
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);

let mut debugging_opts = build_debugging_options(matches, error_format);

Expand Down
9 changes: 7 additions & 2 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ pub fn run_core(search_paths: SearchPaths,
force_unstable_if_unmarked: bool,
edition: Edition,
cg: CodegenOptions,
error_format: ErrorOutputType) -> (clean::Crate, RenderInfo)
error_format: ErrorOutputType,
cmd_lints: Vec<(String, lint::Level)>,
lint_cap: Option<lint::Level>,
describe_lints: bool) -> (clean::Crate, RenderInfo)
{
// Parse, resolve, and typecheck the given crate.

Expand All @@ -200,6 +203,7 @@ pub fn run_core(search_paths: SearchPaths,
Some((lint.name_lower(), lint::Allow))
}
})
.chain(cmd_lints.into_iter())
.collect::<Vec<_>>();

let host_triple = TargetTriple::from_triple(config::host_triple());
Expand All @@ -213,7 +217,7 @@ pub fn run_core(search_paths: SearchPaths,
} else {
vec![]
},
lint_cap: Some(lint::Forbid),
lint_cap: Some(lint_cap.unwrap_or_else(|| lint::Forbid)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leave this as None if you're going to default it to Forbid anyway; rustc will do this unwrap itself later on.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I did at first but didn't get the expected result.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, weird, I didn't realize it was unwrapped anywhere other than where it was used.

cg,
externs,
target_triple: triple.unwrap_or(host_triple),
Expand All @@ -226,6 +230,7 @@ pub fn run_core(search_paths: SearchPaths,
},
error_format,
edition,
describe_lints,
..config::basic_options()
};
driver::spawn_thread_pool(sessopts, move |sessopts| {
Expand Down
28 changes: 27 additions & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use rustc::session::search_paths::SearchPaths;
use rustc::session::config::{ErrorOutputType, RustcOptGroup, Externs, CodegenOptions};
use rustc::session::config::{nightly_options, build_codegen_options};
use rustc_target::spec::TargetTriple;
use rustc::session::config::get_cmd_lint_options;

#[macro_use]
pub mod externalfiles;
Expand Down Expand Up @@ -304,6 +305,28 @@ pub fn opts() -> Vec<RustcOptGroup> {
"disable-minification",
"Disable minification applied on JS files")
}),
unstable("warn", |o| {
o.optmulti("W", "warn", "Set lint warnings", "OPT")
}),
unstable("allow", |o| {
o.optmulti("A", "allow", "Set lint allowed", "OPT")
}),
unstable("deny", |o| {
o.optmulti("D", "deny", "Set lint denied", "OPT")
}),
unstable("forbid", |o| {
o.optmulti("F", "forbid", "Set lint forbidden", "OPT")
}),
unstable("cap-lints", |o| {
o.optmulti(
"",
"cap-lints",
"Set the most restrictive lint level. \
More restrictive lints are capped at this \
level. By default, it is at `forbid` level.",
"LEVEL",
)
}),
]
}

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

let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);

let (tx, rx) = channel();

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

info!("finished with rustc");

Expand Down