From 43339058e32e8bb0d218390b9df5b5a68fe57ca7 Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Fri, 22 May 2020 09:23:31 +0200 Subject: add support of feature flag for runnables #4464 Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- crates/ra_cfg/src/cfg_expr.rs | 17 +++++++---------- crates/ra_ide/src/runnables.rs | 13 ++----------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs index a4b201e0e..98f44f56d 100644 --- a/crates/ra_cfg/src/cfg_expr.rs +++ b/crates/ra_cfg/src/cfg_expr.rs @@ -35,14 +35,11 @@ impl CfgExpr { } /// Return minimal features needed - pub fn minimal_features_needed(&self) -> Option> { + pub fn minimal_features_needed(&self) -> Vec { let mut features = vec![]; self.collect_minimal_features_needed(&mut features); - if features.is_empty() { - None - } else { - Some(features) - } + + features } fn collect_minimal_features_needed(&self, features: &mut Vec) { @@ -169,14 +166,14 @@ mod tests { let (subtree, _) = get_token_tree_generated(r#"#![cfg(feature = "baz")]"#); let cfg_expr = parse_cfg(&subtree); - assert_eq!(cfg_expr.minimal_features_needed().unwrap(), vec![SmolStr::new("baz")]); + assert_eq!(cfg_expr.minimal_features_needed(), vec![SmolStr::new("baz")]); let (subtree, _) = get_token_tree_generated(r#"#![cfg(all(feature = "baz", feature = "foo"))]"#); let cfg_expr = parse_cfg(&subtree); assert_eq!( - cfg_expr.minimal_features_needed().unwrap(), + cfg_expr.minimal_features_needed(), vec![SmolStr::new("baz"), SmolStr::new("foo")] ); @@ -184,11 +181,11 @@ mod tests { get_token_tree_generated(r#"#![cfg(any(feature = "baz", feature = "foo", unix))]"#); let cfg_expr = parse_cfg(&subtree); - assert_eq!(cfg_expr.minimal_features_needed().unwrap(), vec![SmolStr::new("baz")]); + assert_eq!(cfg_expr.minimal_features_needed(), vec![SmolStr::new("baz")]); let (subtree, _) = get_token_tree_generated(r#"#![cfg(foo)]"#); let cfg_expr = parse_cfg(&subtree); - assert!(cfg_expr.minimal_features_needed().is_none()); + assert!(cfg_expr.minimal_features_needed().is_empty()); } } diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs index 3a3d0b0ac..a460370c5 100644 --- a/crates/ra_ide/src/runnables.rs +++ b/crates/ra_ide/src/runnables.rs @@ -190,17 +190,8 @@ fn runnable_mod( fn get_features_needed(attrs: Attrs) -> Option> { let cfg_expr = attrs.by_key("cfg").tt_values().map(|subtree| ra_cfg::parse_cfg(subtree)); - let features_needed = cfg_expr.fold(vec![], |mut acc, cfg| { - if let Some(features_needed) = cfg.minimal_features_needed() { - acc.extend(features_needed); - } - acc - }); - if features_needed.is_empty() { - None - } else { - Some(features_needed) - } + let features_needed = cfg_expr.map(|cfg| cfg.minimal_features_needed()).flatten().collect(); + Some(features_needed).filter(|it: &Vec| !it.is_empty()) } #[cfg(test)] -- cgit v1.2.3