fix clippy. check pr: PASS

This commit is contained in:
Devyn Cairns 2024-07-08 21:10:41 -07:00
parent 0a399011e3
commit 1ce2096b72
12 changed files with 59 additions and 72 deletions

View File

@ -67,7 +67,7 @@ impl Command for OverlayUse {
let maybe_origin_module_id = let maybe_origin_module_id =
if let Some(overlay_expr) = call.get_parser_info(caller_stack, "overlay_expr") { if let Some(overlay_expr) = call.get_parser_info(caller_stack, "overlay_expr") {
if let Expr::Overlay(module_id) = &overlay_expr.expr { if let Expr::Overlay(module_id) = &overlay_expr.expr {
module_id.clone() *module_id
} else { } else {
return Err(ShellError::NushellFailedSpanned { return Err(ShellError::NushellFailedSpanned {
msg: "Not an overlay".to_string(), msg: "Not an overlay".to_string(),

View File

@ -45,14 +45,12 @@ impl Command for ViewIr {
// This helps display the actual compilation error // This helps display the actual compilation error
let ir_block = match &block.ir_block { let ir_block = match &block.ir_block {
Some(ir_block) => Cow::Borrowed(ir_block), Some(ir_block) => Cow::Borrowed(ir_block),
None => Cow::Owned( None => Cow::Owned(compile(&StateWorkingSet::new(engine_state), block).map_err(
compile(&StateWorkingSet::new(engine_state), &block).map_err(|err| { |err| ShellError::IrCompileError {
ShellError::IrCompileError { span: block.span,
span: block.span, errors: vec![err],
errors: vec![err], },
} )?),
})?,
),
}; };
let formatted = if json { let formatted = if json {

View File

@ -420,7 +420,7 @@ impl BlockBuilder {
self.file_count = self self.file_count = self
.file_count .file_count
.checked_add(1) .checked_add(1)
.ok_or_else(|| CompileError::FileOverflow)?; .ok_or(CompileError::FileOverflow)?;
Ok(next) Ok(next)
} }

View File

@ -230,7 +230,7 @@ pub(crate) fn compile_external_call(
// Pass everything to run-external // Pass everything to run-external
let run_external_id = working_set let run_external_id = working_set
.find_decl(b"run-external") .find_decl(b"run-external")
.ok_or_else(|| CompileError::RunExternalNotFound { span: head.span })?; .ok_or(CompileError::RunExternalNotFound { span: head.span })?;
let mut call = Call::new(head.span); let mut call = Call::new(head.span);
call.decl_id = run_external_id; call.decl_id = run_external_id;

View File

@ -133,7 +133,7 @@ pub(crate) fn compile_expression(
Expr::Call(call) => { Expr::Call(call) => {
move_in_reg_to_out_reg(builder)?; move_in_reg_to_out_reg(builder)?;
compile_call(working_set, builder, &call, redirect_modes, out_reg) compile_call(working_set, builder, call, redirect_modes, out_reg)
} }
Expr::ExternalCall(head, args) => { Expr::ExternalCall(head, args) => {
move_in_reg_to_out_reg(builder)?; move_in_reg_to_out_reg(builder)?;
@ -161,9 +161,9 @@ pub(crate) fn compile_expression(
compile_binary_op( compile_binary_op(
working_set, working_set,
builder, builder,
&lhs, lhs,
operator.clone().into_spanned(op.span), operator.clone().into_spanned(op.span),
&rhs, rhs,
out_reg, out_reg,
) )
} else { } else {
@ -172,14 +172,7 @@ pub(crate) fn compile_expression(
} }
Expr::Subexpression(block_id) => { Expr::Subexpression(block_id) => {
let block = working_set.get_block(*block_id); let block = working_set.get_block(*block_id);
compile_block( compile_block(working_set, builder, block, redirect_modes, in_reg, out_reg)
working_set,
builder,
&block,
redirect_modes,
in_reg,
out_reg,
)
} }
Expr::Block(block_id) => lit(builder, Literal::Block(*block_id)), Expr::Block(block_id) => lit(builder, Literal::Block(*block_id)),
Expr::Closure(block_id) => lit(builder, Literal::Closure(*block_id)), Expr::Closure(block_id) => lit(builder, Literal::Closure(*block_id)),

View File

@ -31,7 +31,7 @@ impl RedirectModes {
pub(crate) fn with_capture_out(&self, span: Span) -> Self { pub(crate) fn with_capture_out(&self, span: Span) -> Self {
RedirectModes { RedirectModes {
out: Some(RedirectMode::Capture.into_spanned(span)), out: Some(RedirectMode::Capture.into_spanned(span)),
err: self.err.clone(), err: self.err,
} }
} }
} }
@ -76,14 +76,14 @@ pub(crate) fn redirect_modes_of_expression(
expression: &Expression, expression: &Expression,
redir_span: Span, redir_span: Span,
) -> Result<RedirectModes, CompileError> { ) -> Result<RedirectModes, CompileError> {
let (out, err) = expression.expr.pipe_redirection(&working_set); let (out, err) = expression.expr.pipe_redirection(working_set);
Ok(RedirectModes { Ok(RedirectModes {
out: out out: out
.map(|out| out_dest_to_redirect_mode(out)) .map(out_dest_to_redirect_mode)
.transpose()? .transpose()?
.map(|mode| mode.into_spanned(redir_span)), .map(|mode| mode.into_spanned(redir_span)),
err: err err: err
.map(|err| out_dest_to_redirect_mode(err)) .map(out_dest_to_redirect_mode)
.transpose()? .transpose()?
.map(|mode| mode.into_spanned(redir_span)), .map(|mode| mode.into_spanned(redir_span)),
}) })
@ -95,32 +95,30 @@ pub(crate) fn finish_redirection(
modes: RedirectModes, modes: RedirectModes,
out_reg: RegId, out_reg: RegId,
) -> Result<(), CompileError> { ) -> Result<(), CompileError> {
match modes.out { if let Some(Spanned {
Some(Spanned { item: RedirectMode::File { file_num },
item: RedirectMode::File { file_num }, span,
span, }) = modes.out
}) => { {
// If out is a file and err is a pipe, we must not consume the expression result - // If out is a file and err is a pipe, we must not consume the expression result -
// that is actually the err, in that case. // that is actually the err, in that case.
if !matches!( if !matches!(
modes.err, modes.err,
Some(Spanned { Some(Spanned {
item: RedirectMode::Pipe { .. }, item: RedirectMode::Pipe { .. },
.. ..
}) })
) { ) {
builder.push( builder.push(
Instruction::WriteFile { Instruction::WriteFile {
file_num, file_num,
src: out_reg, src: out_reg,
} }
.into_spanned(span), .into_spanned(span),
)?; )?;
builder.load_empty(out_reg)?; builder.load_empty(out_reg)?;
}
builder.push(Instruction::CloseFile { file_num }.into_spanned(span))?;
} }
_ => (), builder.push(Instruction::CloseFile { file_num }.into_spanned(span))?;
} }
match modes.err { match modes.err {

View File

@ -811,7 +811,7 @@ fn literal_value(
} }
Literal::String(s) => Value::string(ctx.get_str(*s, span)?, span), Literal::String(s) => Value::string(ctx.get_str(*s, span)?, span),
Literal::RawString(s) => Value::string(ctx.get_str(*s, span)?, span), Literal::RawString(s) => Value::string(ctx.get_str(*s, span)?, span),
Literal::CellPath(path) => Value::cell_path(CellPath::clone(&path), span), Literal::CellPath(path) => Value::cell_path(CellPath::clone(path), span),
Literal::Date(dt) => Value::date(**dt, span), Literal::Date(dt) => Value::date(**dt, span),
Literal::Nothing => Value::nothing(span), Literal::Nothing => Value::nothing(span),
}) })
@ -928,7 +928,7 @@ fn eval_call<D: DebugContext>(
// Move environment variables back into the caller stack scope if requested to do so // Move environment variables back into the caller stack scope if requested to do so
if block.redirect_env { if block.redirect_env {
redirect_env(engine_state, &mut caller_stack, &mut callee_stack); redirect_env(engine_state, &mut caller_stack, &callee_stack);
} }
} else { } else {
// FIXME: precalculate this and save it somewhere // FIXME: precalculate this and save it somewhere
@ -937,7 +937,7 @@ fn eval_call<D: DebugContext>(
caller_stack caller_stack
.arguments .arguments
.get_args(*args_base, args_len) .get_args(*args_base, args_len)
.into_iter() .iter()
.flat_map(|arg| arg.span()), .flat_map(|arg| arg.span()),
), ),
); );
@ -1134,8 +1134,7 @@ fn get_env_var_case_insensitive<'a>(ctx: &'a mut EvalContext<'_>, key: &str) ->
.active_overlays .active_overlays
.iter() .iter()
.rev() .rev()
.map(|name| overlays.get(name)) .filter_map(|name| overlays.get(name))
.flatten()
}) })
.find_map(|map| { .find_map(|map| {
// Use the hashmap first to try to be faster? // Use the hashmap first to try to be faster?
@ -1163,18 +1162,17 @@ fn get_env_var_name_case_insensitive<'a>(ctx: &mut EvalContext<'_>, key: &'a str
.active_overlays .active_overlays
.iter() .iter()
.rev() .rev()
.map(|name| overlays.get(name)) .filter_map(|name| overlays.get(name))
.flatten()
}) })
.find_map(|map| { .find_map(|map| {
// Use the hashmap first to try to be faster? // Use the hashmap first to try to be faster?
if map.contains_key(key) { if map.contains_key(key) {
Some(Cow::Borrowed(key)) Some(Cow::Borrowed(key))
} else if let Some(k) = map.keys().find(|k| k.eq_ignore_case(key)) {
// it exists, but with a different case
Some(Cow::Owned(k.to_owned()))
} else { } else {
None map.keys().find(|k| k.eq_ignore_case(key)).map(|k| {
// it exists, but with a different case
Cow::Owned(k.to_owned())
})
} }
}) })
// didn't exist. // didn't exist.
@ -1271,7 +1269,7 @@ fn eval_iterate(
let mut data = ctx.take_reg(stream); let mut data = ctx.take_reg(stream);
if let PipelineData::ListStream(list_stream, _) = &mut data { if let PipelineData::ListStream(list_stream, _) = &mut data {
// Modify the stream, taking one value off, and branching if it's empty // Modify the stream, taking one value off, and branching if it's empty
if let Some(val) = list_stream.next() { if let Some(val) = list_stream.next_value() {
ctx.put_reg(dst, val.into_pipeline_data()); ctx.put_reg(dst, val.into_pipeline_data());
ctx.put_reg(stream, data); // put the stream back so it can be iterated on again ctx.put_reg(stream, data); // put the stream back so it can be iterated on again
Ok(InstructionResult::Continue) Ok(InstructionResult::Continue)
@ -1294,6 +1292,7 @@ fn eval_iterate(
/// Redirect environment from the callee stack to the caller stack /// Redirect environment from the callee stack to the caller stack
fn redirect_env(engine_state: &EngineState, caller_stack: &mut Stack, callee_stack: &Stack) { fn redirect_env(engine_state: &EngineState, caller_stack: &mut Stack, callee_stack: &Stack) {
// TODO: make this more efficient
// Grab all environment variables from the callee // Grab all environment variables from the callee
let caller_env_vars = caller_stack.get_env_var_names(engine_state); let caller_env_vars = caller_stack.get_env_var_names(engine_state);

View File

@ -62,15 +62,15 @@ impl Command for KnownExternal {
command.run(engine_state, stack, &(&extern_call).into(), input) command.run(engine_state, stack, &(&extern_call).into(), input)
} }
CallImpl::AstBox(call) => { CallImpl::AstBox(call) => {
let extern_call = ast_call_to_extern_call(engine_state, &call, &extern_name)?; let extern_call = ast_call_to_extern_call(engine_state, call, &extern_name)?;
command.run(engine_state, stack, &(&extern_call).into(), input) command.run(engine_state, stack, &(&extern_call).into(), input)
} }
CallImpl::IrRef(call) => { CallImpl::IrRef(call) => {
let extern_call = ir_call_to_extern_call(stack, &call, &extern_name)?; let extern_call = ir_call_to_extern_call(stack, call, &extern_name)?;
command.run(engine_state, stack, &(&extern_call).into(), input) command.run(engine_state, stack, &(&extern_call).into(), input)
} }
CallImpl::IrBox(call) => { CallImpl::IrBox(call) => {
let extern_call = ir_call_to_extern_call(stack, &call, &extern_name)?; let extern_call = ir_call_to_extern_call(stack, call, &extern_name)?;
command.run(engine_state, stack, &(&extern_call).into(), input) command.run(engine_state, stack, &(&extern_call).into(), input)
} }
} }
@ -100,7 +100,7 @@ fn ast_call_to_extern_call(
extern_call.add_positional(arg_extern_name); extern_call.add_positional(arg_extern_name);
for subcommand in extern_name.into_iter().skip(1) { for subcommand in extern_name.iter().skip(1) {
extern_call.add_positional(Expression::new_existing( extern_call.add_positional(Expression::new_existing(
Expr::String(subcommand.to_string()), Expr::String(subcommand.to_string()),
call.head, call.head,

View File

@ -5849,7 +5849,7 @@ pub fn parse_block(
/// Compile an IR block for the `Block`, adding a parse warning on failure /// Compile an IR block for the `Block`, adding a parse warning on failure
fn compile_block(working_set: &mut StateWorkingSet<'_>, block: &mut Block) { fn compile_block(working_set: &mut StateWorkingSet<'_>, block: &mut Block) {
match nu_engine::compile(working_set, &block) { match nu_engine::compile(working_set, block) {
Ok(ir_block) => { Ok(ir_block) => {
block.ir_block = Some(ir_block); block.ir_block = Some(ir_block);
} }

View File

@ -53,7 +53,7 @@ impl Call<'_> {
pub fn assert_ast_call(&self) -> Result<&ast::Call, ShellError> { pub fn assert_ast_call(&self) -> Result<&ast::Call, ShellError> {
match &self.inner { match &self.inner {
CallImpl::AstRef(call) => Ok(call), CallImpl::AstRef(call) => Ok(call),
CallImpl::AstBox(call) => Ok(&call), CallImpl::AstBox(call) => Ok(call),
_ => Err(ShellError::NushellFailedSpanned { _ => Err(ShellError::NushellFailedSpanned {
msg: "Can't be used in IR context".into(), msg: "Can't be used in IR context".into(),
label: "this command is not yet supported by IR evaluation".into(), label: "this command is not yet supported by IR evaluation".into(),

View File

@ -155,8 +155,7 @@ impl Call {
_ => None, _ => None,
}) })
.nth(index) .nth(index)
.map(|option| option.as_ref()) .and_then(|option| option.as_ref())
.flatten()
} }
/// Returns every argument to the rest parameter, as well as whether each argument /// Returns every argument to the rest parameter, as well as whether each argument

View File

@ -40,7 +40,7 @@ impl ListStream {
} }
/// Take a single value from the inner `Iterator`, modifying the stream. /// Take a single value from the inner `Iterator`, modifying the stream.
pub fn next(&mut self) -> Option<Value> { pub fn next_value(&mut self) -> Option<Value> {
self.stream.next() self.stream.next()
} }