Skip to content

Commit f439907

Browse files
committed
Streamline Level::from_{attr,symbol}
These methods return `Option<(Self, Option<LintExpectationId>)>`. But all the call sites except one don't look at the `Option<LintExpectationId>`. This commit simplifies these methods to not return the `Option<LintExpectationId>`. This means they no longer need to be passed a closure to compute an `AttrId` (which is usually discarded anyway). The commit also renames `from_attr` as `from_opt_symbol`, because it takes an `Option<Symbol>`, not an `Attribute`. These changes simplify all the call sites that don't need the `Option<LintExpectationId>`, and also the one call site that does (in `LintLevelsBuilder::add`): that call site no longer needs to do an awkward destructuring, and can instead build the appropriate `LintExpectationId` directly.
1 parent f9fd7f7 commit f439907

9 files changed

Lines changed: 32 additions & 50 deletions

File tree

compiler/rustc_lint/src/levels.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_hir as hir;
99
use rustc_hir::HirId;
1010
use rustc_hir::intravisit::{self, Visitor};
1111
use rustc_index::IndexVec;
12-
use rustc_middle::bug;
1312
use rustc_middle::hir::nested_filter;
1413
use rustc_middle::lint::{
1514
LevelSpec, LintExpectation, LintLevelSource, ShallowLintLevelMap, emit_lint_base,
@@ -659,25 +658,23 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
659658
continue;
660659
}
661660

662-
let (level, lint_id) = match Level::from_attr(attr.name(), || attr.id()) {
661+
let (level, lint_id) = match Level::from_opt_symbol(attr.name()) {
663662
None => continue,
664-
// This is the only lint level with a `LintExpectationId` that can be created from
665-
// an attribute.
666-
Some((Level::Expect, Some(unstable_id))) if let Some(hir_id) = source_hir_id => {
667-
let LintExpectationId::Unstable { lint_index: None, attr_id: _ } = unstable_id
668-
else {
669-
bug!("stable id Level::from_attr")
670-
};
671-
672-
let stable_id = LintExpectationId::Stable {
673-
hir_id,
674-
attr_index: attr_index.try_into().unwrap(),
675-
lint_index: None,
663+
// `Expect` is the only lint level with a `LintExpectationId` that can be created
664+
// from an attribute.
665+
Some(Level::Expect) => {
666+
let id = if let Some(hir_id) = source_hir_id {
667+
LintExpectationId::Stable {
668+
hir_id,
669+
attr_index: attr_index.try_into().unwrap(),
670+
lint_index: None,
671+
}
672+
} else {
673+
LintExpectationId::Unstable { attr_id: attr.id(), lint_index: None }
676674
};
677-
678-
(Level::Expect, Some(stable_id))
675+
(Level::Expect, Some(id))
679676
}
680-
Some((level, id)) => (level, id),
677+
Some(level) => (level, None),
681678
};
682679

683680
let Some(mut metas) = attr.meta_item_list() else { continue };

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -215,34 +215,19 @@ impl Level {
215215
}
216216
}
217217

218-
/// Converts an `Attribute` to a level.
219-
pub fn from_attr(
220-
attr_name: Option<Symbol>,
221-
attr_id: impl Fn() -> AttrId,
222-
) -> Option<(Self, Option<LintExpectationId>)> {
223-
attr_name.and_then(|name| Self::from_symbol(name, || Some(attr_id())))
218+
/// Converts an `Option<Symbol>` to a level.
219+
pub fn from_opt_symbol(s: Option<Symbol>) -> Option<Self> {
220+
s.and_then(Self::from_symbol)
224221
}
225222

226223
/// Converts a `Symbol` to a level.
227-
pub fn from_symbol(
228-
s: Symbol,
229-
id: impl FnOnce() -> Option<AttrId>,
230-
) -> Option<(Self, Option<LintExpectationId>)> {
224+
pub fn from_symbol(s: Symbol) -> Option<Self> {
231225
match s {
232-
sym::allow => Some((Level::Allow, None)),
233-
sym::expect => {
234-
if let Some(attr_id) = id() {
235-
Some((
236-
Level::Expect,
237-
Some(LintExpectationId::Unstable { attr_id, lint_index: None }),
238-
))
239-
} else {
240-
None
241-
}
242-
}
243-
sym::warn => Some((Level::Warn, None)),
244-
sym::deny => Some((Level::Deny, None)),
245-
sym::forbid => Some((Level::Forbid, None)),
226+
sym::allow => Some(Level::Allow),
227+
sym::expect => Some(Level::Expect),
228+
sym::warn => Some(Level::Warn),
229+
sym::deny => Some(Level::Deny),
230+
sym::forbid => Some(Level::Forbid),
246231
_ => None,
247232
}
248233
}

compiler/rustc_mir_build/src/builder/scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
13021302
.tcx
13031303
.hir_attrs(id)
13041304
.iter()
1305-
.any(|attr| Level::from_attr(attr.name(), || attr.id()).is_some())
1305+
.any(|attr| Level::from_opt_symbol(attr.name()).is_some())
13061306
{
13071307
// This is a rare case. It's for a node path that doesn't reach the root due to an
13081308
// intervening lint level attribute. This result doesn't get cached.

src/tools/clippy/clippy_lints/src/attrs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ impl EarlyLintPass for PostExpansionEarlyAttributes {
583583
if matches!(name, sym::allow | sym::expect) && self.msrv.meets(msrvs::LINT_REASONS_STABILIZATION) {
584584
allow_attributes_without_reason::check(cx, name, items, attr);
585585
}
586-
if is_lint_level(name, attr.id) {
586+
if is_lint_level(name) {
587587
blanket_clippy_restriction_lints::check(cx, name, items);
588588
}
589589
if items.is_empty() || !attr.has_name(sym::deprecated) {

src/tools/clippy/clippy_lints/src/attrs/unnecessary_clippy_cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(super) fn check(
1515
) {
1616
if cfg_attr.has_name(sym::clippy)
1717
&& let Some(ident) = behind_cfg_attr.ident()
18-
&& Level::from_symbol(ident.name, || Some(attr.id)).is_some()
18+
&& Level::from_symbol(ident.name).is_some()
1919
&& let Some(items) = behind_cfg_attr.meta_item_list()
2020
{
2121
let nb_items = items.len();

src/tools/clippy/clippy_lints/src/attrs/useless_attribute.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, item: &Item, attrs: &[Attribute]) {
1515
return;
1616
}
1717
if let Some(lint_list) = &attr.meta_item_list()
18-
&& attr.name().is_some_and(|name| is_lint_level(name, attr.id))
18+
&& attr.name().is_some_and(|name| is_lint_level(name))
1919
{
2020
for lint in lint_list {
2121
match item.kind {

src/tools/clippy/clippy_lints/src/attrs/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::macros::{is_panic, macro_backtrace};
2-
use rustc_ast::{AttrId, MetaItemInner};
2+
use rustc_ast::MetaItemInner;
33
use rustc_hir::{
44
Block, Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, StmtKind, TraitFn, TraitItem, TraitItemKind,
55
};
@@ -16,8 +16,8 @@ pub(super) fn is_word(nmi: &MetaItemInner, expected: Symbol) -> bool {
1616
}
1717
}
1818

19-
pub(super) fn is_lint_level(symbol: Symbol, attr_id: AttrId) -> bool {
20-
Level::from_symbol(symbol, || Some(attr_id)).is_some()
19+
pub(super) fn is_lint_level(symbol: Symbol) -> bool {
20+
Level::from_symbol(symbol).is_some()
2121
}
2222

2323
pub(super) fn is_relevant_item(cx: &LateContext<'_>, item: &Item<'_>) -> bool {

src/tools/clippy/clippy_lints/src/collapsible_if.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl CollapsibleIf {
238238
},
239239

240240
[attr]
241-
if matches!(Level::from_attr(attr.name(), || attr.id()), Some((Level::Expect, _)))
241+
if matches!(Level::from_opt_symbol(attr.name()), Some(Level::Expect))
242242
&& let Some(metas) = attr.meta_item_list()
243243
&& let Some(MetaItemInner::MetaItem(meta_item)) = metas.first()
244244
&& let [tool, lint_name] = meta_item.path.segments.as_slice()

src/tools/clippy/clippy_lints/src/returns/needless_return.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ fn check_final_expr<'tcx>(
181181
match cx.tcx.hir_attrs(expr.hir_id) {
182182
[] => {},
183183
[attr] => {
184-
if matches!(Level::from_attr(attr.name(), || attr.id()), Some((Level::Expect, _)))
184+
if matches!(Level::from_opt_symbol(attr.name()), Some(Level::Expect))
185185
&& let metas = attr.meta_item_list()
186186
&& let Some(lst) = metas
187187
&& let [MetaItemInner::MetaItem(meta_item), ..] = lst.as_slice()

0 commit comments

Comments
 (0)