Skip to content

Commit

Permalink
Rollup merge of #138107 - yotamofek:pr/rustdoc/clippy, r=GuillaumeGomez
Browse files Browse the repository at this point in the history
`librustdoc`: clippy fixes

First commit is all machine-generated fixes,
next two are some more lints fixed by hand/misc. cleanups

Inspired by the redundant `.and_then()` added in #137320 , and [this comment](#138090 (comment))

r? ``@GuillaumeGomez``
  • Loading branch information
matthiaskrgr authored Mar 7, 2025
2 parents 327ead3 + 5d25922 commit d207e21
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 49 deletions.
5 changes: 2 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2532,7 +2532,7 @@ fn clean_generic_args<'tcx>(
) -> GenericArgs {
// FIXME(return_type_notation): Fix RTN parens rendering
if let Some((inputs, output)) = generic_args.paren_sugar_inputs_output() {
let inputs = inputs.iter().map(|x| clean_ty(x, cx)).collect::<ThinVec<_>>().into();
let inputs = inputs.iter().map(|x| clean_ty(x, cx)).collect();
let output = match output.kind {
hir::TyKind::Tup(&[]) => None,
_ => Some(Box::new(clean_ty(output, cx))),
Expand All @@ -2553,8 +2553,7 @@ fn clean_generic_args<'tcx>(
}
hir::GenericArg::Infer(_inf) => GenericArg::Infer,
})
.collect::<ThinVec<_>>()
.into();
.collect();
let constraints = generic_args
.constraints
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2417,7 +2417,7 @@ impl ConstantKind {
ConstantKind::Local { body, .. } | ConstantKind::Anonymous { body } => {
rendered_const(tcx, tcx.hir_body(body), tcx.hir_body_owner_def_id(body))
}
ConstantKind::Infer { .. } => "_".to_string(),
ConstantKind::Infer => "_".to_string(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ fn clean_middle_generic_args_with_constraints<'tcx>(

let args = clean_middle_generic_args(cx, args.map_bound(|args| &args[..]), has_self, did);

GenericArgs::AngleBracketed { args: args.into(), constraints }
GenericArgs::AngleBracketed { args, constraints }
}

pub(super) fn clean_middle_path<'tcx>(
Expand Down Expand Up @@ -394,7 +394,7 @@ pub(crate) fn print_evaluated_const(
fn format_integer_with_underscore_sep(num: &str) -> String {
let num_chars: Vec<_> = num.chars().collect();
let mut num_start_index = if num_chars.first() == Some(&'-') { 1 } else { 0 };
let chunk_size = match num[num_start_index..].as_bytes() {
let chunk_size = match &num.as_bytes()[num_start_index..] {
[b'0', b'b' | b'x', ..] => {
num_start_index += 2;
4
Expand Down Expand Up @@ -524,7 +524,7 @@ pub(crate) fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
| AssocConst
| Variant
| Fn
| TyAlias { .. }
| TyAlias
| Enum
| Trait
| Struct
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ where
let mut iter = self.into_iter();
let Some(first) = iter.next() else { return Ok(()) };
first.fmt(f)?;
while let Some(item) = iter.next() {
for item in iter {
f.write_str(sep)?;
item.fmt(f)?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/doctest/extracted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl ExtractedDocTests {
opts: &super::GlobalTestOptions,
options: &RustdocOptions,
) {
let edition = scraped_test.edition(&options);
let edition = scraped_test.edition(options);

let ScrapedDocTest { filename, line, langstr, text, name } = scraped_test;

Expand All @@ -48,7 +48,7 @@ impl ExtractedDocTests {
let (full_test_code, size) = doctest.generate_unique_doctest(
&text,
langstr.test_harness,
&opts,
opts,
Some(&opts.crate_name),
);
self.doctests.push(ExtractedDocTest {
Expand Down
12 changes: 5 additions & 7 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,9 @@ pub(crate) fn href_relative_parts<'fqp>(
// e.g. linking to std::iter from std::vec (`dissimilar_part_count` will be 1)
if f != r {
let dissimilar_part_count = relative_to_fqp.len() - i;
let fqp_module = &fqp[i..fqp.len()];
let fqp_module = &fqp[i..];
return Box::new(
iter::repeat(sym::dotdot)
.take(dissimilar_part_count)
iter::repeat_n(sym::dotdot, dissimilar_part_count)
.chain(fqp_module.iter().copied()),
);
}
Expand All @@ -639,7 +638,7 @@ pub(crate) fn href_relative_parts<'fqp>(
Ordering::Greater => {
// e.g. linking to std::sync from std::sync::atomic
let dissimilar_part_count = relative_to_fqp.len() - fqp.len();
Box::new(iter::repeat(sym::dotdot).take(dissimilar_part_count))
Box::new(iter::repeat_n(sym::dotdot, dissimilar_part_count))
}
Ordering::Equal => {
// linking to the same module
Expand Down Expand Up @@ -770,10 +769,9 @@ fn primitive_link_fragment(
ExternalLocation::Local => {
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
Some(if cx.current.first() == Some(&cname_sym) {
iter::repeat(sym::dotdot).take(cx.current.len() - 1).collect()
iter::repeat_n(sym::dotdot, cx.current.len() - 1).collect()
} else {
iter::repeat(sym::dotdot)
.take(cx.current.len())
iter::repeat_n(sym::dotdot, cx.current.len())
.chain(iter::once(cname_sym))
.collect()
})
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ fn write_header(
}

if let Some(extra) = extra_content {
out.push_str(&extra);
out.push_str(extra);
}
if class.is_empty() {
write_str(
Expand Down Expand Up @@ -131,7 +131,7 @@ fn write_header(
/// * If the other `Class` is unclassified and only contains white characters (backline,
/// whitespace, etc), it can be merged.
/// * `Class::Ident` is considered the same as unclassified (because it doesn't have an associated
/// CSS class).
/// CSS class).
fn can_merge(class1: Option<Class>, class2: Option<Class>, text: &str) -> bool {
match (class1, class2) {
(Some(c1), Some(c2)) => c1.is_equal_to(c2),
Expand Down Expand Up @@ -233,7 +233,7 @@ impl<F: Write> TokenHandler<'_, '_, F> {

#[inline]
fn write_line_number(&mut self, line: u32, extra: &'static str) {
(self.write_line_number)(&mut self.out, line, extra);
(self.write_line_number)(self.out, line, extra);
}
}

Expand Down Expand Up @@ -610,7 +610,7 @@ impl Decorations {
let (mut starts, mut ends): (Vec<_>, Vec<_>) = info
.0
.iter()
.flat_map(|(&kind, ranges)| ranges.into_iter().map(move |&(lo, hi)| ((lo, kind), hi)))
.flat_map(|(&kind, ranges)| ranges.iter().map(move |&(lo, hi)| ((lo, kind), hi)))
.unzip();

// Sort the sequences in document order.
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1791,7 +1791,7 @@ pub(crate) fn markdown_links<'md, R>(
}
}
} else if !c.is_ascii_whitespace() {
while let Some((j, c)) = iter.next() {
for (j, c) in iter.by_ref() {
if c.is_ascii_whitespace() {
return MarkdownLinkRange::Destination(i + span.start..j + span.start);
}
Expand Down
18 changes: 7 additions & 11 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::cmp::Ordering;
use std::fmt;
use std::fmt::{Display, Write as _};
use std::fmt::{self, Display, Write as _};
use std::iter;

use rinja::Template;
use rustc_abi::VariantIdx;
Expand Down Expand Up @@ -1192,10 +1192,8 @@ fn item_trait(cx: &Context<'_>, it: &clean::Item, t: &clean::Trait) -> impl fmt:
// to already be in the HTML, and will be ignored.
//
// [JSONP]: https://en.wikipedia.org/wiki/JSONP
let mut js_src_path: UrlPartsBuilder = std::iter::repeat("..")
.take(cx.current.len())
.chain(std::iter::once("trait.impl"))
.collect();
let mut js_src_path: UrlPartsBuilder =
iter::repeat_n("..", cx.current.len()).chain(iter::once("trait.impl")).collect();
if let Some(did) = it.item_id.as_def_id()
&& let get_extern = { || cx.shared.cache.external_paths.get(&did).map(|s| &s.0) }
&& let Some(fqp) = cx.shared.cache.exact_paths.get(&did).or_else(get_extern)
Expand Down Expand Up @@ -1446,10 +1444,8 @@ fn item_type_alias(cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) ->
&& let get_local = { || cache.paths.get(&self_did).map(|(p, _)| p) }
&& let Some(self_fqp) = cache.exact_paths.get(&self_did).or_else(get_local)
{
let mut js_src_path: UrlPartsBuilder = std::iter::repeat("..")
.take(cx.current.len())
.chain(std::iter::once("type.impl"))
.collect();
let mut js_src_path: UrlPartsBuilder =
iter::repeat_n("..", cx.current.len()).chain(iter::once("type.impl")).collect();
js_src_path.extend(target_fqp[..target_fqp.len() - 1].iter().copied());
js_src_path.push_fmt(format_args!("{target_type}.{}.js", target_fqp.last().unwrap()));
let self_path = fmt::from_fn(|f| self_fqp.iter().joined("::", f));
Expand Down Expand Up @@ -1493,7 +1489,7 @@ fn item_union(cx: &Context<'_>, it: &clean::Item, s: &clean::Union) -> impl fmt:

fn fields_iter(
&self,
) -> std::iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>> {
) -> iter::Peekable<impl Iterator<Item = (&'a clean::Item, &'a clean::Type)>> {
self.s
.fields
.iter()
Expand Down
5 changes: 1 addition & 4 deletions src/librustdoc/html/render/search_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,10 +842,7 @@ pub(crate) fn get_function_type_for_search(
}
clean::ConstantItem(ref c) => make_nullary_fn(&c.type_),
clean::StaticItem(ref s) => make_nullary_fn(&s.type_),
clean::StructFieldItem(ref t) => {
let Some(parent) = parent else {
return None;
};
clean::StructFieldItem(ref t) if let Some(parent) = parent => {
let mut rgen: FxIndexMap<SimplifiedParam, (isize, Vec<RenderType>)> =
Default::default();
let output = get_index_type(t, vec![], &mut rgen);
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/search_index/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ pub(crate) fn write_bitmap_to_bytes(
out.write_all(&[b])?;
}
if size < NO_OFFSET_THRESHOLD {
4 + 4 * size + ((size + 7) / 8)
4 + 4 * size + size.div_ceil(8)
} else {
4 + 8 * size + ((size + 7) / 8)
4 + 8 * size + size.div_ceil(8)
}
} else {
out.write_all(&u32::to_le_bytes(SERIAL_COOKIE_NO_RUNCONTAINER))?;
Expand Down
18 changes: 12 additions & 6 deletions src/librustdoc/html/render/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'a> LinkBlock<'a> {
}

/// A link to an item. Content should not be escaped.
#[derive(Ord, PartialEq, Eq, Hash, Clone)]
#[derive(PartialEq, Eq, Hash, Clone)]
pub(crate) struct Link<'a> {
/// The content for the anchor tag and title attr
name: Cow<'a, str>,
Expand All @@ -91,20 +91,26 @@ pub(crate) struct Link<'a> {
children: Vec<Link<'a>>,
}

impl PartialOrd for Link<'_> {
fn partial_cmp(&self, other: &Link<'_>) -> Option<Ordering> {
impl Ord for Link<'_> {
fn cmp(&self, other: &Self) -> Ordering {
match compare_names(&self.name, &other.name) {
Ordering::Equal => (),
result => return Some(result),
Ordering::Equal => {}
result => return result,
}
(&self.name_html, &self.href, &self.children).partial_cmp(&(
(&self.name_html, &self.href, &self.children).cmp(&(
&other.name_html,
&other.href,
&other.children,
))
}
}

impl PartialOrd for Link<'_> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl<'a> Link<'a> {
pub fn new(href: impl Into<Cow<'a, str>>, name: impl Into<Cow<'a, str>>) -> Self {
Self { href: href.into(), name: name.into(), children: vec![], name_html: None }
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2057,7 +2057,7 @@ fn resolution_failure(
return;
}
Trait
| TyAlias { .. }
| TyAlias
| ForeignTy
| OpaqueTy
| TraitAlias
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/passes/propagate_stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ impl DocFolder for StabilityPropagator<'_, '_> {
let item_stability = self.cx.tcx.lookup_stability(def_id);
let inline_stability =
item.inline_stmt_id.and_then(|did| self.cx.tcx.lookup_stability(did));
let is_glob_export = item.inline_stmt_id.and_then(|id| {
let is_glob_export = item.inline_stmt_id.map(|id| {
let hir_id = self.cx.tcx.local_def_id_to_hir_id(id);
Some(matches!(
matches!(
self.cx.tcx.hir_node(hir_id),
rustc_hir::Node::Item(rustc_hir::Item {
kind: rustc_hir::ItemKind::Use(_, rustc_hir::UseKind::Glob),
..
})
))
)
});
let own_stability = if let Some(item_stab) = item_stability
&& let StabilityLevel::Stable { since: _, allowed_through_unstable_modules } =
Expand Down

0 comments on commit d207e21

Please sign in to comment.