Skip to content

Commit d539f45

Browse files
committed
rustdoc: some code style improvements
1 parent 970b86b commit d539f45

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
207207
///
208208
/// Any leading or trailing whitespace will be trimmed.
209209
fn collapse_whitespace(s: &str) -> String {
210-
s.split(|c: char| c.is_whitespace()).filter(|s| {
211-
!s.is_empty()
212-
}).collect::<Vec<_>>().join(" ")
210+
s.split_whitespace().collect::<Vec<_>>().join(" ")
213211
}
214212

215213
thread_local!(static USED_HEADER_MAP: RefCell<HashMap<String, usize>> = {
@@ -277,10 +275,10 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
277275

278276
// Extract the text provided
279277
let s = if text.is_null() {
280-
"".to_string()
278+
"".to_owned()
281279
} else {
282280
let s = unsafe { (*text).as_bytes() };
283-
str::from_utf8(s).unwrap().to_string()
281+
str::from_utf8(&s).unwrap().to_owned()
284282
};
285283

286284
// Discard '<em>', '<code>' tags and some escaped characters,
@@ -322,22 +320,15 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
322320
id
323321
});
324322

325-
let sec = match opaque.toc_builder {
326-
Some(ref mut builder) => {
327-
builder.push(level as u32, s.clone(), id.clone())
328-
}
329-
None => {""}
330-
};
323+
324+
let sec = opaque.toc_builder.as_mut().map_or("".to_owned(), |builder| {
325+
format!("{} ", builder.push(level as u32, s.clone(), id.clone()))
326+
});
331327

332328
// Render the HTML
333329
let text = format!(r##"<h{lvl} id="{id}" class='section-header'><a
334330
href="#{id}">{sec}{}</a></h{lvl}>"##,
335-
s, lvl = level, id = id,
336-
sec = if sec.is_empty() {
337-
sec.to_string()
338-
} else {
339-
format!("{} ", sec)
340-
});
331+
s, lvl = level, id = id, sec = sec);
341332

342333
let text = CString::new(text).unwrap();
343334
unsafe { hoedown_buffer_puts(ob, text.as_ptr()) }
@@ -351,7 +342,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
351342
_: *const hoedown_renderer_data,
352343
) -> libc::c_int {
353344
let content = if text.is_null() {
354-
"".to_string()
345+
"".to_owned()
355346
} else {
356347
let bytes = unsafe { (*text).as_bytes() };
357348
let s = str::from_utf8(bytes).unwrap();
@@ -385,10 +376,9 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
385376

386377
hoedown_html_renderer_free(renderer);
387378

388-
let mut ret = match opaque.toc_builder {
389-
Some(b) => write!(w, "<nav id=\"TOC\">{}</nav>", b.into_toc()),
390-
None => Ok(())
391-
};
379+
let mut ret = opaque.toc_builder.map_or(Ok(()), |builder| {
380+
write!(w, "<nav id=\"TOC\">{}</nav>", builder.into_toc())
381+
});
392382

393383
if ret.is_ok() {
394384
let buf = (*ob).as_bytes();
@@ -422,7 +412,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
422412
stripped_filtered_line(l).unwrap_or(l)
423413
});
424414
let text = lines.collect::<Vec<&str>>().join("\n");
425-
tests.add_test(text.to_string(),
415+
tests.add_test(text.to_owned(),
426416
block_info.should_panic, block_info.no_run,
427417
block_info.ignore, block_info.test_harness);
428418
}
@@ -578,10 +568,7 @@ pub fn plain_summary_line(md: &str) -> String {
578568
md.len() as libc::size_t);
579569
hoedown_document_free(document);
580570
let plain_slice = (*ob).as_bytes();
581-
let plain = match str::from_utf8(plain_slice) {
582-
Ok(s) => s.to_string(),
583-
Err(_) => "".to_string(),
584-
};
571+
let plain = str::from_utf8(plain_slice).unwrap_or("").to_owned();
585572
hoedown_buffer_free(ob);
586573
plain
587574
}

0 commit comments

Comments
 (0)