about summary refs log tree commit diff
path: root/src/librustdoc
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2025-04-10 14:33:59 +1000
committerNicholas Nethercote <n.nethercote@gmail.com>2025-04-17 09:50:52 +1000
commit2fef0a30ae6b2687dfb286cb544d2a542f7e2335 (patch)
tree9c17b4e3cf1885d0181a26d0bc24c3c6fb0e114d /src/librustdoc
parent7e1f2f9c54338cbefddffcce725f89d271d2cbe8 (diff)
downloadrust-2fef0a30ae6b2687dfb286cb544d2a542f7e2335.tar.gz
rust-2fef0a30ae6b2687dfb286cb544d2a542f7e2335.zip
Replace infallible `name_or_empty` methods with fallible `name` methods.
I'm removing empty identifiers everywhere, because in practice they
always mean "no identifier" rather than "empty identifier". (An empty
identifier is impossible.) It's better to use `Option` to mean "no
identifier" because you then can't forget about the "no identifier"
possibility.

Some specifics:
- When testing an attribute for a single name, the commit uses the
  `has_name` method.
- When testing an attribute for multiple names, the commit uses the new
  `has_any_name` method.
- When using `match` on an attribute, the match arms now have `Some` on
  them.

In the tests, we now avoid printing empty identifiers by not printing
the identifier in the `error:` line at all, instead letting the carets
point out the problem.
Diffstat (limited to 'src/librustdoc')
-rw-r--r--src/librustdoc/clean/types.rs2
-rw-r--r--src/librustdoc/core.rs4
-rw-r--r--src/librustdoc/doctest/make.rs14
-rw-r--r--src/librustdoc/html/render/context.rs12
4 files changed, 13 insertions, 19 deletions
diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs
index 31aa84535cc..b084eded403 100644
--- a/src/librustdoc/clean/types.rs
+++ b/src/librustdoc/clean/types.rs
@@ -788,7 +788,7 @@ impl Item {
                         }
                         _ => Some(rustc_hir_pretty::attribute_to_string(&tcx, attr)),
                     }
-                } else if ALLOWED_ATTRIBUTES.contains(&attr.name_or_empty()) {
+                } else if attr.has_any_name(ALLOWED_ATTRIBUTES) {
                     Some(
                         rustc_hir_pretty::attribute_to_string(&tcx, attr)
                             .replace("\\\n", "")
diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs
index c4dea79370d..f7695ac8635 100644
--- a/src/librustdoc/core.rs
+++ b/src/librustdoc/core.rs
@@ -412,9 +412,7 @@ pub(crate) fn run_global_ctxt(
     // Process all of the crate attributes, extracting plugin metadata along
     // with the passes which we are supposed to run.
     for attr in krate.module.attrs.lists(sym::doc) {
-        let name = attr.name_or_empty();
-
-        if attr.is_word() && name == sym::document_private_items {
+        if attr.is_word() && attr.has_name(sym::document_private_items) {
             ctxt.render_options.document_private = true;
         }
     }
diff --git a/src/librustdoc/doctest/make.rs b/src/librustdoc/doctest/make.rs
index 4edd5433de6..d5c965f7053 100644
--- a/src/librustdoc/doctest/make.rs
+++ b/src/librustdoc/doctest/make.rs
@@ -345,7 +345,7 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
     fn check_item(item: &ast::Item, info: &mut ParseSourceInfo, crate_name: &Option<&str>) -> bool {
         let mut is_extern_crate = false;
         if !info.has_global_allocator
-            && item.attrs.iter().any(|attr| attr.name_or_empty() == sym::global_allocator)
+            && item.attrs.iter().any(|attr| attr.has_name(sym::global_allocator))
         {
             info.has_global_allocator = true;
         }
@@ -377,7 +377,7 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
     }
 
     let mut prev_span_hi = 0;
-    let not_crate_attrs = [sym::forbid, sym::allow, sym::warn, sym::deny, sym::expect];
+    let not_crate_attrs = &[sym::forbid, sym::allow, sym::warn, sym::deny, sym::expect];
     let parsed = parser.parse_item(rustc_parse::parser::ForceCollect::No);
 
     let result = match parsed {
@@ -386,17 +386,13 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
                 && let Some(ref body) = fn_item.body =>
         {
             for attr in &item.attrs {
-                let attr_name = attr.name_or_empty();
-
-                if attr.style == AttrStyle::Outer || not_crate_attrs.contains(&attr_name) {
+                if attr.style == AttrStyle::Outer || attr.has_any_name(not_crate_attrs) {
                     // There is one exception to these attributes:
                     // `#![allow(internal_features)]`. If this attribute is used, we need to
                     // consider it only as a crate-level attribute.
-                    if attr_name == sym::allow
+                    if attr.has_name(sym::allow)
                         && let Some(list) = attr.meta_item_list()
-                        && list.iter().any(|sub_attr| {
-                            sub_attr.name_or_empty().as_str() == "internal_features"
-                        })
+                        && list.iter().any(|sub_attr| sub_attr.has_name(sym::internal_features))
                     {
                         push_to_s(&mut info.crate_attrs, source, attr.span, &mut prev_span_hi);
                     } else {
diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs
index 596ac665fc3..f22935df96c 100644
--- a/src/librustdoc/html/render/context.rs
+++ b/src/librustdoc/html/render/context.rs
@@ -521,23 +521,23 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
         // Crawl the crate attributes looking for attributes which control how we're
         // going to emit HTML
         for attr in krate.module.attrs.lists(sym::doc) {
-            match (attr.name_or_empty(), attr.value_str()) {
-                (sym::html_favicon_url, Some(s)) => {
+            match (attr.name(), attr.value_str()) {
+                (Some(sym::html_favicon_url), Some(s)) => {
                     layout.favicon = s.to_string();
                 }
-                (sym::html_logo_url, Some(s)) => {
+                (Some(sym::html_logo_url), Some(s)) => {
                     layout.logo = s.to_string();
                 }
-                (sym::html_playground_url, Some(s)) => {
+                (Some(sym::html_playground_url), Some(s)) => {
                     playground = Some(markdown::Playground {
                         crate_name: Some(krate.name(tcx)),
                         url: s.to_string(),
                     });
                 }
-                (sym::issue_tracker_base_url, Some(s)) => {
+                (Some(sym::issue_tracker_base_url), Some(s)) => {
                     issue_tracker_base_url = Some(s.to_string());
                 }
-                (sym::html_no_source, None) if attr.is_word() => {
+                (Some(sym::html_no_source), None) if attr.is_word() => {
                     include_sources = false;
                 }
                 _ => {}