Repository: rust-lang/rust
File: src/librustdoc/html/render/mod.rs (Rust)

Code before:
    item: &clean::Item,
    prefix: &str,
    cx: &Context<'_>,
) -> fmt::Result {
    let _ = render_attributes_in_code_with_options(w, item, prefix, cx, true, None)?;
    Ok(())
}

pub(super) fn render_attributes_in_code_with_options(
    w: &mut impl fmt::Write,
    item: &clean::Item,
    prefix: &str,
    cx: &Context<'_>,
    render_doc_hidden: bool,
    open_tag: Option<&str>,
) -> Result<bool, fmt::Error> {
    let mut wrote_any = false;
    let mut render_attr = |attr: &str| -> fmt::Result {
        if !wrote_any {
            if let Some(open_tag) = open_tag {
                w.write_str(open_tag)?;
            }
            wrote_any = true;
        }
        render_code_attribute(prefix, attr, w)
    };

    if render_doc_hidden && item.is_doc_hidden() {
        render_attr("#[doc(hidden)]")?;
    }
    for attr in &item.attrs.other_attrs {
        let hir::Attribute::Parsed(kind) = attr else { continue };
        let attr = match kind {
            AttributeKind::LinkSection { name, .. } => {
                Cow::Owned(format!("#[unsafe(link_section = {})]", Escape(&format!("{name:?}"))))
            }
            AttributeKind::NoMangle(..) => Cow::Borrowed("#[unsafe(no_mangle)]"),
            AttributeKind::ExportName { name, .. } => {
                Cow::Owned(format!("#[unsafe(export_name = {})]", Escape(&format!("{name:?}"))))
            }
            AttributeKind::NonExhaustive(..) => Cow::Borrowed("#[non_exhaustive]"),
            _ => continue,
        };
        render_attr(attr.as_ref())?;
    }

    if let Some(def_id) = item.def_id()
        && let Some(repr) = repr_attribute(cx.tcx(), cx.cache(), def_id)
    {
        render_attr(&repr)?;
    }

Review comment (suggestion):
In case `open_tag` is `None`, we could remove the need for the `if` check by creating two different closures:

```suggestion
    let mut render_attr: FnMut(&str) -> fmt::Result = if let Some(open_tag) = open_tag {
        |attr: &str| -> fmt::Result {
            if !wrote_any {
                w.write_str(open_tag)?;
                wrote_any = true;
            }
            render_code_attribute(prefix, attr, w)
        }
    } else {
        |attr: &str| -> fmt::Result {
            render_code_attribute(prefix, attr, w)
        }
    };
```

Not sure it compiles but with a few tweaks it should (or use functions instead, it works too).

Code after:
                    r#"<li><a href="{url}">{name}</a></li>"#,
                    url = url,
                    name = call_data.display_name
                )
            })?;
            w.write_str("</ul></div>")?;
        }

        w.write_str("</div></details>")?;
    }

    w.write_str("</div>")
}

fn render_attributes_in_code(
    w: &mut impl fmt::Write,
    item: &clean::Item,
    prefix: &str,
    cx: &Context<'_>,
) -> fmt::Result {
    render_attributes_in_code_with_options(w, item, prefix, cx, true, "")
}

pub(super) fn render_attributes_in_code_with_options(
    w: &mut impl fmt::Write,
    item: &clean::Item,
    prefix: &str,
    cx: &Context<'_>,
    render_doc_hidden: bool,
    open_tag: &str,
) -> fmt::Result {
    w.write_str(open_tag)?;
    if render_doc_hidden && item.is_doc_hidden() {
        render_code_attribute(prefix, "#[doc(hidden)]", w)?;
    }
    for attr in &item.attrs.other_attrs {
        let hir::Attribute::Parsed(kind) = attr else { continue };
        let attr = match kind {
            AttributeKind::LinkSection { name, .. } => {
                Cow::Owned(format!("#[unsafe(link_section = {})]", Escape(&format!("{name:?}"))))
            }
            AttributeKind::NoMangle(..) => Cow::Borrowed("#[unsafe(no_mangle)]"),
            AttributeKind::ExportName { name, .. } => {
                Cow::Owned(format!("#[unsafe(export_name = {})]", Escape(&format!("{name:?}"))))
            }
            AttributeKind::NonExhaustive(..) => Cow::Borrowed("#[non_exhaustive]"),
            _ => continue,
        };
        render_code_attribute(prefix, attr.as_ref(), w)?;
    }