mirror of
https://github.com/nushell/nushell.git
synced 2024-12-26 08:59:12 +01:00
Remove usages of Call::positional_nth
(#12871)
# Description Following from #12867, this PR replaces usages of `Call::positional_nth` with existing spans. This removes several `expect`s from the code. Also remove unused `positional_nth_mut` and `positional_iter_mut`
This commit is contained in:
parent
b08135d877
commit
06fe7d1e16
10
crates/nu-command/src/env/with_env.rs
vendored
10
crates/nu-command/src/env/with_env.rs
vendored
@ -90,10 +90,7 @@ fn with_env(
|
|||||||
return Err(ShellError::CantConvert {
|
return Err(ShellError::CantConvert {
|
||||||
to_type: "record".into(),
|
to_type: "record".into(),
|
||||||
from_type: x.get_type().to_string(),
|
from_type: x.get_type().to_string(),
|
||||||
span: call
|
span: x.span(),
|
||||||
.positional_nth(1)
|
|
||||||
.expect("already checked through .req")
|
|
||||||
.span,
|
|
||||||
help: None,
|
help: None,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -124,10 +121,7 @@ fn with_env(
|
|||||||
return Err(ShellError::CantConvert {
|
return Err(ShellError::CantConvert {
|
||||||
to_type: "record".into(),
|
to_type: "record".into(),
|
||||||
from_type: x.get_type().to_string(),
|
from_type: x.get_type().to_string(),
|
||||||
span: call
|
span: x.span(),
|
||||||
.positional_nth(1)
|
|
||||||
.expect("already checked through .req")
|
|
||||||
.span,
|
|
||||||
help: None,
|
help: None,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -117,7 +117,7 @@ impl Command for Touch {
|
|||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
let cwd = current_dir(engine_state, stack)?;
|
let cwd = current_dir(engine_state, stack)?;
|
||||||
|
|
||||||
for (index, glob) in files.into_iter().enumerate() {
|
for glob in files {
|
||||||
let path = expand_path_with(glob.item.as_ref(), &cwd, glob.item.is_expand());
|
let path = expand_path_with(glob.item.as_ref(), &cwd, glob.item.is_expand());
|
||||||
|
|
||||||
// If --no-create is passed and the file/dir does not exist there's nothing to do
|
// If --no-create is passed and the file/dir does not exist there's nothing to do
|
||||||
@ -135,10 +135,7 @@ impl Command for Touch {
|
|||||||
{
|
{
|
||||||
return Err(ShellError::CreateNotPossible {
|
return Err(ShellError::CreateNotPossible {
|
||||||
msg: format!("Failed to create file: {err}"),
|
msg: format!("Failed to create file: {err}"),
|
||||||
span: call
|
span: glob.span,
|
||||||
.positional_nth(index)
|
|
||||||
.expect("already checked positional")
|
|
||||||
.span,
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -148,10 +145,7 @@ impl Command for Touch {
|
|||||||
{
|
{
|
||||||
return Err(ShellError::ChangeModifiedTimeNotPossible {
|
return Err(ShellError::ChangeModifiedTimeNotPossible {
|
||||||
msg: format!("Failed to change the modified time: {err}"),
|
msg: format!("Failed to change the modified time: {err}"),
|
||||||
span: call
|
span: glob.span,
|
||||||
.positional_nth(index)
|
|
||||||
.expect("already checked positional")
|
|
||||||
.span,
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -161,10 +155,7 @@ impl Command for Touch {
|
|||||||
{
|
{
|
||||||
return Err(ShellError::ChangeAccessTimeNotPossible {
|
return Err(ShellError::ChangeAccessTimeNotPossible {
|
||||||
msg: format!("Failed to change the access time: {err}"),
|
msg: format!("Failed to change the access time: {err}"),
|
||||||
span: call
|
span: glob.span,
|
||||||
.positional_nth(index)
|
|
||||||
.expect("already checked positional")
|
|
||||||
.span,
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -784,10 +784,7 @@ fn heavy_lifting(code: Value, escape: bool, osc: bool, call: &Call) -> Result<St
|
|||||||
None => {
|
None => {
|
||||||
return Err(ShellError::TypeMismatch {
|
return Err(ShellError::TypeMismatch {
|
||||||
err_message: String::from("Unknown ansi code"),
|
err_message: String::from("Unknown ansi code"),
|
||||||
span: call
|
span: code.span(),
|
||||||
.positional_nth(0)
|
|
||||||
.expect("Unexpected missing argument")
|
|
||||||
.span,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -154,30 +154,10 @@ impl Call {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn positional_iter_mut(&mut self) -> impl Iterator<Item = &mut Expression> {
|
|
||||||
self.arguments
|
|
||||||
.iter_mut()
|
|
||||||
.take_while(|arg| match arg {
|
|
||||||
Argument::Spread(_) => false, // Don't include positional arguments given to rest parameter
|
|
||||||
_ => true,
|
|
||||||
})
|
|
||||||
.filter_map(|arg| match arg {
|
|
||||||
Argument::Named(_) => None,
|
|
||||||
Argument::Positional(positional) => Some(positional),
|
|
||||||
Argument::Unknown(unknown) => Some(unknown),
|
|
||||||
Argument::Spread(_) => None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn positional_nth(&self, i: usize) -> Option<&Expression> {
|
pub fn positional_nth(&self, i: usize) -> Option<&Expression> {
|
||||||
self.positional_iter().nth(i)
|
self.positional_iter().nth(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO this method is never used. Delete?
|
|
||||||
pub fn positional_nth_mut(&mut self, i: usize) -> Option<&mut Expression> {
|
|
||||||
self.positional_iter_mut().nth(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn positional_len(&self) -> usize {
|
pub fn positional_len(&self) -> usize {
|
||||||
self.positional_iter().count()
|
self.positional_iter().count()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user