From c17539c0684db0011f96ed652e58481de3e65c63 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 17 Feb 2024 12:42:21 +0100 Subject: [PATCH] Extend Level API --- compiler/rustc_lint_defs/src/lib.rs | 30 ++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 09b1f03f151..d4e5c78c492 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -227,8 +227,8 @@ pub fn as_str(self) -> &'static str { } /// 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 as that would require a [`LintExpectationId`]. + pub fn from_str(x: &str) -> Option { match x { "allow" => Some(Level::Allow), "warn" => Some(Level::Warn), @@ -238,17 +238,21 @@ pub fn from_str(x: &str) -> Option { } } - /// Converts a symbol to a level. - pub fn from_attr(attr: &Attribute) -> Option { - 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::from_symbol(attr.name_or_empty(), Some(attr.id)) + } + + /// Converts a `Symbol` to a level. + pub fn from_symbol(s: Symbol, id: Option) -> Option { + 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, } }