Rollup merge of #132642 - GuillaumeGomez:attr-docs, r=compiler-errors

Add documentation on `ast::Attribute`

I was working again with attributes in clippy recently and I often find myself in need to read the source code to ensure it's doing what I want.

Instead, a bit of documentation would allow me (and hopefully others) to skip this step.
This commit is contained in:
Matthias Krüger 2024-11-05 20:10:54 +01:00 committed by GitHub
commit 4346249c88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -136,6 +136,13 @@ impl Attribute {
}
}
/// 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<ThinVec<MetaItemInner>> {
match &self.kind {
AttrKind::Normal(normal) => normal.item.meta_item_list(),
@ -143,6 +150,21 @@ impl Attribute {
}
}
/// 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<Symbol> {
match &self.kind {
AttrKind::Normal(normal) => normal.item.value_str(),
@ -232,6 +254,18 @@ impl AttrItem {
}
}
/// Returns the string value in:
///
/// ```text
/// #[attribute = "value"]
/// ^^^^^^^
/// ```
///
/// It returns `None` in any other cases like:
///
/// ```text
/// #[attr("value")]
/// ```
fn value_str(&self) -> Option<Symbol> {
match &self.args {
AttrArgs::Eq(_, args) => args.value_str(),
@ -315,6 +349,18 @@ impl MetaItem {
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<Symbol> {
match &self.kind {
MetaItemKind::NameValue(v) => v.kind.str(),