diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index 4b7822cd3a9f..9280701ea3f8 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -2935,11 +2935,11 @@ fn render_attributes_in_code(
let hir::Attribute::Parsed(kind) = attr else { continue };
let attr = match kind {
AttributeKind::LinkSection { name, .. } => {
- Cow::Owned(format!("#[unsafe(link_section = \"{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 = \"{name}\")]"))
+ Cow::Owned(format!("#[unsafe(export_name = {})]", Escape(&format!("{name:?}"))))
}
AttributeKind::NonExhaustive(..) => Cow::Borrowed("#[non_exhaustive]"),
_ => continue,
diff --git a/tests/rustdoc/attribute-rendering.rs b/tests/rustdoc/attribute-rendering.rs
deleted file mode 100644
index fb40d0a9887c..000000000000
--- a/tests/rustdoc/attribute-rendering.rs
+++ /dev/null
@@ -1,8 +0,0 @@
-#![crate_name = "foo"]
-
-//@ has 'foo/fn.f.html'
-//@ has - //*[@'class="code-attribute"]' '#[unsafe(export_name = "f")]'
-//@ has - //*[@'class="rust item-decl"]' 'pub fn f()'
-#[unsafe(export_name = "\
-f")]
-pub fn f() {}
diff --git a/tests/rustdoc/attributes.rs b/tests/rustdoc/attributes.rs
index 33e4e31bec6e..429a42a7252c 100644
--- a/tests/rustdoc/attributes.rs
+++ b/tests/rustdoc/attributes.rs
@@ -9,6 +9,18 @@ pub extern "C" fn f() {}
#[unsafe(export_name = "bar")]
pub extern "C" fn g() {}
+//@ has foo/fn.escape_special.html '//*[@class="code-attribute"]' \
+// '#[unsafe(export_name = "\n\"\n")]'
+#[unsafe(export_name = "\n\"
+")]
+pub extern "C" fn escape_special() {}
+
+// issue:
+//@ has foo/fn.escape_html.html '//*[@class="code-attribute"]' \
+// '#[unsafe(export_name = "")]'
+#[unsafe(export_name = "")]
+pub extern "C" fn escape_html() {}
+
//@ has foo/fn.example.html '//*[@class="code-attribute"]' '#[unsafe(link_section = ".text")]'
#[unsafe(link_section = ".text")]
pub extern "C" fn example() {}
diff --git a/tests/rustdoc/inline_cross/attributes.rs b/tests/rustdoc/inline_cross/attributes.rs
index 4747f8ad67c1..1657b7bdc8f7 100644
--- a/tests/rustdoc/inline_cross/attributes.rs
+++ b/tests/rustdoc/inline_cross/attributes.rs
@@ -1,7 +1,20 @@
+// Ensure that we render attributes on inlined cross-crate re-exported items.
+// issue:
+
//@ aux-crate:attributes=attributes.rs
//@ edition:2021
#![crate_name = "user"]
-//@ has 'user/struct.NonExhaustive.html'
-//@ has - '//*[@class="rust item-decl"]//*[@class="code-attribute"]' '#[non_exhaustive]'
+//@ has 'user/fn.no_mangle.html' '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
+pub use attributes::no_mangle;
+
+//@ has 'user/fn.link_section.html' '//pre[@class="rust item-decl"]' \
+// '#[unsafe(link_section = ".here")]'
+pub use attributes::link_section;
+
+//@ has 'user/fn.export_name.html' '//pre[@class="rust item-decl"]' \
+// '#[unsafe(export_name = "exonym")]'
+pub use attributes::export_name;
+
+//@ has 'user/struct.NonExhaustive.html' '//pre[@class="rust item-decl"]' '#[non_exhaustive]'
pub use attributes::NonExhaustive;
diff --git a/tests/rustdoc/inline_cross/auxiliary/attributes.rs b/tests/rustdoc/inline_cross/auxiliary/attributes.rs
index c6f155d4ba5a..6068d3855858 100644
--- a/tests/rustdoc/inline_cross/auxiliary/attributes.rs
+++ b/tests/rustdoc/inline_cross/auxiliary/attributes.rs
@@ -1,2 +1,11 @@
+#[unsafe(no_mangle)]
+pub fn no_mangle() {}
+
+#[unsafe(link_section = ".here")]
+pub fn link_section() {}
+
+#[unsafe(export_name = "exonym")]
+pub fn export_name() {}
+
#[non_exhaustive]
pub struct NonExhaustive;
diff --git a/tests/rustdoc/reexport/auxiliary/reexports-attrs.rs b/tests/rustdoc/reexport/auxiliary/reexports-attrs.rs
deleted file mode 100644
index 96fa8209cde8..000000000000
--- a/tests/rustdoc/reexport/auxiliary/reexports-attrs.rs
+++ /dev/null
@@ -1,14 +0,0 @@
-#[unsafe(no_mangle)]
-pub fn f0() {}
-
-#[unsafe(link_section = ".here")]
-pub fn f1() {}
-
-#[unsafe(export_name = "f2export")]
-pub fn f2() {}
-
-#[repr(u8)]
-pub enum T0 { V1 }
-
-#[non_exhaustive]
-pub enum T1 {}
diff --git a/tests/rustdoc/reexport/reexport-attrs.rs b/tests/rustdoc/reexport/reexport-attrs.rs
deleted file mode 100644
index aec0a11c0c6a..000000000000
--- a/tests/rustdoc/reexport/reexport-attrs.rs
+++ /dev/null
@@ -1,20 +0,0 @@
-//@ aux-build: reexports-attrs.rs
-
-#![crate_name = "foo"]
-
-extern crate reexports_attrs;
-
-//@ has 'foo/fn.f0.html' '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
-pub use reexports_attrs::f0;
-
-//@ has 'foo/fn.f1.html' '//pre[@class="rust item-decl"]' '#[unsafe(link_section = ".here")]'
-pub use reexports_attrs::f1;
-
-//@ has 'foo/fn.f2.html' '//pre[@class="rust item-decl"]' '#[unsafe(export_name = "f2export")]'
-pub use reexports_attrs::f2;
-
-//@ has 'foo/enum.T0.html' '//pre[@class="rust item-decl"]' '#[repr(u8)]'
-pub use reexports_attrs::T0;
-
-//@ has 'foo/enum.T1.html' '//pre[@class="rust item-decl"]' '#[non_exhaustive]'
-pub use reexports_attrs::T1;