Skip to content

Commit 9e2cd03

Browse files
committed
Factor out some repeated feature-getting code.
1 parent e24f394 commit 9e2cd03

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

compiler/rustc_expand/src/config.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc_session::Session;
2424
use rustc_span::edition::{Edition, ALL_EDITIONS};
2525
use rustc_span::symbol::{sym, Symbol};
2626
use rustc_span::Span;
27+
use thin_vec::ThinVec;
2728

2829
/// A folder that strips out items that do not belong in the current configuration.
2930
pub struct StripUnconfigured<'a> {
@@ -54,6 +55,14 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute]) -> Features {
5455
})
5556
}
5657

58+
fn feature_list(attr: &Attribute) -> ThinVec<ast::NestedMetaItem> {
59+
if attr.has_name(sym::feature) && let Some(list) = attr.meta_item_list() {
60+
list
61+
} else {
62+
ThinVec::new()
63+
}
64+
}
65+
5766
let mut features = Features::default();
5867
let mut edition_enabled_features = FxHashMap::default();
5968
let crate_edition = sess.edition();
@@ -81,15 +90,7 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute]) -> Features {
8190
// - E.g. enable `test_2018_feature` if the crate edition is 2015 but
8291
// `rust_2018_preview` is present
8392
for attr in krate_attrs {
84-
if !attr.has_name(sym::feature) {
85-
continue;
86-
}
87-
88-
let Some(list) = attr.meta_item_list() else {
89-
continue;
90-
};
91-
92-
for mi in list {
93+
for mi in feature_list(attr) {
9394
if !mi.is_word() {
9495
continue;
9596
}
@@ -114,15 +115,7 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute]) -> Features {
114115

115116
// Process all features declared in the code.
116117
for attr in krate_attrs {
117-
if !attr.has_name(sym::feature) {
118-
continue;
119-
}
120-
121-
let Some(list) = attr.meta_item_list() else {
122-
continue;
123-
};
124-
125-
for mi in list {
118+
for mi in feature_list(attr) {
126119
let name = match mi.ident() {
127120
Some(ident) if mi.is_word() => ident.name,
128121
Some(ident) => {

0 commit comments

Comments
 (0)