From 565f644edfb83c2e5bc082fea2bf7045639fb179 Mon Sep 17 00:00:00 2001 From: est31 Date: Sat, 19 Feb 2022 00:43:47 +0100 Subject: [PATCH] librustdoc: adopt let else in more places --- src/librustdoc/clean/mod.rs | 11 ++++------- src/librustdoc/clean/simplify.rs | 10 ++-------- src/librustdoc/clean/utils.rs | 6 ++---- src/librustdoc/config.rs | 7 +++---- src/librustdoc/doctest.rs | 12 +++++------- src/librustdoc/externalfiles.rs | 5 +---- src/librustdoc/formats/renderer.rs | 6 ++---- src/librustdoc/html/highlight.rs | 16 ++++++---------- src/librustdoc/html/render/context.rs | 6 ++---- src/librustdoc/html/render/mod.rs | 12 ++---------- src/librustdoc/passes/bare_urls.rs | 10 ++++------ src/librustdoc/passes/check_code_block_syntax.rs | 6 +++--- .../passes/check_doc_test_visibility.rs | 10 ++++------ src/librustdoc/passes/collect_intra_doc_links.rs | 12 +++++------- src/librustdoc/passes/html_tags.rs | 10 +++------- src/librustdoc/passes/unindent_comments.rs | 7 +++---- 16 files changed, 51 insertions(+), 95 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 0d1482ce47017..38c918f691d8b 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -107,9 +107,9 @@ impl Clean> for hir::GenericBound<'_> { let trait_ref = ty::TraitRef::identity(cx.tcx, def_id).skip_binder(); let generic_args = generic_args.clean(cx); - let bindings = match generic_args { - GenericArgs::AngleBracketed { bindings, .. } => bindings, - _ => bug!("clean: parenthesized `GenericBound::LangItemTrait`"), + let GenericArgs::AngleBracketed { bindings, .. } = generic_args + else { + bug!("clean: parenthesized `GenericBound::LangItemTrait`"); }; let trait_ = clean_trait_ref_with_bindings(cx, trait_ref, &bindings); @@ -1282,10 +1282,7 @@ impl Clean for ty::AssocItem { fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type { let hir::Ty { hir_id: _, span, ref kind } = *hir_ty; - let qpath = match kind { - hir::TyKind::Path(qpath) => qpath, - _ => unreachable!(), - }; + let hir::TyKind::Path(qpath) = kind else { unreachable!() }; match qpath { hir::QPath::Resolved(None, ref path) => { diff --git a/src/librustdoc/clean/simplify.rs b/src/librustdoc/clean/simplify.rs index dd8e1132572fc..194c25a795ab4 100644 --- a/src/librustdoc/clean/simplify.rs +++ b/src/librustdoc/clean/simplify.rs @@ -54,14 +54,8 @@ crate fn where_clauses(cx: &DocContext<'_>, clauses: Vec) -> Vec { let Some((self_, trait_did, name)) = lhs.projection() else { return true; }; - let generic = match self_ { - clean::Generic(s) => s, - _ => return true, - }; - let (bounds, _) = match params.get_mut(generic) { - Some(bound) => bound, - None => return true, - }; + let clean::Generic(generic) = self_ else { return true }; + let Some((bounds, _)) = params.get_mut(generic) else { return true }; merge_bounds(cx, bounds, trait_did, name, rhs) }); diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 20eea32560b27..f33bd9429dd4e 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -57,10 +57,8 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate { let primitives = local_crate.primitives(cx.tcx); let keywords = local_crate.keywords(cx.tcx); { - let m = match *module.kind { - ItemKind::ModuleItem(ref mut m) => m, - _ => unreachable!(), - }; + let ItemKind::ModuleItem(ref mut m) = *module.kind + else { unreachable!() }; m.items.extend(primitives.iter().map(|&(def_id, prim)| { Item::from_def_id_and_parts( def_id, diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 93f90b90e0a0d..cee3dcb416f80 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -562,7 +562,7 @@ impl Options { let edition = config::parse_crate_edition(matches); let mut id_map = html::markdown::IdMap::new(); - let external_html = match ExternalHtml::load( + let Some(external_html) = ExternalHtml::load( &matches.opt_strs("html-in-header"), &matches.opt_strs("html-before-content"), &matches.opt_strs("html-after-content"), @@ -573,9 +573,8 @@ impl Options { &mut id_map, edition, &None, - ) { - Some(eh) => eh, - None => return Err(3), + ) else { + return Err(3); }; match matches.opt_str("r").as_deref() { diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index 63a8e85f7c540..51df7313e8637 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -614,13 +614,11 @@ crate fn make_test( (found_main, found_extern_crate, found_macro) }) }); - let (already_has_main, already_has_extern_crate, found_macro) = match result { - Ok(result) => result, - Err(ErrorGuaranteed) => { - // If the parser panicked due to a fatal error, pass the test code through unchanged. - // The error will be reported during compilation. - return (s.to_owned(), 0, false); - } + let Ok((already_has_main, already_has_extern_crate, found_macro)) = result + else { + // If the parser panicked due to a fatal error, pass the test code through unchanged. + // The error will be reported during compilation. + return (s.to_owned(), 0, false); }; // If a doctest's `fn main` is being masked by a wrapper macro, the parsing loop above won't diff --git a/src/librustdoc/externalfiles.rs b/src/librustdoc/externalfiles.rs index 6ed911b8d2409..4df48cef593b6 100644 --- a/src/librustdoc/externalfiles.rs +++ b/src/librustdoc/externalfiles.rs @@ -99,10 +99,7 @@ crate fn load_string>( fn load_external_files(names: &[String], diag: &rustc_errors::Handler) -> Option { let mut out = String::new(); for name in names { - let s = match load_string(name, diag) { - Ok(s) => s, - Err(_) => return None, - }; + let Ok(s) = load_string(name, diag) else { return None }; out.push_str(&s); out.push('\n'); } diff --git a/src/librustdoc/formats/renderer.rs b/src/librustdoc/formats/renderer.rs index b7af8c9801f97..81053042f67d5 100644 --- a/src/librustdoc/formats/renderer.rs +++ b/src/librustdoc/formats/renderer.rs @@ -77,10 +77,8 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>( prof.generic_activity_with_arg("render_mod_item", item.name.unwrap().to_string()); cx.mod_item_in(&item)?; - let module = match *item.kind { - clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m, - _ => unreachable!(), - }; + let (clean::StrippedItem(box clean::ModuleItem(module)) | clean::ModuleItem(module)) = *item.kind + else { unreachable!() }; for it in module.items { debug!("Adding {:?} to worklist", it.name); work.push((cx.make_child_renderer(), it)); diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 06d60b6d06c9c..a0187bd77f880 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -689,16 +689,12 @@ fn string( klass: Option, context_info: &Option>, ) { - let klass = match klass { - None => return write!(out, "{}", text), - Some(klass) => klass, - }; - let def_span = match klass.get_span() { - Some(d) => d, - None => { - write!(out, "{}", klass.as_html(), text); - return; - } + let Some(klass) = klass + else { return write!(out, "{}", text) }; + let Some(def_span) = klass.get_span() + else { + write!(out, "{}", klass.as_html(), text); + return; }; let mut text_s = text.to_string(); if text_s.contains("::") { diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 3f71a53f963e4..34784bbed0cf5 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -655,10 +655,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { // Render sidebar-items.js used throughout this module. if !self.render_redirect_pages { - let module = match *item.kind { - clean::StrippedItem(box clean::ModuleItem(ref m)) | clean::ModuleItem(ref m) => m, - _ => unreachable!(), - }; + let (clean::StrippedItem(box clean::ModuleItem(ref module)) | clean::ModuleItem(ref module)) = *item.kind + else { unreachable!() }; let items = self.build_sidebar_items(module); let js_dst = self.dst.join(&format!("sidebar-items{}.js", self.shared.resource_suffix)); let v = format!("initSidebarItems({});", serde_json::to_string(&items).unwrap()); diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 558dbb3b3965a..d3c9fdc48ddc4 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1058,10 +1058,7 @@ fn render_assoc_items_inner( ) { info!("Documenting associated items of {:?}", containing_item.name); let cache = cx.cache(); - let v = match cache.impls.get(&it) { - Some(v) => v, - None => return, - }; + let Some(v) = cache.impls.get(&it) else { return }; let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none()); if !non_trait.is_empty() { let mut tmp_buf = Buffer::empty_from(w); @@ -2654,12 +2651,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) { let tcx = cx.tcx(); let def_id = item.def_id.expect_def_id(); let key = tcx.def_path_hash(def_id); - let call_locations = match cx.shared.call_locations.get(&key) { - Some(call_locations) => call_locations, - _ => { - return; - } - }; + let Some(call_locations) = cx.shared.call_locations.get(&key) else { return }; // Generate a unique ID so users can link to this section for a given method let id = cx.id_map.borrow_mut().derive("scraped-examples"); diff --git a/src/librustdoc/passes/bare_urls.rs b/src/librustdoc/passes/bare_urls.rs index 3410f46e2a8d2..71fa71750f43a 100644 --- a/src/librustdoc/passes/bare_urls.rs +++ b/src/librustdoc/passes/bare_urls.rs @@ -61,12 +61,10 @@ crate fn check_bare_urls(krate: Crate, cx: &mut DocContext<'_>) -> Crate { impl<'a, 'tcx> DocVisitor for BareUrlsLinter<'a, 'tcx> { fn visit_item(&mut self, item: &Item) { - let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, item.def_id) { - Some(hir_id) => hir_id, - None => { - // If non-local, no need to check anything. - return; - } + let Some(hir_id) = DocContext::as_local_hir_id(self.cx.tcx, item.def_id) + else { + // If non-local, no need to check anything. + return; }; let dox = item.attrs.collapsed_doc_value().unwrap_or_default(); if !dox.is_empty() { diff --git a/src/librustdoc/passes/check_code_block_syntax.rs b/src/librustdoc/passes/check_code_block_syntax.rs index c1dd5894947ca..c4aa31ad91230 100644 --- a/src/librustdoc/passes/check_code_block_syntax.rs +++ b/src/librustdoc/passes/check_code_block_syntax.rs @@ -67,11 +67,11 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> { return; } - let local_id = match item.def_id.as_def_id().and_then(|x| x.as_local()) { - Some(id) => id, + let Some(local_id) = item.def_id.as_def_id().and_then(|x| x.as_local()) + else { // We don't need to check the syntax for other crates so returning // without doing anything should not be a problem. - None => return, + return; }; let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_id); diff --git a/src/librustdoc/passes/check_doc_test_visibility.rs b/src/librustdoc/passes/check_doc_test_visibility.rs index 6cffb52bb875f..f1bb766f467af 100644 --- a/src/librustdoc/passes/check_doc_test_visibility.rs +++ b/src/librustdoc/passes/check_doc_test_visibility.rs @@ -107,12 +107,10 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo } crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) { - let hir_id = match DocContext::as_local_hir_id(cx.tcx, item.def_id) { - Some(hir_id) => hir_id, - None => { - // If non-local, no need to check anything. - return; - } + let Some(hir_id) = DocContext::as_local_hir_id(cx.tcx, item.def_id) + else { + // If non-local, no need to check anything. + return; }; let mut tests = Tests { found_tests: 0 }; diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 3ebd28e83b122..a83f549e6d244 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -1868,13 +1868,11 @@ fn report_diagnostic( DiagnosticInfo { item, ori_link: _, dox, link_range }: &DiagnosticInfo<'_>, decorate: impl FnOnce(&mut Diagnostic, Option), ) { - let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) { - Some(hir_id) => hir_id, - None => { - // If non-local, no need to check anything. - info!("ignoring warning from parent crate: {}", msg); - return; - } + let Some(hir_id) = DocContext::as_local_hir_id(tcx, item.def_id) + else { + // If non-local, no need to check anything. + info!("ignoring warning from parent crate: {}", msg); + return; }; let sp = item.attr_span(tcx); diff --git a/src/librustdoc/passes/html_tags.rs b/src/librustdoc/passes/html_tags.rs index f047f6bb1699b..00e10c6d5a7c7 100644 --- a/src/librustdoc/passes/html_tags.rs +++ b/src/librustdoc/passes/html_tags.rs @@ -197,13 +197,9 @@ fn extract_tags( impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> { fn visit_item(&mut self, item: &Item) { let tcx = self.cx.tcx; - let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) { - Some(hir_id) => hir_id, - None => { - // If non-local, no need to check anything. - return; - } - }; + let Some(hir_id) = DocContext::as_local_hir_id(tcx, item.def_id) + // If non-local, no need to check anything. + else { return }; let dox = item.attrs.collapsed_doc_value().unwrap_or_default(); if !dox.is_empty() { let report_diag = |msg: &str, range: &Range, is_open_tag: bool| { diff --git a/src/librustdoc/passes/unindent_comments.rs b/src/librustdoc/passes/unindent_comments.rs index 6cac31d2f90dc..0f604157291bd 100644 --- a/src/librustdoc/passes/unindent_comments.rs +++ b/src/librustdoc/passes/unindent_comments.rs @@ -80,7 +80,7 @@ fn unindent_fragments(docs: &mut Vec) { // In here, the `min_indent` is 1 (because non-sugared fragment are always counted with minimum // 1 whitespace), meaning that "hello!" will be considered a codeblock because it starts with 4 // (5 - 1) whitespaces. - let min_indent = match docs + let Some(min_indent) = docs .iter() .map(|fragment| { fragment.doc.as_str().lines().fold(usize::MAX, |min_indent, line| { @@ -96,9 +96,8 @@ fn unindent_fragments(docs: &mut Vec) { }) }) .min() - { - Some(x) => x, - None => return, + else { + return; }; for fragment in docs {