From 33009601afefcd79cd500b12ccc1b3ab5eda11f1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 5 Nov 2024 16:15:45 +0100 Subject: [PATCH] Add documentation on `ast::Attribute` --- compiler/rustc_ast/src/attr/mod.rs | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index 9311af28f54..54e826585d2 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -136,6 +136,13 @@ pub fn is_word(&self) -> bool { } } + /// Returns a list of meta items if the attribute is delimited with parenthesis: + /// + /// ```text + /// #[attr(a, b = "c")] // Returns `Some()`. + /// #[attr = ""] // Returns `None`. + /// #[attr] // Returns `None`. + /// ``` pub fn meta_item_list(&self) -> Option> { match &self.kind { AttrKind::Normal(normal) => normal.item.meta_item_list(), @@ -143,6 +150,21 @@ pub fn meta_item_list(&self) -> Option> { } } + /// Returns the string value in: + /// + /// ```text + /// #[attribute = "value"] + /// ^^^^^^^ + /// ``` + /// + /// It returns `None` in any other cases, including doc comments if they + /// are not under the form `#[doc = "..."]`. + /// + /// It also returns `None` for: + /// + /// ```text + /// #[attr("value")] + /// ``` pub fn value_str(&self) -> Option { match &self.kind { AttrKind::Normal(normal) => normal.item.value_str(), @@ -232,6 +254,18 @@ pub fn meta_item_list(&self) -> Option> { } } + /// Returns the string value in: + /// + /// ```text + /// #[attribute = "value"] + /// ^^^^^^^ + /// ``` + /// + /// It returns `None` in any other cases like: + /// + /// ```text + /// #[attr("value")] + /// ``` fn value_str(&self) -> Option { match &self.args { AttrArgs::Eq(_, args) => args.value_str(), @@ -315,6 +349,18 @@ pub fn name_value_literal_span(&self) -> Option { Some(self.name_value_literal()?.span) } + /// Returns the string value in: + /// + /// ```text + /// #[attribute = "value"] + /// ^^^^^^^ + /// ``` + /// + /// It returns `None` in any other cases like: + /// + /// ```text + /// #[attr("value")] + /// ``` pub fn value_str(&self) -> Option { match &self.kind { MetaItemKind::NameValue(v) => v.kind.str(),