about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2023-10-05 11:15:14 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2023-10-05 11:16:14 +1100
commit9e2cd038b03c8194a68e30dd823d145f1f0d4806 (patch)
tree623019d30c96c015f7af0d4ae3056f3c9a5c11f1
parente24f39440478dcddddb83b9999aa724d4a093d8f (diff)
downloadrust-9e2cd038b03c8194a68e30dd823d145f1f0d4806.tar.gz
rust-9e2cd038b03c8194a68e30dd823d145f1f0d4806.zip
Factor out some repeated feature-getting code.
-rw-r--r--compiler/rustc_expand/src/config.rs29
1 files changed, 11 insertions, 18 deletions
diff --git a/compiler/rustc_expand/src/config.rs b/compiler/rustc_expand/src/config.rs
index 9cb16e0b0c9..0a7e424376d 100644
--- a/compiler/rustc_expand/src/config.rs
+++ b/compiler/rustc_expand/src/config.rs
@@ -24,6 +24,7 @@ use rustc_session::Session;
 use rustc_span::edition::{Edition, ALL_EDITIONS};
 use rustc_span::symbol::{sym, Symbol};
 use rustc_span::Span;
+use thin_vec::ThinVec;
 
 /// A folder that strips out items that do not belong in the current configuration.
 pub struct StripUnconfigured<'a> {
@@ -54,6 +55,14 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute]) -> Features {
         })
     }
 
+    fn feature_list(attr: &Attribute) -> ThinVec<ast::NestedMetaItem> {
+        if attr.has_name(sym::feature) && let Some(list) = attr.meta_item_list() {
+            list
+        } else {
+            ThinVec::new()
+        }
+    }
+
     let mut features = Features::default();
     let mut edition_enabled_features = FxHashMap::default();
     let crate_edition = sess.edition();
@@ -81,15 +90,7 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute]) -> Features {
     // - E.g. enable `test_2018_feature` if the crate edition is 2015 but
     //   `rust_2018_preview` is present
     for attr in krate_attrs {
-        if !attr.has_name(sym::feature) {
-            continue;
-        }
-
-        let Some(list) = attr.meta_item_list() else {
-            continue;
-        };
-
-        for mi in list {
+        for mi in feature_list(attr) {
             if !mi.is_word() {
                 continue;
             }
@@ -114,15 +115,7 @@ pub fn features(sess: &Session, krate_attrs: &[Attribute]) -> Features {
 
     // Process all features declared in the code.
     for attr in krate_attrs {
-        if !attr.has_name(sym::feature) {
-            continue;
-        }
-
-        let Some(list) = attr.meta_item_list() else {
-            continue;
-        };
-
-        for mi in list {
+        for mi in feature_list(attr) {
             let name = match mi.ident() {
                 Some(ident) if mi.is_word() => ident.name,
                 Some(ident) => {