1
Fork 0

rustdoc: Remove unnecessary optional

Previously, the HTML output format was represented by both
`Some(OutputFormat::Html)` and `None` so there's no need to have an
optional. Instead, `OutputFormat::Html` is explicitly the default and we
no longer have a "tri-state enum".
This commit is contained in:
Camelid 2021-01-28 18:00:07 -08:00
parent bf193d69fe
commit f620b5ced2
4 changed files with 21 additions and 13 deletions

View file

@ -35,6 +35,12 @@ crate enum OutputFormat {
Html, Html,
} }
impl Default for OutputFormat {
fn default() -> OutputFormat {
OutputFormat::Html
}
}
impl OutputFormat { impl OutputFormat {
crate fn is_json(&self) -> bool { crate fn is_json(&self) -> bool {
matches!(self, OutputFormat::Json) matches!(self, OutputFormat::Json)
@ -118,7 +124,7 @@ crate struct Options {
crate enable_per_target_ignores: bool, crate enable_per_target_ignores: bool,
/// The path to a rustc-like binary to build tests with. If not set, we /// The path to a rustc-like binary to build tests with. If not set, we
/// default to loading from $sysroot/bin/rustc. /// default to loading from `$sysroot/bin/rustc`.
crate test_builder: Option<PathBuf>, crate test_builder: Option<PathBuf>,
// Options that affect the documentation process // Options that affect the documentation process
@ -142,8 +148,10 @@ crate struct Options {
crate crate_version: Option<String>, crate crate_version: Option<String>,
/// Collected options specific to outputting final pages. /// Collected options specific to outputting final pages.
crate render_options: RenderOptions, crate render_options: RenderOptions,
/// Output format rendering (used only for "show-coverage" option for the moment) /// The format that we output when rendering.
crate output_format: Option<OutputFormat>, ///
/// Currently used only for the `--show-coverage` option.
crate output_format: OutputFormat,
/// If this option is set to `true`, rustdoc will only run checks and not generate /// If this option is set to `true`, rustdoc will only run checks and not generate
/// documentation. /// documentation.
crate run_check: bool, crate run_check: bool,
@ -271,7 +279,7 @@ crate struct RenderInfo {
crate deref_trait_did: Option<DefId>, crate deref_trait_did: Option<DefId>,
crate deref_mut_trait_did: Option<DefId>, crate deref_mut_trait_did: Option<DefId>,
crate owned_box_did: Option<DefId>, crate owned_box_did: Option<DefId>,
crate output_format: Option<OutputFormat>, crate output_format: OutputFormat,
} }
impl Options { impl Options {
@ -537,28 +545,28 @@ impl Options {
let output_format = match matches.opt_str("output-format") { let output_format = match matches.opt_str("output-format") {
Some(s) => match OutputFormat::try_from(s.as_str()) { Some(s) => match OutputFormat::try_from(s.as_str()) {
Ok(o) => { Ok(out_fmt) => {
if o.is_json() if out_fmt.is_json()
&& !(show_coverage || nightly_options::match_is_nightly_build(matches)) && !(show_coverage || nightly_options::match_is_nightly_build(matches))
{ {
diag.struct_err("json output format isn't supported for doc generation") diag.struct_err("json output format isn't supported for doc generation")
.emit(); .emit();
return Err(1); return Err(1);
} else if !o.is_json() && show_coverage { } else if !out_fmt.is_json() && show_coverage {
diag.struct_err( diag.struct_err(
"html output format isn't supported for the --show-coverage option", "html output format isn't supported for the --show-coverage option",
) )
.emit(); .emit();
return Err(1); return Err(1);
} }
Some(o) out_fmt
} }
Err(e) => { Err(e) => {
diag.struct_err(&e).emit(); diag.struct_err(&e).emit();
return Err(1); return Err(1);
} }
}, },
None => None, None => OutputFormat::default(),
}; };
let crate_name = matches.opt_str("crate-name"); let crate_name = matches.opt_str("crate-name");
let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro); let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);

View file

@ -460,7 +460,7 @@ crate fn run_global_ctxt(
mut default_passes: passes::DefaultPassOption, mut default_passes: passes::DefaultPassOption,
mut manual_passes: Vec<String>, mut manual_passes: Vec<String>,
render_options: RenderOptions, render_options: RenderOptions,
output_format: Option<OutputFormat>, output_format: OutputFormat,
) -> (clean::Crate, RenderInfo, RenderOptions) { ) -> (clean::Crate, RenderInfo, RenderOptions) {
// Certain queries assume that some checks were run elsewhere // Certain queries assume that some checks were run elsewhere
// (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425), // (see https://github.com/rust-lang/rust/pull/73566#issuecomment-656954425),

View file

@ -578,7 +578,7 @@ fn main_options(options: config::Options) -> MainResult {
let (error_format, edition, debugging_options) = diag_opts; let (error_format, edition, debugging_options) = diag_opts;
let diag = core::new_handler(error_format, None, &debugging_options); let diag = core::new_handler(error_format, None, &debugging_options);
match output_format { match output_format {
None | Some(config::OutputFormat::Html) => sess.time("render_html", || { config::OutputFormat::Html => sess.time("render_html", || {
run_renderer::<html::render::Context<'_>>( run_renderer::<html::render::Context<'_>>(
krate, krate,
render_opts, render_opts,
@ -588,7 +588,7 @@ fn main_options(options: config::Options) -> MainResult {
tcx, tcx,
) )
}), }),
Some(config::OutputFormat::Json) => sess.time("render_json", || { config::OutputFormat::Json => sess.time("render_json", || {
run_renderer::<json::JsonRenderer<'_>>( run_renderer::<json::JsonRenderer<'_>>(
krate, krate,
render_opts, render_opts,

View file

@ -132,7 +132,7 @@ impl<'a, 'b> CoverageCalculator<'a, 'b> {
fn print_results(&self) { fn print_results(&self) {
let output_format = self.ctx.renderinfo.borrow().output_format; let output_format = self.ctx.renderinfo.borrow().output_format;
if output_format.map(|o| o.is_json()).unwrap_or_else(|| false) { if output_format.is_json() {
println!("{}", self.to_json()); println!("{}", self.to_json());
return; return;
} }