Reduced LOC by replacing several instances of Value::Int {}, Value::Float{}, Value::Bool {}, and Value::String {} with Value::int(), Value::float(), Value::boolean() and Value::string() (#7412)

# Description

While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`,
`Value::boolean()` and `Value::string()` constructors, which seem
designed to make it easier to construct various Values, but which aren't
used often at all in the codebase. So, using a few find-replaces
regexes, I increased their usage. This reduces overall LOC because
structures like this:
```
Value::Int {
  val: a,
  span: head
}
```
are changed into
```
Value::int(a, head)
```
and are respected as such by the project's formatter.
There are little readability concerns because the second argument to all
of these is `span`, and it's almost always extremely obvious which is
the span at every callsite.

# User-Facing Changes

None.

# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
Leon
2022-12-10 02:37:51 +10:00
committed by GitHub
parent b56ad92e25
commit 220b105efb
139 changed files with 540 additions and 2141 deletions

View File

@ -187,11 +187,7 @@ fn get_documentation(
engine_state,
stack,
&Call::new(Span::unknown()),
Value::String {
val: example.example.to_string(),
span: Span::unknown(),
}
.into_pipeline_data(),
Value::string(example.example, Span::unknown()).into_pipeline_data(),
) {
Ok(output) => {
let result = output.into_value(Span::unknown());

View File

@ -124,13 +124,7 @@ pub fn eval_call(
callee_stack.add_var(var_id, result);
} else {
callee_stack.add_var(
var_id,
Value::Bool {
val: true,
span: call.head,
},
)
callee_stack.add_var(var_id, Value::boolean(true, call.head))
}
found = true;
}
@ -138,13 +132,7 @@ pub fn eval_call(
if !found {
if named.arg.is_none() {
callee_stack.add_var(
var_id,
Value::Bool {
val: false,
span: call.head,
},
)
callee_stack.add_var(var_id, Value::boolean(false, call.head))
} else if let Some(arg) = &named.default_value {
let result = eval_expression(engine_state, caller_stack, arg)?;
@ -267,18 +255,9 @@ pub fn eval_expression(
expr: &Expression,
) -> Result<Value, ShellError> {
match &expr.expr {
Expr::Bool(b) => Ok(Value::Bool {
val: *b,
span: expr.span,
}),
Expr::Int(i) => Ok(Value::Int {
val: *i,
span: expr.span,
}),
Expr::Float(f) => Ok(Value::Float {
val: *f,
span: expr.span,
}),
Expr::Bool(b) => Ok(Value::boolean(*b, expr.span)),
Expr::Int(i) => Ok(Value::int(*i, expr.span)),
Expr::Float(f) => Ok(Value::float(*f, expr.span)),
Expr::Binary(b) => Ok(Value::Binary {
val: b.clone(),
span: expr.span,
@ -364,10 +343,7 @@ pub fn eval_expression(
Expr::UnaryNot(expr) => {
let lhs = eval_expression(engine_state, stack, expr)?;
match lhs {
Value::Bool { val, .. } => Ok(Value::Bool {
val: !val,
span: expr.span,
}),
Value::Bool { val, .. } => Ok(Value::boolean(!val, expr.span)),
_ => Err(ShellError::TypeMismatch("bool".to_string(), expr.span)),
}
}
@ -381,10 +357,7 @@ pub fn eval_expression(
match boolean {
Boolean::And => {
if lhs.is_false() {
Ok(Value::Bool {
val: false,
span: expr.span,
})
Ok(Value::boolean(false, expr.span))
} else {
let rhs = eval_expression(engine_state, stack, rhs)?;
lhs.and(op_span, &rhs, expr.span)
@ -392,10 +365,7 @@ pub fn eval_expression(
}
Boolean::Or => {
if lhs.is_true() {
Ok(Value::Bool {
val: true,
span: expr.span,
})
Ok(Value::boolean(true, expr.span))
} else {
let rhs = eval_expression(engine_state, stack, rhs)?;
lhs.or(op_span, &rhs, expr.span)
@ -641,35 +611,23 @@ pub fn eval_expression(
let cwd = current_dir_str(engine_state, stack)?;
let path = expand_path_with(s, cwd);
Ok(Value::String {
val: path.to_string_lossy().to_string(),
span: expr.span,
})
Ok(Value::string(path.to_string_lossy(), expr.span))
}
Expr::Directory(s) => {
if s == "-" {
Ok(Value::String {
val: "-".to_string(),
span: expr.span,
})
Ok(Value::string("-", expr.span))
} else {
let cwd = current_dir_str(engine_state, stack)?;
let path = expand_path_with(s, cwd);
Ok(Value::String {
val: path.to_string_lossy().to_string(),
span: expr.span,
})
Ok(Value::string(path.to_string_lossy(), expr.span))
}
}
Expr::GlobPattern(s) => {
let cwd = current_dir_str(engine_state, stack)?;
let path = expand_path_with(s, cwd);
Ok(Value::String {
val: path.to_string_lossy().to_string(),
span: expr.span,
})
Ok(Value::string(path.to_string_lossy(), expr.span))
}
Expr::Signature(_) => Ok(Value::Nothing { span: expr.span }),
Expr::Garbage => Ok(Value::Nothing { span: expr.span }),