From fa812849b89c37e5827948a95a2fa6a28fff2997 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Wed, 27 May 2020 16:50:26 +1200 Subject: [PATCH] Fix warnings and split Scope (#1902) --- crates/nu-cli/src/cli.rs | 13 +++- crates/nu-cli/src/commands/autoview.rs | 2 +- .../nu-cli/src/commands/classified/block.rs | 17 +++-- crates/nu-cli/src/commands/classified/expr.rs | 10 +-- .../src/commands/classified/external.rs | 6 +- .../src/commands/classified/internal.rs | 13 +++- crates/nu-cli/src/commands/command.rs | 2 +- crates/nu-cli/src/commands/each.rs | 7 +- crates/nu-cli/src/commands/format.rs | 3 +- crates/nu-cli/src/commands/keep_until.rs | 2 +- crates/nu-cli/src/commands/keep_while.rs | 2 +- crates/nu-cli/src/commands/merge.rs | 6 +- crates/nu-cli/src/commands/run_alias.rs | 6 +- crates/nu-cli/src/commands/skip_until.rs | 2 +- crates/nu-cli/src/commands/skip_while.rs | 2 +- crates/nu-cli/src/commands/update.rs | 5 +- crates/nu-cli/src/commands/where_.rs | 7 +- crates/nu-cli/src/commands/with_env.rs | 9 ++- crates/nu-cli/src/evaluate/evaluate_args.rs | 6 +- crates/nu-cli/src/evaluate/evaluator.rs | 63 ++++++++--------- crates/nu-cli/src/evaluate/variables.rs | 7 +- crates/nu-cli/src/examples.rs | 18 +++-- crates/nu-cli/src/prelude.rs | 1 + crates/nu-cli/tests/commands/split_row.rs | 2 +- crates/nu-cli/tests/commands/sum.rs | 4 ++ crates/nu-protocol/src/value.rs | 4 ++ crates/nu-protocol/src/value/evaluate.rs | 68 +++---------------- crates/nu_plugin_str/src/nu/tests.rs | 1 + crates/nu_plugin_str/src/strutils.rs | 1 + 29 files changed, 141 insertions(+), 148 deletions(-) diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index d608703152..e72e837cff 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -13,7 +13,7 @@ use futures_codec::FramedRead; use nu_errors::ShellError; use nu_protocol::hir::{ClassifiedCommand, Expression, InternalCommand, Literal, NamedArguments}; -use nu_protocol::{Primitive, ReturnSuccess, Scope, Signature, UntaggedValue, Value}; +use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; use log::{debug, trace}; use rustyline::error::ReadlineError; @@ -870,7 +870,16 @@ async fn process_line( trace!("{:#?}", classified_block); let env = ctx.get_env(); - match run_block(&classified_block.block, ctx, input_stream, &Scope::env(env)).await { + match run_block( + &classified_block.block, + ctx, + input_stream, + &Value::nothing(), + &IndexMap::new(), + &env, + ) + .await + { Ok(input) => { // Running a pipeline gives us back a stream that we can then // work through. At the top level, we just want to pull on the diff --git a/crates/nu-cli/src/commands/autoview.rs b/crates/nu-cli/src/commands/autoview.rs index 61b1ad1d53..4ee86a55f1 100644 --- a/crates/nu-cli/src/commands/autoview.rs +++ b/crates/nu-cli/src/commands/autoview.rs @@ -368,7 +368,7 @@ fn create_default_command_args(context: &RunnableContextWithoutInput) -> RawComm is_last: true, }, name_tag: context.name.clone(), - scope: Scope::empty(), + scope: Scope::new(), }, } } diff --git a/crates/nu-cli/src/commands/classified/block.rs b/crates/nu-cli/src/commands/classified/block.rs index 9eb3b40a0d..1c34b393a8 100644 --- a/crates/nu-cli/src/commands/classified/block.rs +++ b/crates/nu-cli/src/commands/classified/block.rs @@ -6,14 +6,16 @@ use crate::stream::InputStream; use futures::stream::TryStreamExt; use nu_errors::ShellError; use nu_protocol::hir::{Block, ClassifiedCommand, Commands}; -use nu_protocol::{ReturnSuccess, Scope, UntaggedValue, Value}; +use nu_protocol::{ReturnSuccess, UntaggedValue, Value}; use std::sync::atomic::Ordering; pub(crate) async fn run_block( block: &Block, ctx: &mut Context, mut input: InputStream, - scope: &Scope, + it: &Value, + vars: &IndexMap, + env: &IndexMap, ) -> Result { let mut output: Result = Ok(InputStream::empty()); for pipeline in &block.block { @@ -52,7 +54,7 @@ pub(crate) async fn run_block( return Err(e); } } - output = run_pipeline(pipeline, ctx, input, scope).await; + output = run_pipeline(pipeline, ctx, input, it, vars, env).await; input = InputStream::empty(); } @@ -64,10 +66,11 @@ async fn run_pipeline( commands: &Commands, ctx: &mut Context, mut input: InputStream, - scope: &Scope, + it: &Value, + vars: &IndexMap, + env: &IndexMap, ) -> Result { let mut iter = commands.list.clone().into_iter().peekable(); - loop { let item: Option = iter.next(); let next: Option<&ClassifiedCommand> = iter.peek(); @@ -78,13 +81,13 @@ async fn run_pipeline( } (Some(ClassifiedCommand::Expr(expr)), _) => { - run_expression_block(*expr, ctx, input, scope).await? + run_expression_block(*expr, ctx, it, vars, env).await? } (Some(ClassifiedCommand::Error(err)), _) => return Err(err.into()), (_, Some(ClassifiedCommand::Error(err))) => return Err(err.clone().into()), (Some(ClassifiedCommand::Internal(left)), _) => { - run_internal_command(left, ctx, input, scope)? + run_internal_command(left, ctx, input, it, vars, env)? } (None, _) => break, diff --git a/crates/nu-cli/src/commands/classified/expr.rs b/crates/nu-cli/src/commands/classified/expr.rs index 9657719ece..3c8f5b3d78 100644 --- a/crates/nu-cli/src/commands/classified/expr.rs +++ b/crates/nu-cli/src/commands/classified/expr.rs @@ -6,22 +6,22 @@ use log::{log_enabled, trace}; use futures::stream::once; use nu_errors::ShellError; use nu_protocol::hir::SpannedExpression; -use nu_protocol::Scope; +use nu_protocol::Value; pub(crate) async fn run_expression_block( expr: SpannedExpression, context: &mut Context, - _input: InputStream, - scope: &Scope, + it: &Value, + vars: &IndexMap, + env: &IndexMap, ) -> Result { if log_enabled!(log::Level::Trace) { trace!(target: "nu::run::expr", "->"); trace!(target: "nu::run::expr", "{:?}", expr); } - let scope = scope.clone(); let registry = context.registry().clone(); - let output = evaluate_baseline_expr(&expr, ®istry, &scope).await?; + let output = evaluate_baseline_expr(&expr, ®istry, it, vars, env).await?; Ok(once(async { Ok(output) }).to_input_stream()) } diff --git a/crates/nu-cli/src/commands/classified/external.rs b/crates/nu-cli/src/commands/classified/external.rs index 0095bd658c..c0b62418ef 100644 --- a/crates/nu-cli/src/commands/classified/external.rs +++ b/crates/nu-cli/src/commands/classified/external.rs @@ -115,7 +115,9 @@ async fn run_with_stdin( let mut command_args = vec![]; for arg in command.args.iter() { - let value = evaluate_baseline_expr(arg, &context.registry, scope).await?; + let value = + evaluate_baseline_expr(arg, &context.registry, &scope.it, &scope.vars, &scope.env) + .await?; // Skip any arguments that don't really exist, treating them as optional // FIXME: we may want to preserve the gap in the future, though it's hard to say // what value we would put in its place. @@ -509,7 +511,7 @@ mod tests { let mut ctx = Context::basic().expect("There was a problem creating a basic context."); assert!( - run_external_command(cmd, &mut ctx, input, &Scope::empty(), false) + run_external_command(cmd, &mut ctx, input, &Scope::new(), false) .await .is_err() ); diff --git a/crates/nu-cli/src/commands/classified/internal.rs b/crates/nu-cli/src/commands/classified/internal.rs index c64d6822c8..f9b9652682 100644 --- a/crates/nu-cli/src/commands/classified/internal.rs +++ b/crates/nu-cli/src/commands/classified/internal.rs @@ -11,13 +11,20 @@ pub(crate) fn run_internal_command( command: InternalCommand, context: &mut Context, input: InputStream, - scope: &Scope, + it: &Value, + vars: &IndexMap, + env: &IndexMap, ) -> Result { if log_enabled!(log::Level::Trace) { trace!(target: "nu::run::internal", "->"); trace!(target: "nu::run::internal", "{}", command.name); } + let scope = Scope { + it: it.clone(), + vars: vars.clone(), + env: env.clone(), + }; let objects: InputStream = trace_stream!(target: "nu::trace_stream::internal", "input" = input); let internal_command = context.expect_command(&command.name); @@ -26,14 +33,14 @@ pub(crate) fn run_internal_command( internal_command?, Tag::unknown_anchor(command.name_span), command.args.clone(), - scope, + &scope, objects, ) }; let mut result = trace_out_stream!(target: "nu::trace_stream::internal", "output" = result); let mut context = context.clone(); - let scope = scope.clone(); + // let scope = scope.clone(); let stream = async_stream! { let mut soft_errs: Vec = vec![]; diff --git a/crates/nu-cli/src/commands/command.rs b/crates/nu-cli/src/commands/command.rs index 2446af40e4..3af49b2038 100644 --- a/crates/nu-cli/src/commands/command.rs +++ b/crates/nu-cli/src/commands/command.rs @@ -35,7 +35,7 @@ impl UnevaluatedCallInfo { it: &Value, ) -> Result { let mut scope = self.scope.clone(); - scope = scope.set_it(it.clone()); + scope.it = it.clone(); let args = evaluate_args(&self.args, registry, &scope).await?; Ok(CallInfo { diff --git a/crates/nu-cli/src/commands/each.rs b/crates/nu-cli/src/commands/each.rs index 542ec5ad7c..c588803b67 100644 --- a/crates/nu-cli/src/commands/each.rs +++ b/crates/nu-cli/src/commands/each.rs @@ -84,19 +84,20 @@ fn each(raw_args: CommandArgs, registry: &CommandRegistry) -> Result Result { let registry = registry.clone(); + let scope = raw_args.call_info.scope.clone(); let stream = async_stream! { let mut context = Context::from_raw(&raw_args, ®istry); let name_tag = raw_args.call_info.name_tag.clone(); - let scope = raw_args.call_info.scope.clone(); let (merge_args, mut input): (MergeArgs, _) = raw_args.process(®istry).await?; let block = merge_args.block; let table: Option> = match run_block(&block, &mut context, InputStream::empty(), - &scope).await { + &scope.it, + &scope.vars, + &scope.env).await { Ok(mut stream) => Some(stream.drain_vec().await), Err(err) => { yield Err(err); diff --git a/crates/nu-cli/src/commands/run_alias.rs b/crates/nu-cli/src/commands/run_alias.rs index fcebf47e8e..ed7af09d9c 100644 --- a/crates/nu-cli/src/commands/run_alias.rs +++ b/crates/nu-cli/src/commands/run_alias.rs @@ -50,7 +50,7 @@ impl WholeStreamCommand for AliasCommand { let evaluated = call_info.evaluate(®istry).await?; if let Some(positional) = &evaluated.args.positional { for (pos, arg) in positional.iter().enumerate() { - scope = scope.set_var(alias_command.args[pos].to_string(), arg.clone()); + scope.vars.insert(alias_command.args[pos].to_string(), arg.clone()); } } @@ -58,7 +58,9 @@ impl WholeStreamCommand for AliasCommand { &block, &mut context, input, - &scope, + &scope.it, + &scope.vars, + &scope.env, ).await; match result { diff --git a/crates/nu-cli/src/commands/skip_until.rs b/crates/nu-cli/src/commands/skip_until.rs index 299cb087f3..ed139340d8 100644 --- a/crates/nu-cli/src/commands/skip_until.rs +++ b/crates/nu-cli/src/commands/skip_until.rs @@ -90,7 +90,7 @@ impl WholeStreamCommand for SkipUntil { let condition = condition.clone(); trace!("ITEM = {:?}", item); let result = - evaluate_baseline_expr(&*condition, ®istry, &scope.clone().set_it(item.clone())) + evaluate_baseline_expr(&*condition, ®istry, &item, &scope.vars, &scope.env) .await; trace!("RESULT = {:?}", result); diff --git a/crates/nu-cli/src/commands/skip_while.rs b/crates/nu-cli/src/commands/skip_while.rs index bd036fdee9..b31a4c247d 100644 --- a/crates/nu-cli/src/commands/skip_while.rs +++ b/crates/nu-cli/src/commands/skip_while.rs @@ -90,7 +90,7 @@ impl WholeStreamCommand for SkipWhile { let condition = condition.clone(); trace!("ITEM = {:?}", item); let result = - evaluate_baseline_expr(&*condition, ®istry, &scope.clone().set_it(item.clone())) + evaluate_baseline_expr(&*condition, ®istry, &item, &scope.vars, &scope.env) .await; trace!("RESULT = {:?}", result); diff --git a/crates/nu-cli/src/commands/update.rs b/crates/nu-cli/src/commands/update.rs index 57d687ca6a..4734ee6d8d 100644 --- a/crates/nu-cli/src/commands/update.rs +++ b/crates/nu-cli/src/commands/update.rs @@ -62,14 +62,15 @@ fn update(raw_args: CommandArgs, registry: &CommandRegistry) -> Result { let for_block = input.clone(); - let input_clone = input.clone(); let input_stream = once(async { Ok(for_block) }).to_input_stream(); let result = run_block( &block, &mut context, input_stream, - &scope.clone().set_it(input_clone), + &input, + &scope.vars, + &scope.env ).await; match result { diff --git a/crates/nu-cli/src/commands/where_.rs b/crates/nu-cli/src/commands/where_.rs index 23264f1f71..7c731a60d9 100644 --- a/crates/nu-cli/src/commands/where_.rs +++ b/crates/nu-cli/src/commands/where_.rs @@ -67,10 +67,9 @@ fn where_command( registry: &CommandRegistry, ) -> Result { let registry = registry.clone(); + let scope = raw_args.call_info.scope.clone(); + let tag = raw_args.call_info.name_tag.clone(); let stream = async_stream! { - let tag = raw_args.call_info.name_tag.clone(); - let scope = raw_args.call_info.scope.clone(); - let (WhereArgs { block }, mut input) = raw_args.process(®istry).await?; let condition = { if block.block.len() != 1 { @@ -108,7 +107,7 @@ fn where_command( while let Some(input) = input.next().await { //FIXME: should we use the scope that's brought in as well? - let condition = evaluate_baseline_expr(&condition, ®istry, &scope.clone().set_it(input.clone())).await?; + let condition = evaluate_baseline_expr(&condition, ®istry, &input, &scope.vars, &scope.env).await?; match condition.as_bool() { Ok(b) => { diff --git a/crates/nu-cli/src/commands/with_env.rs b/crates/nu-cli/src/commands/with_env.rs index dc11e558be..c44166df79 100644 --- a/crates/nu-cli/src/commands/with_env.rs +++ b/crates/nu-cli/src/commands/with_env.rs @@ -57,18 +57,21 @@ fn with_env(raw_args: CommandArgs, registry: &CommandRegistry) -> Result { named_args.insert( name.clone(), - evaluate_baseline_expr(expr, registry, scope).await?, + evaluate_baseline_expr(expr, registry, &scope.it, &scope.vars, &scope.env) + .await?, ); } diff --git a/crates/nu-cli/src/evaluate/evaluator.rs b/crates/nu-cli/src/evaluate/evaluator.rs index 723226753c..c1896c1d19 100644 --- a/crates/nu-cli/src/evaluate/evaluator.rs +++ b/crates/nu-cli/src/evaluate/evaluator.rs @@ -7,14 +7,16 @@ use log::trace; use nu_errors::{ArgumentError, ShellError}; use nu_protocol::hir::{self, Expression, SpannedExpression}; use nu_protocol::{ - ColumnPath, Primitive, RangeInclusion, Scope, UnspannedPathMember, UntaggedValue, Value, + ColumnPath, Primitive, RangeInclusion, UnspannedPathMember, UntaggedValue, Value, }; #[async_recursion] pub(crate) async fn evaluate_baseline_expr( expr: &SpannedExpression, registry: &CommandRegistry, - scope: &Scope, + it: &Value, + vars: &IndexMap, + env: &IndexMap, ) -> Result { let tag = Tag { span: expr.span, @@ -31,14 +33,14 @@ pub(crate) async fn evaluate_baseline_expr( Expression::Synthetic(hir::Synthetic::String(s)) => { Ok(UntaggedValue::string(s).into_untagged_value()) } - Expression::Variable(var) => evaluate_reference(&var, &scope, tag), - Expression::Command(_) => evaluate_command(tag, &scope), - Expression::Invocation(block) => evaluate_invocation(block, registry, scope).await, - Expression::ExternalCommand(external) => evaluate_external(&external, &scope), + Expression::Variable(var) => evaluate_reference(&var, it, vars, env, tag), + Expression::Command(_) => unimplemented!(), + Expression::Invocation(block) => evaluate_invocation(block, registry, it, vars, env).await, + Expression::ExternalCommand(_) => unimplemented!(), Expression::Binary(binary) => { // TODO: If we want to add short-circuiting, we'll need to move these down - let left = evaluate_baseline_expr(&binary.left, registry, scope).await?; - let right = evaluate_baseline_expr(&binary.right, registry, scope).await?; + let left = evaluate_baseline_expr(&binary.left, registry, it, vars, env).await?; + let right = evaluate_baseline_expr(&binary.right, registry, it, vars, env).await?; trace!("left={:?} right={:?}", left.value, right.value); @@ -59,8 +61,8 @@ pub(crate) async fn evaluate_baseline_expr( let left = &range.left; let right = &range.right; - let left = evaluate_baseline_expr(&left, registry, scope).await?; - let right = evaluate_baseline_expr(&right, registry, scope).await?; + let left = evaluate_baseline_expr(&left, registry, it, vars, env).await?; + let right = evaluate_baseline_expr(&right, registry, it, vars, env).await?; let left_span = left.tag.span; let right_span = right.tag.span; @@ -79,7 +81,7 @@ pub(crate) async fn evaluate_baseline_expr( let mut exprs = vec![]; for expr in list { - let expr = evaluate_baseline_expr(&expr, registry, scope).await?; + let expr = evaluate_baseline_expr(&expr, registry, it, vars, env).await?; exprs.push(expr); } @@ -87,7 +89,7 @@ pub(crate) async fn evaluate_baseline_expr( } Expression::Block(block) => Ok(UntaggedValue::Block(block.clone()).into_value(&tag)), Expression::Path(path) => { - let value = evaluate_baseline_expr(&path.head, registry, scope).await?; + let value = evaluate_baseline_expr(&path.head, registry, it, vars, env).await?; let mut item = value; for member in &path.tail { @@ -151,12 +153,17 @@ fn evaluate_literal(literal: &hir::Literal, span: Span) -> Value { } } -fn evaluate_reference(name: &hir::Variable, scope: &Scope, tag: Tag) -> Result { - trace!("Evaluating {:?} with Scope {:?}", name, scope); +fn evaluate_reference( + name: &hir::Variable, + it: &Value, + vars: &IndexMap, + env: &IndexMap, + tag: Tag, +) -> Result { match name { - hir::Variable::It(_) => Ok(scope.it.value.clone().into_value(tag)), + hir::Variable::It(_) => Ok(it.clone()), hir::Variable::Other(name, _) => match name { - x if x == "$nu" => crate::evaluate::variables::nu(scope, tag), + x if x == "$nu" => crate::evaluate::variables::nu(env, tag), x if x == "$true" => Ok(Value { value: UntaggedValue::boolean(true), tag, @@ -165,8 +172,7 @@ fn evaluate_reference(name: &hir::Variable, scope: &Scope, tag: Tag) -> Result Ok(scope - .vars + x => Ok(vars .get(x) .cloned() .unwrap_or_else(|| UntaggedValue::nothing().into_value(tag))), @@ -174,19 +180,12 @@ fn evaluate_reference(name: &hir::Variable, scope: &Scope, tag: Tag) -> Result Result { - Err(ShellError::syntax_error( - "Unexpected external command".spanned(external.name.span), - )) -} - async fn evaluate_invocation( block: &hir::Block, registry: &CommandRegistry, - scope: &Scope, + it: &Value, + vars: &IndexMap, + env: &IndexMap, ) -> Result { // FIXME: we should use a real context here let mut context = Context::basic()?; @@ -194,7 +193,7 @@ async fn evaluate_invocation( let input = InputStream::empty(); - let result = run_block(&block, &mut context, input, &scope.clone()).await?; + let result = run_block(&block, &mut context, input, it, vars, env).await?; let output = result.into_vec().await; @@ -208,9 +207,3 @@ async fn evaluate_invocation( _ => Ok(UntaggedValue::nothing().into_value(Tag::unknown())), } } - -fn evaluate_command(tag: Tag, _scope: &Scope) -> Result { - Err(ShellError::syntax_error( - "Unexpected command".spanned(tag.span), - )) -} diff --git a/crates/nu-cli/src/evaluate/variables.rs b/crates/nu-cli/src/evaluate/variables.rs index 22b76fefe8..7aae9e21f4 100644 --- a/crates/nu-cli/src/evaluate/variables.rs +++ b/crates/nu-cli/src/evaluate/variables.rs @@ -1,15 +1,16 @@ use crate::cli::History; +use crate::prelude::*; use nu_errors::ShellError; -use nu_protocol::{Scope, TaggedDictBuilder, UntaggedValue, Value}; +use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value}; use nu_source::Tag; -pub fn nu(scope: &Scope, tag: impl Into) -> Result { +pub fn nu(env: &IndexMap, tag: impl Into) -> Result { let tag = tag.into(); let mut nu_dict = TaggedDictBuilder::new(&tag); let mut dict = TaggedDictBuilder::new(&tag); - for v in scope.env.iter() { + for v in env.iter() { if v.0 != "PATH" && v.0 != "Path" { dict.insert_untagged(v.0, UntaggedValue::string(v.1)); } diff --git a/crates/nu-cli/src/examples.rs b/crates/nu-cli/src/examples.rs index d1b590c37e..44017eeec7 100644 --- a/crates/nu-cli/src/examples.rs +++ b/crates/nu-cli/src/examples.rs @@ -1,8 +1,9 @@ use futures::executor::block_on; +use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::hir::ClassifiedBlock; -use nu_protocol::{Scope, ShellTypeName, Value}; +use nu_protocol::{ShellTypeName, Value}; use crate::commands::classified::block::run_block; use crate::commands::{whole_stream_command, Echo}; @@ -58,10 +59,17 @@ async fn evaluate_block( let input_stream = InputStream::empty(); let env = ctx.get_env(); - Ok(run_block(&block.block, ctx, input_stream, &Scope::env(env)) - .await? - .into_vec() - .await) + Ok(run_block( + &block.block, + ctx, + input_stream, + &Value::nothing(), + &IndexMap::new(), + &env, + ) + .await? + .into_vec() + .await) } // TODO probably something already available to do this diff --git a/crates/nu-cli/src/prelude.rs b/crates/nu-cli/src/prelude.rs index 73b358c1b7..31307a2e55 100644 --- a/crates/nu-cli/src/prelude.rs +++ b/crates/nu-cli/src/prelude.rs @@ -102,6 +102,7 @@ pub(crate) use std::future::Future; pub(crate) use std::sync::atomic::AtomicBool; pub(crate) use std::sync::Arc; +pub(crate) use indexmap::IndexMap; pub(crate) use itertools::Itertools; pub trait FromInputStream { diff --git a/crates/nu-cli/tests/commands/split_row.rs b/crates/nu-cli/tests/commands/split_row.rs index 6f9cbc9fa8..19fb22f862 100644 --- a/crates/nu-cli/tests/commands/split_row.rs +++ b/crates/nu-cli/tests/commands/split_row.rs @@ -24,6 +24,6 @@ fn to_row() { "# )); - assert!(actual.out.contains("5")); + assert!(actual.out.contains('5')); }) } diff --git a/crates/nu-cli/tests/commands/sum.rs b/crates/nu-cli/tests/commands/sum.rs index 8aefad85b1..5d5e440cdf 100644 --- a/crates/nu-cli/tests/commands/sum.rs +++ b/crates/nu-cli/tests/commands/sum.rs @@ -63,6 +63,8 @@ fn outputs_zero_with_no_input() { } #[test] +#[allow(clippy::unreadable_literal)] +#[allow(clippy::float_cmp)] fn compute_sum_of_individual_row() -> Result<(), String> { let answers_for_columns = [ ("cpu", 88.257434), @@ -82,6 +84,8 @@ fn compute_sum_of_individual_row() -> Result<(), String> { } #[test] +#[allow(clippy::unreadable_literal)] +#[allow(clippy::float_cmp)] fn compute_sum_of_table() -> Result<(), String> { let answers_for_columns = [ ("cpu", 88.257434), diff --git a/crates/nu-protocol/src/value.rs b/crates/nu-protocol/src/value.rs index 48a8911d93..9852c89414 100644 --- a/crates/nu-protocol/src/value.rs +++ b/crates/nu-protocol/src/value.rs @@ -387,6 +387,10 @@ impl Value { _ => false, } } + + pub fn nothing() -> Value { + UntaggedValue::nothing().into_untagged_value() + } } impl From for Value { diff --git a/crates/nu-protocol/src/value/evaluate.rs b/crates/nu-protocol/src/value/evaluate.rs index 4a141e3f26..e400d19358 100644 --- a/crates/nu-protocol/src/value/evaluate.rs +++ b/crates/nu-protocol/src/value/evaluate.rs @@ -1,4 +1,4 @@ -use crate::value::{Primitive, UntaggedValue, Value}; +use crate::value::Value; use indexmap::IndexMap; use serde::{Deserialize, Serialize}; use std::fmt::Debug; @@ -13,69 +13,19 @@ pub struct Scope { pub env: IndexMap, } -impl Scope { - /// Create a new scope - pub fn new(it: Value) -> Scope { - Scope { - it, - vars: IndexMap::new(), - env: IndexMap::new(), - } - } -} - impl Scope { /// Create an empty scope - pub fn empty() -> Scope { + pub fn new() -> Scope { Scope { - it: UntaggedValue::Primitive(Primitive::Nothing).into_untagged_value(), + it: Value::nothing(), vars: IndexMap::new(), env: IndexMap::new(), } } - - /// Create an empty scope, setting $it to a known Value - pub fn it_value(value: Value) -> Scope { - Scope { - it: value, - vars: IndexMap::new(), - env: IndexMap::new(), - } - } - - pub fn env(env: IndexMap) -> Scope { - Scope { - it: UntaggedValue::Primitive(Primitive::Nothing).into_untagged_value(), - vars: IndexMap::new(), - env, - } - } - - pub fn set_it(self, value: Value) -> Scope { - Scope { - it: value, - vars: self.vars, - env: self.env, - } - } - - pub fn set_var(self, name: String, value: Value) -> Scope { - let mut new_vars = self.vars.clone(); - new_vars.insert(name, value); - Scope { - it: self.it, - vars: new_vars, - env: self.env, - } - } - - pub fn set_env_var(self, variable: String, value: String) -> Scope { - let mut new_env_vars = self.env.clone(); - new_env_vars.insert(variable, value); - Scope { - it: self.it, - vars: self.vars, - env: new_env_vars, - } - } +} + +impl Default for Scope { + fn default() -> Scope { + Scope::new() + } } diff --git a/crates/nu_plugin_str/src/nu/tests.rs b/crates/nu_plugin_str/src/nu/tests.rs index 62ab893575..0e5c13d8a2 100644 --- a/crates/nu_plugin_str/src/nu/tests.rs +++ b/crates/nu_plugin_str/src/nu/tests.rs @@ -267,6 +267,7 @@ mod integration { Ok(()) } #[test] + #[allow(clippy::approx_constant)] fn converts_the_input_to_float_using_the_field_passed_as_parameter() -> Result<(), ShellError> { let run = plugin(&mut Str::new()) .args( diff --git a/crates/nu_plugin_str/src/strutils.rs b/crates/nu_plugin_str/src/strutils.rs index da8f024ab0..6b61876e16 100644 --- a/crates/nu_plugin_str/src/strutils.rs +++ b/crates/nu_plugin_str/src/strutils.rs @@ -294,6 +294,7 @@ pub mod tests { } #[test] + #[allow(clippy::approx_constant)] fn converts_to_float() -> Result<(), Box> { let mut strutils = Str::new(); strutils.for_to_float();