Skip to content

Commit

Permalink
Rollup merge of #121230 - GuillaumeGomez:extend-level-api, r=Nadrieril
Browse files Browse the repository at this point in the history
Extend Level API

I need this API for rust-lang/rust-clippy#12303: I have a nested `cfg` attribute (so a `MetaItem`) and I'd like to still be able to match against all possible kind of `Level`s.
  • Loading branch information
matthiaskrgr authored Feb 19, 2024
2 parents cf0b36a + c17539c commit ec07410
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ impl Level {
}

/// Converts a lower-case string to a level. This will never construct the expect
/// level as that would require a [`LintExpectationId`]
pub fn from_str(x: &str) -> Option<Level> {
/// level as that would require a [`LintExpectationId`].
pub fn from_str(x: &str) -> Option<Self> {
match x {
"allow" => Some(Level::Allow),
"warn" => Some(Level::Warn),
Expand All @@ -238,17 +238,21 @@ impl Level {
}
}

/// Converts a symbol to a level.
pub fn from_attr(attr: &Attribute) -> Option<Level> {
match attr.name_or_empty() {
sym::allow => Some(Level::Allow),
sym::expect => Some(Level::Expect(LintExpectationId::Unstable {
attr_id: attr.id,
lint_index: None,
})),
sym::warn => Some(Level::Warn),
sym::deny => Some(Level::Deny),
sym::forbid => Some(Level::Forbid),
/// Converts an `Attribute` to a level.
pub fn from_attr(attr: &Attribute) -> Option<Self> {
Self::from_symbol(attr.name_or_empty(), Some(attr.id))
}

/// Converts a `Symbol` to a level.
pub fn from_symbol(s: Symbol, id: Option<AttrId>) -> Option<Self> {
match (s, id) {
(sym::allow, _) => Some(Level::Allow),
(sym::expect, Some(attr_id)) => {
Some(Level::Expect(LintExpectationId::Unstable { attr_id, lint_index: None }))
}
(sym::warn, _) => Some(Level::Warn),
(sym::deny, _) => Some(Level::Deny),
(sym::forbid, _) => Some(Level::Forbid),
_ => None,
}
}
Expand Down

0 comments on commit ec07410

Please sign in to comment.