Skip to content

Commit b751124

Browse files
committed
Auto merge of rust-lang#94139 - est31:let_else_rustdoc, r=notriddle
librustdoc: adopt let else in more places Continuation of rust-lang#89933, rust-lang#91018, rust-lang#91481, rust-lang#93046, rust-lang#93590, rust-lang#94011. I have extended my clippy lint to also recognize tuple passing and match statements. The diff caused by fixing it is way above 1 thousand lines. Thus, I split it up into multiple pull requests to make reviewing easier. This PR handles librustdoc.
2 parents 6b9b2bd + 565f644 commit b751124

16 files changed

+51
-95
lines changed

Diff for: src/librustdoc/clean/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ impl Clean<Option<GenericBound>> for hir::GenericBound<'_> {
107107
let trait_ref = ty::TraitRef::identity(cx.tcx, def_id).skip_binder();
108108

109109
let generic_args = generic_args.clean(cx);
110-
let bindings = match generic_args {
111-
GenericArgs::AngleBracketed { bindings, .. } => bindings,
112-
_ => bug!("clean: parenthesized `GenericBound::LangItemTrait`"),
110+
let GenericArgs::AngleBracketed { bindings, .. } = generic_args
111+
else {
112+
bug!("clean: parenthesized `GenericBound::LangItemTrait`");
113113
};
114114

115115
let trait_ = clean_trait_ref_with_bindings(cx, trait_ref, &bindings);
@@ -1273,10 +1273,7 @@ impl Clean<Item> for ty::AssocItem {
12731273

12741274
fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &mut DocContext<'_>) -> Type {
12751275
let hir::Ty { hir_id: _, span, ref kind } = *hir_ty;
1276-
let qpath = match kind {
1277-
hir::TyKind::Path(qpath) => qpath,
1278-
_ => unreachable!(),
1279-
};
1276+
let hir::TyKind::Path(qpath) = kind else { unreachable!() };
12801277

12811278
match qpath {
12821279
hir::QPath::Resolved(None, ref path) => {

Diff for: src/librustdoc/clean/simplify.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,8 @@ crate fn where_clauses(cx: &DocContext<'_>, clauses: Vec<WP>) -> Vec<WP> {
5454
let Some((self_, trait_did, name)) = lhs.projection() else {
5555
return true;
5656
};
57-
let generic = match self_ {
58-
clean::Generic(s) => s,
59-
_ => return true,
60-
};
61-
let (bounds, _) = match params.get_mut(generic) {
62-
Some(bound) => bound,
63-
None => return true,
64-
};
57+
let clean::Generic(generic) = self_ else { return true };
58+
let Some((bounds, _)) = params.get_mut(generic) else { return true };
6559

6660
merge_bounds(cx, bounds, trait_did, name, rhs)
6761
});

Diff for: src/librustdoc/clean/utils.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
5757
let primitives = local_crate.primitives(cx.tcx);
5858
let keywords = local_crate.keywords(cx.tcx);
5959
{
60-
let m = match *module.kind {
61-
ItemKind::ModuleItem(ref mut m) => m,
62-
_ => unreachable!(),
63-
};
60+
let ItemKind::ModuleItem(ref mut m) = *module.kind
61+
else { unreachable!() };
6462
m.items.extend(primitives.iter().map(|&(def_id, prim)| {
6563
Item::from_def_id_and_parts(
6664
def_id,

Diff for: src/librustdoc/config.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ impl Options {
562562
let edition = config::parse_crate_edition(matches);
563563

564564
let mut id_map = html::markdown::IdMap::new();
565-
let external_html = match ExternalHtml::load(
565+
let Some(external_html) = ExternalHtml::load(
566566
&matches.opt_strs("html-in-header"),
567567
&matches.opt_strs("html-before-content"),
568568
&matches.opt_strs("html-after-content"),
@@ -573,9 +573,8 @@ impl Options {
573573
&mut id_map,
574574
edition,
575575
&None,
576-
) {
577-
Some(eh) => eh,
578-
None => return Err(3),
576+
) else {
577+
return Err(3);
579578
};
580579

581580
match matches.opt_str("r").as_deref() {

Diff for: src/librustdoc/doctest.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -614,13 +614,11 @@ crate fn make_test(
614614
(found_main, found_extern_crate, found_macro)
615615
})
616616
});
617-
let (already_has_main, already_has_extern_crate, found_macro) = match result {
618-
Ok(result) => result,
619-
Err(ErrorGuaranteed) => {
620-
// If the parser panicked due to a fatal error, pass the test code through unchanged.
621-
// The error will be reported during compilation.
622-
return (s.to_owned(), 0, false);
623-
}
617+
let Ok((already_has_main, already_has_extern_crate, found_macro)) = result
618+
else {
619+
// If the parser panicked due to a fatal error, pass the test code through unchanged.
620+
// The error will be reported during compilation.
621+
return (s.to_owned(), 0, false);
624622
};
625623

626624
// If a doctest's `fn main` is being masked by a wrapper macro, the parsing loop above won't

Diff for: src/librustdoc/externalfiles.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ crate fn load_string<P: AsRef<Path>>(
9999
fn load_external_files(names: &[String], diag: &rustc_errors::Handler) -> Option<String> {
100100
let mut out = String::new();
101101
for name in names {
102-
let s = match load_string(name, diag) {
103-
Ok(s) => s,
104-
Err(_) => return None,
105-
};
102+
let Ok(s) = load_string(name, diag) else { return None };
106103
out.push_str(&s);
107104
out.push('\n');
108105
}

Diff for: src/librustdoc/formats/renderer.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,8 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
7777
prof.generic_activity_with_arg("render_mod_item", item.name.unwrap().to_string());
7878

7979
cx.mod_item_in(&item)?;
80-
let module = match *item.kind {
81-
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
82-
_ => unreachable!(),
83-
};
80+
let (clean::StrippedItem(box clean::ModuleItem(module)) | clean::ModuleItem(module)) = *item.kind
81+
else { unreachable!() };
8482
for it in module.items {
8583
debug!("Adding {:?} to worklist", it.name);
8684
work.push((cx.make_child_renderer(), it));

Diff for: src/librustdoc/html/highlight.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -689,16 +689,12 @@ fn string<T: Display>(
689689
klass: Option<Class>,
690690
context_info: &Option<ContextInfo<'_, '_, '_>>,
691691
) {
692-
let klass = match klass {
693-
None => return write!(out, "{}", text),
694-
Some(klass) => klass,
695-
};
696-
let def_span = match klass.get_span() {
697-
Some(d) => d,
698-
None => {
699-
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
700-
return;
701-
}
692+
let Some(klass) = klass
693+
else { return write!(out, "{}", text) };
694+
let Some(def_span) = klass.get_span()
695+
else {
696+
write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text);
697+
return;
702698
};
703699
let mut text_s = text.to_string();
704700
if text_s.contains("::") {

Diff for: src/librustdoc/html/render/context.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -655,10 +655,8 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
655655

656656
// Render sidebar-items.js used throughout this module.
657657
if !self.render_redirect_pages {
658-
let module = match *item.kind {
659-
clean::StrippedItem(box clean::ModuleItem(ref m)) | clean::ModuleItem(ref m) => m,
660-
_ => unreachable!(),
661-
};
658+
let (clean::StrippedItem(box clean::ModuleItem(ref module)) | clean::ModuleItem(ref module)) = *item.kind
659+
else { unreachable!() };
662660
let items = self.build_sidebar_items(module);
663661
let js_dst = self.dst.join(&format!("sidebar-items{}.js", self.shared.resource_suffix));
664662
let v = format!("initSidebarItems({});", serde_json::to_string(&items).unwrap());

Diff for: src/librustdoc/html/render/mod.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1064,10 +1064,7 @@ fn render_assoc_items_inner(
10641064
) {
10651065
info!("Documenting associated items of {:?}", containing_item.name);
10661066
let cache = cx.cache();
1067-
let v = match cache.impls.get(&it) {
1068-
Some(v) => v,
1069-
None => return,
1070-
};
1067+
let Some(v) = cache.impls.get(&it) else { return };
10711068
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
10721069
if !non_trait.is_empty() {
10731070
let mut tmp_buf = Buffer::empty_from(w);
@@ -2664,12 +2661,7 @@ fn render_call_locations(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item) {
26642661
let tcx = cx.tcx();
26652662
let def_id = item.def_id.expect_def_id();
26662663
let key = tcx.def_path_hash(def_id);
2667-
let call_locations = match cx.shared.call_locations.get(&key) {
2668-
Some(call_locations) => call_locations,
2669-
_ => {
2670-
return;
2671-
}
2672-
};
2664+
let Some(call_locations) = cx.shared.call_locations.get(&key) else { return };
26732665

26742666
// Generate a unique ID so users can link to this section for a given method
26752667
let id = cx.id_map.borrow_mut().derive("scraped-examples");

Diff for: src/librustdoc/passes/bare_urls.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,10 @@ crate fn check_bare_urls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
6161

6262
impl<'a, 'tcx> DocVisitor for BareUrlsLinter<'a, 'tcx> {
6363
fn visit_item(&mut self, item: &Item) {
64-
let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, item.def_id) {
65-
Some(hir_id) => hir_id,
66-
None => {
67-
// If non-local, no need to check anything.
68-
return;
69-
}
64+
let Some(hir_id) = DocContext::as_local_hir_id(self.cx.tcx, item.def_id)
65+
else {
66+
// If non-local, no need to check anything.
67+
return;
7068
};
7169
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
7270
if !dox.is_empty() {

Diff for: src/librustdoc/passes/check_code_block_syntax.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
6767
return;
6868
}
6969

70-
let local_id = match item.def_id.as_def_id().and_then(|x| x.as_local()) {
71-
Some(id) => id,
70+
let Some(local_id) = item.def_id.as_def_id().and_then(|x| x.as_local())
71+
else {
7272
// We don't need to check the syntax for other crates so returning
7373
// without doing anything should not be a problem.
74-
None => return,
74+
return;
7575
};
7676

7777
let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_id);

Diff for: src/librustdoc/passes/check_doc_test_visibility.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,10 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo
107107
}
108108

109109
crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
110-
let hir_id = match DocContext::as_local_hir_id(cx.tcx, item.def_id) {
111-
Some(hir_id) => hir_id,
112-
None => {
113-
// If non-local, no need to check anything.
114-
return;
115-
}
110+
let Some(hir_id) = DocContext::as_local_hir_id(cx.tcx, item.def_id)
111+
else {
112+
// If non-local, no need to check anything.
113+
return;
116114
};
117115

118116
let mut tests = Tests { found_tests: 0 };

Diff for: src/librustdoc/passes/collect_intra_doc_links.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -1897,13 +1897,11 @@ fn report_diagnostic(
18971897
DiagnosticInfo { item, ori_link: _, dox, link_range }: &DiagnosticInfo<'_>,
18981898
decorate: impl FnOnce(&mut Diagnostic, Option<rustc_span::Span>),
18991899
) {
1900-
let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) {
1901-
Some(hir_id) => hir_id,
1902-
None => {
1903-
// If non-local, no need to check anything.
1904-
info!("ignoring warning from parent crate: {}", msg);
1905-
return;
1906-
}
1900+
let Some(hir_id) = DocContext::as_local_hir_id(tcx, item.def_id)
1901+
else {
1902+
// If non-local, no need to check anything.
1903+
info!("ignoring warning from parent crate: {}", msg);
1904+
return;
19071905
};
19081906

19091907
let sp = item.attr_span(tcx);

Diff for: src/librustdoc/passes/html_tags.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,9 @@ fn extract_tags(
197197
impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
198198
fn visit_item(&mut self, item: &Item) {
199199
let tcx = self.cx.tcx;
200-
let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) {
201-
Some(hir_id) => hir_id,
202-
None => {
203-
// If non-local, no need to check anything.
204-
return;
205-
}
206-
};
200+
let Some(hir_id) = DocContext::as_local_hir_id(tcx, item.def_id)
201+
// If non-local, no need to check anything.
202+
else { return };
207203
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
208204
if !dox.is_empty() {
209205
let report_diag = |msg: &str, range: &Range<usize>, is_open_tag: bool| {

Diff for: src/librustdoc/passes/unindent_comments.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn unindent_fragments(docs: &mut Vec<DocFragment>) {
8080
// In here, the `min_indent` is 1 (because non-sugared fragment are always counted with minimum
8181
// 1 whitespace), meaning that "hello!" will be considered a codeblock because it starts with 4
8282
// (5 - 1) whitespaces.
83-
let min_indent = match docs
83+
let Some(min_indent) = docs
8484
.iter()
8585
.map(|fragment| {
8686
fragment.doc.as_str().lines().fold(usize::MAX, |min_indent, line| {
@@ -96,9 +96,8 @@ fn unindent_fragments(docs: &mut Vec<DocFragment>) {
9696
})
9797
})
9898
.min()
99-
{
100-
Some(x) => x,
101-
None => return,
99+
else {
100+
return;
102101
};
103102

104103
for fragment in docs {

0 commit comments

Comments
 (0)