Skip to content

Commit bdc8df4

Browse files
Improve rustdoc code
1 parent 7c002ff commit bdc8df4

File tree

12 files changed

+22
-24
lines changed

12 files changed

+22
-24
lines changed

src/librustdoc/clean/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ fn clean_ty_generics<'tcx>(
894894

895895
// Add back a `Sized` bound if there are no *trait* bounds remaining (incl. `?Sized`).
896896
// Since all potential trait bounds are at the front we can just check the first bound.
897-
if bounds.first().map_or(true, |b| !b.is_trait_bound()) {
897+
if bounds.first().is_none_or(|b| !b.is_trait_bound()) {
898898
bounds.insert(0, GenericBound::sized(cx));
899899
}
900900

@@ -1811,7 +1811,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18111811
}
18121812
TyKind::Slice(ty) => Slice(Box::new(clean_ty(ty, cx))),
18131813
TyKind::Pat(ty, pat) => Type::Pat(Box::new(clean_ty(ty, cx)), format!("{pat:?}").into()),
1814-
TyKind::Array(ty, ref const_arg) => {
1814+
TyKind::Array(ty, const_arg) => {
18151815
// NOTE(min_const_generics): We can't use `const_eval_poly` for constants
18161816
// as we currently do not supply the parent generics to anonymous constants
18171817
// but do allow `ConstKind::Param`.
@@ -2337,7 +2337,7 @@ fn clean_middle_opaque_bounds<'tcx>(
23372337

23382338
// Add back a `Sized` bound if there are no *trait* bounds remaining (incl. `?Sized`).
23392339
// Since all potential trait bounds are at the front we can just check the first bound.
2340-
if bounds.first().map_or(true, |b| !b.is_trait_bound()) {
2340+
if bounds.first().is_none_or(|b| !b.is_trait_bound()) {
23412341
bounds.insert(0, GenericBound::sized(cx));
23422342
}
23432343

src/librustdoc/doctest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
220220
} = interface::run_compiler(config, |compiler| {
221221
let krate = rustc_interface::passes::parse(&compiler.sess);
222222

223-
let collector = rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
223+
let collector = rustc_interface::create_and_enter_global_ctxt(compiler, krate, |tcx| {
224224
let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
225225
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
226226
let opts = scrape_test_config(crate_name, crate_attrs, args_path);

src/librustdoc/doctest/make.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ fn handle_attr(mod_attr_pending: &mut String, source_info: &mut SourceInfo, edit
538538
// If it's complete, then we can clear the pending content.
539539
mod_attr_pending.clear();
540540
} else {
541-
mod_attr_pending.push_str("\n");
541+
mod_attr_pending.push('\n');
542542
}
543543
}
544544

src/librustdoc/formats/cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl DocFolder for CacheBuilder<'_, '_> {
413413
let impl_item = Impl { impl_item: item };
414414
let impl_did = impl_item.def_id();
415415
let trait_did = impl_item.trait_did();
416-
if trait_did.map_or(true, |d| self.cache.traits.contains_key(&d)) {
416+
if trait_did.is_none_or(|d| self.cache.traits.contains_key(&d)) {
417417
for did in dids {
418418
if self.impl_ids.entry(did).or_default().insert(impl_did) {
419419
self.cache.impls.entry(did).or_default().push(impl_item.clone());

src/librustdoc/html/escape.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ impl fmt::Display for EscapeBodyTextWithWbr<'_> {
104104
continue;
105105
}
106106
let is_uppercase = || s.chars().any(|c| c.is_uppercase());
107-
let next_is_uppercase =
108-
|| pk.map_or(true, |(_, t)| t.chars().any(|c| c.is_uppercase()));
109-
let next_is_underscore = || pk.map_or(true, |(_, t)| t.contains('_'));
110-
let next_is_colon = || pk.map_or(true, |(_, t)| t.contains(':'));
107+
let next_is_uppercase = || pk.is_none_or(|(_, t)| t.chars().any(|c| c.is_uppercase()));
108+
let next_is_underscore = || pk.is_none_or(|(_, t)| t.contains('_'));
109+
let next_is_colon = || pk.is_none_or(|(_, t)| t.contains(':'));
111110
// Check for CamelCase.
112111
//
113112
// `i - last > 3` avoids turning FmRadio into Fm<wbr>Radio, which is technically

src/librustdoc/html/markdown.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl<'a, I: Iterator<Item = SpannedEvent<'a>>> Iterator for SpannedLinkReplacer<
480480
type Item = SpannedEvent<'a>;
481481

482482
fn next(&mut self) -> Option<Self::Item> {
483-
let Some((mut event, range)) = self.iter.next() else { return None };
483+
let (mut event, range) = self.iter.next()?;
484484
self.inner.handle_event(&mut event);
485485
// Yield the modified event
486486
Some((event, range))
@@ -2039,7 +2039,7 @@ impl IdMap {
20392039
let candidate = candidate.to_string();
20402040
if is_default_id(&candidate) {
20412041
let id = format!("{}-{}", candidate, 1);
2042-
self.map.insert(candidate.into(), 2);
2042+
self.map.insert(candidate, 2);
20432043
id
20442044
} else {
20452045
candidate
@@ -2052,7 +2052,7 @@ impl IdMap {
20522052
}
20532053
};
20542054

2055-
self.map.insert(id.clone().into(), 1);
2055+
self.map.insert(id.clone(), 1);
20562056
id
20572057
}
20582058

src/librustdoc/html/render/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
748748
&shared.layout,
749749
&page,
750750
"",
751-
scrape_examples_help(&shared),
751+
scrape_examples_help(shared),
752752
&shared.style_files,
753753
);
754754
shared.fs.write(scrape_examples_help_file, v)?;

src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1974,7 +1974,7 @@ fn render_impl(
19741974
.opt_doc_value()
19751975
.map(|dox| {
19761976
Markdown {
1977-
content: &*dox,
1977+
content: &dox,
19781978
links: &i.impl_item.links(cx),
19791979
ids: &mut cx.id_map.borrow_mut(),
19801980
error_codes: cx.shared.codes,

src/librustdoc/html/render/print_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
14201420
ty: &'a clean::Type,
14211421
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
14221422
display_fn(move |f| {
1423-
let v = ty.print(&self.cx);
1423+
let v = ty.print(self.cx);
14241424
write!(f, "{v}")
14251425
})
14261426
}

src/librustdoc/json/conversions.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,15 @@ fn from_clean_item(item: clean::Item, renderer: &JsonRenderer<'_>) -> ItemEnum {
312312
StructFieldItem(f) => ItemEnum::StructField(f.into_json(renderer)),
313313
EnumItem(e) => ItemEnum::Enum(e.into_json(renderer)),
314314
VariantItem(v) => ItemEnum::Variant(v.into_json(renderer)),
315-
FunctionItem(f) => ItemEnum::Function(from_function(f, true, header.unwrap(), renderer)),
315+
FunctionItem(f) => ItemEnum::Function(from_function(*f, true, header.unwrap(), renderer)),
316316
ForeignFunctionItem(f, _) => {
317-
ItemEnum::Function(from_function(f, false, header.unwrap(), renderer))
317+
ItemEnum::Function(from_function(*f, false, header.unwrap(), renderer))
318318
}
319319
TraitItem(t) => ItemEnum::Trait((*t).into_json(renderer)),
320320
TraitAliasItem(t) => ItemEnum::TraitAlias(t.into_json(renderer)),
321-
MethodItem(m, _) => ItemEnum::Function(from_function(m, true, header.unwrap(), renderer)),
321+
MethodItem(m, _) => ItemEnum::Function(from_function(*m, true, header.unwrap(), renderer)),
322322
RequiredMethodItem(m) => {
323-
ItemEnum::Function(from_function(m, false, header.unwrap(), renderer))
323+
ItemEnum::Function(from_function(*m, false, header.unwrap(), renderer))
324324
}
325325
ImplItem(i) => ItemEnum::Impl((*i).into_json(renderer)),
326326
StaticItem(s) => ItemEnum::Static(convert_static(s, rustc_hir::Safety::Safe, renderer)),
@@ -730,12 +730,11 @@ impl FromClean<clean::Impl> for Impl {
730730
}
731731

732732
pub(crate) fn from_function(
733-
function: Box<clean::Function>,
733+
clean::Function { decl, generics }: clean::Function,
734734
has_body: bool,
735735
header: rustc_hir::FnHeader,
736736
renderer: &JsonRenderer<'_>,
737737
) -> Function {
738-
let clean::Function { decl, generics } = *function;
739738
Function {
740739
sig: decl.into_json(renderer),
741740
generics: generics.into_json(renderer),

src/librustdoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ fn main_args(
869869
sess.dcx().fatal("Compilation failed, aborting rustdoc");
870870
}
871871

872-
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
872+
rustc_interface::create_and_enter_global_ctxt(compiler, krate, |tcx| {
873873
let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || {
874874
core::run_global_ctxt(tcx, show_coverage, render_options, output_format)
875875
});

src/librustdoc/passes/collect_intra_doc_links.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,7 @@ fn resolution_failure(
19771977
}
19781978

19791979
if !path_str.contains("::") {
1980-
if disambiguator.map_or(true, |d| d.ns() == MacroNS)
1980+
if disambiguator.is_none_or(|d| d.ns() == MacroNS)
19811981
&& collector
19821982
.cx
19831983
.tcx

0 commit comments

Comments
 (0)