diff options
Diffstat (limited to 'src/librustdoc/html/render/mod.rs')
| -rw-r--r-- | src/librustdoc/html/render/mod.rs | 143 |
1 files changed, 75 insertions, 68 deletions
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 97e7c38ecb8..f7eb136a754 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -30,7 +30,6 @@ crate mod cache; #[cfg(test)] mod tests; -use std::borrow::Cow; use std::cell::{Cell, RefCell}; use std::cmp::Ordering; use std::collections::{BTreeMap, VecDeque}; @@ -198,12 +197,8 @@ impl SharedContext<'_> { /// Based on whether the `collapse-docs` pass was run, return either the `doc_value` or the /// `collapsed_doc_value` of the given item. - crate fn maybe_collapsed_doc_value<'a>(&self, item: &'a clean::Item) -> Option<Cow<'a, str>> { - if self.collapsed { - item.collapsed_doc_value().map(|s| s.into()) - } else { - item.doc_value().map(|s| s.into()) - } + crate fn maybe_collapsed_doc_value<'a>(&self, item: &'a clean::Item) -> Option<String> { + if self.collapsed { item.collapsed_doc_value() } else { item.doc_value() } } } @@ -538,7 +533,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { fn after_krate(&mut self, krate: &clean::Crate, cache: &Cache) -> Result<(), Error> { let final_file = self.dst.join(&*krate.name.as_str()).join("all.html"); let settings_file = self.dst.join("settings.html"); - let crate_name = krate.name.clone(); + let crate_name = krate.name; let mut root_path = self.dst.to_str().expect("invalid path").to_owned(); if !root_path.ends_with('/') { @@ -633,7 +628,7 @@ 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 { + let module = match *item.kind { clean::StrippedItem(box clean::ModuleItem(ref m)) | clean::ModuleItem(ref m) => m, _ => unreachable!(), }; @@ -979,7 +974,7 @@ themePicker.onblur = handleThemeButtonsBlur; .iter() .map(|s| format!("\"{}\"", s.to_str().expect("invalid osstring conversion"))) .collect::<Vec<_>>(); - files.sort_unstable_by(|a, b| a.cmp(b)); + files.sort_unstable(); let subs = subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(","); let dirs = if subs.is_empty() { String::new() } else { format!(",\"dirs\":[{}]", subs) }; @@ -1428,7 +1423,7 @@ impl Setting { .map(|opt| format!( "<option value=\"{}\" {}>{}</option>", opt.0, - if &opt.0 == default_value { "selected" } else { "" }, + if opt.0 == default_value { "selected" } else { "" }, opt.1, )) .collect::<String>(), @@ -1595,7 +1590,7 @@ impl Context<'_> { if let Some(&(ref names, ty)) = cache.paths.get(&it.def_id) { for name in &names[..names.len() - 1] { url.push_str(name); - url.push_str("/"); + url.push('/'); } url.push_str(&item_path(ty, names.last().unwrap())); layout::redirect(&url) @@ -1622,7 +1617,7 @@ impl Context<'_> { let short = short.to_string(); map.entry(short).or_default().push(( myname, - Some(item.doc_value().map_or_else(|| String::new(), plain_text_summary)), + Some(item.doc_value().map_or_else(String::new, |s| plain_text_summary(&s))), )); } @@ -1739,7 +1734,7 @@ fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer, cache: &Ca write!(buf, "</span>"); // out-of-band write!(buf, "<span class=\"in-band\">"); - let name = match item.kind { + let name = match *item.kind { clean::ModuleItem(ref m) => { if m.is_crate { "Crate " @@ -1788,7 +1783,7 @@ fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer, cache: &Ca write!(buf, "</span></h1>"); // in-band - match item.kind { + match *item.kind { clean::ModuleItem(ref m) => item_module(buf, cx, item, &m.items), clean::FunctionItem(ref f) | clean::ForeignFunctionItem(ref f) => { item_function(buf, cx, item, f) @@ -1880,7 +1875,7 @@ fn document_short( return; } if let Some(s) = item.doc_value() { - let mut summary_html = MarkdownSummaryLine(s, &item.links()).into_string(); + let mut summary_html = MarkdownSummaryLine(&s, &item.links()).into_string(); if s.contains('\n') { let link = format!(r#" <a href="{}">Read more</a>"#, naive_assoc_href(item, link)); @@ -2086,8 +2081,8 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl (true, false) => return Ordering::Greater, } } - let lhs = i1.name.unwrap_or(kw::Invalid).as_str(); - let rhs = i2.name.unwrap_or(kw::Invalid).as_str(); + let lhs = i1.name.unwrap_or(kw::Empty).as_str(); + let rhs = i2.name.unwrap_or(kw::Empty).as_str(); compare_names(&lhs, &rhs) } @@ -2149,7 +2144,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl ); } - match myitem.kind { + match *myitem.kind { clean::ExternCrateItem(ref name, ref src) => { use crate::html::format::anchor; @@ -2157,14 +2152,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl Some(ref src) => write!( w, "<tr><td><code>{}extern crate {} as {};", - myitem.visibility.print_with_space(cx.tcx()), + myitem.visibility.print_with_space(cx.tcx(), myitem.def_id), anchor(myitem.def_id, &*src.as_str()), name ), None => write!( w, "<tr><td><code>{}extern crate {};", - myitem.visibility.print_with_space(cx.tcx()), + myitem.visibility.print_with_space(cx.tcx(), myitem.def_id), anchor(myitem.def_id, &*name.as_str()) ), } @@ -2175,7 +2170,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl write!( w, "<tr><td><code>{}{}</code></td></tr>", - myitem.visibility.print_with_space(cx.tcx()), + myitem.visibility.print_with_space(cx.tcx(), myitem.def_id), import.print() ); } @@ -2185,7 +2180,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl continue; } - let unsafety_flag = match myitem.kind { + let unsafety_flag = match *myitem.kind { clean::FunctionItem(ref func) | clean::ForeignFunctionItem(ref func) if func.header.unsafety == hir::Unsafety::Unsafe => { @@ -2197,7 +2192,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl let stab = myitem.stability_class(cx.tcx()); let add = if stab.is_some() { " " } else { "" }; - let doc_value = myitem.doc_value().unwrap_or(""); + let doc_value = myitem.doc_value().unwrap_or_default(); write!( w, "<tr class=\"{stab}{add}module-item\">\ @@ -2207,7 +2202,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl </tr>", name = *myitem.name.as_ref().unwrap(), stab_tags = extra_info_tags(myitem, item, cx.tcx()), - docs = MarkdownSummaryLine(doc_value, &myitem.links()).into_string(), + docs = MarkdownSummaryLine(&doc_value, &myitem.links()).into_string(), class = myitem.type_(), add = add, stab = stab.unwrap_or_else(String::new), @@ -2308,7 +2303,7 @@ fn short_item_info( let since = &since.as_str(); if !stability::deprecation_in_effect(is_since_rustc_version, Some(since)) { if *since == "TBD" { - format!("Deprecating in a future Rust version") + String::from("Deprecating in a future Rust version") } else { format!("Deprecating in {}", Escape(since)) } @@ -2392,7 +2387,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean:: write!( w, "{vis}const {name}: {typ}", - vis = it.visibility.print_with_space(cx.tcx()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id), name = it.name.as_ref().unwrap(), typ = c.type_.print(), ); @@ -2426,7 +2421,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St write!( w, "{vis}static {mutability}{name}: {typ}</pre>", - vis = it.visibility.print_with_space(cx.tcx()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id), mutability = s.mutability.print_with_space(), name = it.name.as_ref().unwrap(), typ = s.type_.print() @@ -2437,7 +2432,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) { let header_len = format!( "{}{}{}{}{:#}fn {}{:#}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id), f.header.constness.print_with_space(), f.header.asyncness.print_with_space(), f.header.unsafety.print_with_space(), @@ -2452,7 +2447,7 @@ fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean:: w, "{vis}{constness}{asyncness}{unsafety}{abi}fn \ {name}{generics}{decl}{spotlight}{where_clause}</pre>", - vis = it.visibility.print_with_space(cx.tcx()), + vis = it.visibility.print_with_space(cx.tcx(), it.def_id), constness = f.header.constness.print_with_space(), asyncness = f.header.asyncness.print_with_space(), unsafety = f.header.unsafety.print_with_space(), @@ -2578,7 +2573,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra write!( w, "{}{}{}trait {}{}{}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id), t.unsafety.print_with_space(), if t.is_auto { "auto " } else { "" }, it.name.as_ref().unwrap(), @@ -2624,7 +2619,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra } for (pos, m) in provided.iter().enumerate() { render_assoc_item(w, m, AssocItemLink::Anchor(None), ItemType::Trait, cx); - match m.kind { + match *m.kind { clean::MethodItem(ref inner, _) if !inner.generics.where_predicates.is_empty() => { @@ -2896,7 +2891,7 @@ fn assoc_const( w, "{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}", extra, - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id), naive_assoc_href(it, link), it.name.as_ref().unwrap(), ty.print() @@ -3015,7 +3010,7 @@ fn render_assoc_item( }; let mut header_len = format!( "{}{}{}{}{}{:#}fn {}{:#}", - meth.visibility.print_with_space(cx.tcx()), + meth.visibility.print_with_space(cx.tcx(), meth.def_id), header.constness.print_with_space(), header.asyncness.print_with_space(), header.unsafety.print_with_space(), @@ -3037,7 +3032,7 @@ fn render_assoc_item( "{}{}{}{}{}{}{}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\ {generics}{decl}{spotlight}{where_clause}", if parent == ItemType::Trait { " " } else { "" }, - meth.visibility.print_with_space(cx.tcx()), + meth.visibility.print_with_space(cx.tcx(), meth.def_id), header.constness.print_with_space(), header.asyncness.print_with_space(), header.unsafety.print_with_space(), @@ -3051,7 +3046,7 @@ fn render_assoc_item( where_clause = WhereClause { gens: g, indent, end_newline } ) } - match item.kind { + match *item.kind { clean::StrippedItem(..) => {} clean::TyMethodItem(ref m) => { method(w, item, m.header, &m.generics, &m.decl, link, parent, cx) @@ -3098,7 +3093,7 @@ fn item_struct( let mut fields = s .fields .iter() - .filter_map(|f| match f.kind { + .filter_map(|f| match *f.kind { clean::StructFieldItem(ref ty) => Some((f, ty)), _ => None, }) @@ -3148,7 +3143,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni let mut fields = s .fields .iter() - .filter_map(|f| match f.kind { + .filter_map(|f| match *f.kind { clean::StructFieldItem(ref ty) => Some((f, ty)), _ => None, }) @@ -3189,7 +3184,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum write!( w, "{}enum {}{}{}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id), it.name.as_ref().unwrap(), e.generics.print(), WhereClause { gens: &e.generics, indent: 0, end_newline: true } @@ -3201,7 +3196,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum for v in &e.variants { write!(w, " "); let name = v.name.as_ref().unwrap(); - match v.kind { + match *v.kind { clean::VariantItem(ref var) => match var.kind { clean::VariantKind::CLike => write!(w, "{}", name), clean::VariantKind::Tuple(ref tys) => { @@ -3251,7 +3246,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum id = id, name = variant.name.as_ref().unwrap() ); - if let clean::VariantItem(ref var) = variant.kind { + if let clean::VariantItem(ref var) = *variant.kind { if let clean::VariantKind::Tuple(ref tys) = var.kind { write!(w, "("); for (i, ty) in tys.iter().enumerate() { @@ -3268,7 +3263,8 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum document_non_exhaustive(w, variant); use crate::clean::{Variant, VariantKind}; - if let clean::VariantItem(Variant { kind: VariantKind::Struct(ref s) }) = variant.kind { + if let clean::VariantItem(Variant { kind: VariantKind::Struct(ref s) }) = *variant.kind + { let variant_id = cx.derive_id(format!( "{}.{}.fields", ItemType::Variant, @@ -3282,7 +3278,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum ); for field in &s.fields { use crate::clean::StructFieldItem; - if let StructFieldItem(ref ty) = field.kind { + if let StructFieldItem(ref ty) = *field.kind { let id = cx.derive_id(format!( "variant.{}.field.{}", variant.name.as_ref().unwrap(), @@ -3364,7 +3360,7 @@ fn render_struct( write!( w, "{}{}{}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id), if structhead { "struct " } else { "" }, it.name.as_ref().unwrap() ); @@ -3379,12 +3375,12 @@ fn render_struct( let mut has_visible_fields = false; write!(w, " {{"); for field in fields { - if let clean::StructFieldItem(ref ty) = field.kind { + if let clean::StructFieldItem(ref ty) = *field.kind { write!( w, "\n{} {}{}: {},", tab, - field.visibility.print_with_space(cx.tcx()), + field.visibility.print_with_space(cx.tcx(), field.def_id), field.name.as_ref().unwrap(), ty.print() ); @@ -3410,10 +3406,15 @@ fn render_struct( if i > 0 { write!(w, ", "); } - match field.kind { + match *field.kind { clean::StrippedItem(box clean::StructFieldItem(..)) => write!(w, "_"), clean::StructFieldItem(ref ty) => { - write!(w, "{}{}", field.visibility.print_with_space(cx.tcx()), ty.print()) + write!( + w, + "{}{}", + field.visibility.print_with_space(cx.tcx(), field.def_id), + ty.print() + ) } _ => unreachable!(), } @@ -3446,7 +3447,7 @@ fn render_union( write!( w, "{}{}{}", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id), if structhead { "union " } else { "" }, it.name.as_ref().unwrap() ); @@ -3457,11 +3458,11 @@ fn render_union( write!(w, " {{\n{}", tab); for field in fields { - if let clean::StructFieldItem(ref ty) = field.kind { + if let clean::StructFieldItem(ref ty) = *field.kind { write!( w, " {}{}: {},\n{}", - field.visibility.print_with_space(cx.tcx()), + field.visibility.print_with_space(cx.tcx(), field.def_id), field.name.as_ref().unwrap(), ty.print(), tab @@ -3482,7 +3483,7 @@ enum AssocItemLink<'a> { } impl<'a> AssocItemLink<'a> { - fn anchor(&self, id: &'a String) -> Self { + fn anchor(&self, id: &'a str) -> Self { match *self { AssocItemLink::Anchor(_) => AssocItemLink::Anchor(Some(&id)), ref other => *other, @@ -3619,7 +3620,7 @@ fn render_deref_methods( .inner_impl() .items .iter() - .find_map(|item| match item.kind { + .find_map(|item| match *item.kind { clean::TypedefItem(ref t, true) => Some(match *t { clean::Typedef { item_type: Some(ref type_), .. } => (type_, &t.type_), _ => (&t.type_, &t.type_), @@ -3641,7 +3642,7 @@ fn render_deref_methods( } fn should_render_item(item: &clean::Item, deref_mut_: bool) -> bool { - let self_type_opt = match item.kind { + let self_type_opt = match *item.kind { clean::MethodItem(ref method, _) => method.decl.self_type(), clean::TyMethodItem(ref method) => method.decl.self_type(), _ => None, @@ -3692,7 +3693,7 @@ fn spotlight_decl(decl: &clean::FnDecl) -> String { )); let t_did = impl_.trait_.def_id().unwrap(); for it in &impl_.items { - if let clean::TypedefItem(ref tydef, _) = it.kind { + if let clean::TypedefItem(ref tydef, _) = *it.kind { out.push_str("<span class=\"where fmt-newline\"> "); assoc_type( &mut out, @@ -3764,7 +3765,7 @@ fn render_impl( fmt_impl_for_trait_page(&i.inner_impl(), w, use_absolute); if show_def_docs { for it in &i.inner_impl().items { - if let clean::TypedefItem(ref tydef, _) = it.kind { + if let clean::TypedefItem(ref tydef, _) = *it.kind { write!(w, "<span class=\"where fmt-newline\"> "); assoc_type(w, it, &[], Some(&tydef.type_), AssocItemLink::Anchor(None), ""); write!(w, ";</span>"); @@ -3846,7 +3847,7 @@ fn render_impl( } else { (true, " hidden") }; - match item.kind { + match *item.kind { clean::MethodItem(..) | clean::TyMethodItem(_) => { // Only render when the method is not static or we allow static methods if render_method_item { @@ -3966,7 +3967,7 @@ fn render_impl( cache: &Cache, ) { for trait_item in &t.items { - let n = trait_item.name.clone(); + let n = trait_item.name; if i.items.iter().any(|m| m.name == n) { continue; } @@ -4100,7 +4101,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, cache: write!( w, " {}type {};\n}}</pre>", - it.visibility.print_with_space(cx.tcx()), + it.visibility.print_with_space(cx.tcx(), it.def_id), it.name.as_ref().unwrap(), ); @@ -4123,7 +4124,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer, cache: write!( buffer, "<p class=\"location\">{}{}</p>", - match it.kind { + match *it.kind { clean::StructItem(..) => "Struct ", clean::TraitItem(..) => "Trait ", clean::PrimitiveItem(..) => "Primitive Type ", @@ -4163,7 +4164,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer, cache: it.name.as_ref().expect("crates always have a name") ); } - match it.kind { + match *it.kind { clean::StructItem(ref s) => sidebar_struct(buffer, it, s), clean::TraitItem(ref t) => sidebar_trait(buffer, it, t), clean::PrimitiveItem(_) => sidebar_primitive(buffer, it), @@ -4206,7 +4207,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer, cache: ty: \"{ty}\", \ relpath: \"{path}\"\ }};</script>", - name = it.name.unwrap_or(kw::Invalid), + name = it.name.unwrap_or(kw::Empty), ty = it.type_(), path = relpath ); @@ -4302,8 +4303,9 @@ fn sidebar_assoc_items(it: &clean::Item) -> String { .filter(|i| i.inner_impl().trait_.is_some()) .find(|i| i.inner_impl().trait_.def_id() == c.deref_trait_did) { + debug!("found Deref: {:?}", impl_); if let Some((target, real_target)) = - impl_.inner_impl().items.iter().find_map(|item| match item.kind { + impl_.inner_impl().items.iter().find_map(|item| match *item.kind { clean::TypedefItem(ref t, true) => Some(match *t { clean::Typedef { item_type: Some(ref type_), .. } => (type_, &t.type_), _ => (&t.type_, &t.type_), @@ -4311,17 +4313,21 @@ fn sidebar_assoc_items(it: &clean::Item) -> String { _ => None, }) { + debug!("found target, real_target: {:?} {:?}", target, real_target); let deref_mut = v .iter() .filter(|i| i.inner_impl().trait_.is_some()) .any(|i| i.inner_impl().trait_.def_id() == c.deref_mut_trait_did); let inner_impl = target .def_id() - .or(target - .primitive_type() - .and_then(|prim| c.primitive_locations.get(&prim).cloned())) + .or_else(|| { + target + .primitive_type() + .and_then(|prim| c.primitive_locations.get(&prim).cloned()) + }) .and_then(|did| c.impls.get(&did)); if let Some(impls) = inner_impl { + debug!("found inner_impl: {:?}", impls); out.push_str("<a class=\"sidebar-title\" href=\"#deref-methods\">"); out.push_str(&format!( "Methods from {}<Target={}>", @@ -4442,7 +4448,7 @@ fn get_id_for_impl_on_foreign_type(for_: &clean::Type, trait_: &clean::Type) -> } fn extract_for_impl_name(item: &clean::Item) -> Option<(String, String)> { - match item.kind { + match *item.kind { clean::ItemKind::ImplItem(ref i) => { if let Some(ref trait_) = i.trait_ { Some(( @@ -4593,7 +4599,7 @@ fn sidebar_typedef(buf: &mut Buffer, it: &clean::Item) { fn get_struct_fields_name(fields: &[clean::Item]) -> String { let mut fields = fields .iter() - .filter(|f| if let clean::StructFieldItem(..) = f.kind { true } else { false }) + .filter(|f| matches!(*f.kind, clean::StructFieldItem(..))) .filter_map(|f| match f.name { Some(ref name) => { Some(format!("<a href=\"#structfield.{name}\">{name}</a>", name = name)) @@ -4747,6 +4753,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac Some("macro"), None, None, + it.source.span().edition(), )) }); document(w, cx, it, None) |
