Skip to content

Commit

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

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
GuillaumeGomez committed Feb 18, 2024
2 parents ffce640 + c17539c commit 900491d
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 900491d

Please sign in to comment.