mirror of
https://github.com/nushell/nushell
synced 2024-12-28 22:13:10 +00:00
Errors when let in
, let env
and similar commands are passed. (#5866)
* throw `let nu/env/nothing/in` error in parsing * add tests and fmt * fix clippy * suggestions * fmt * `lvalue.span` instead of `spans[1]` * clippy * fmt
This commit is contained in:
parent
f02076daa8
commit
1345f97202
4 changed files with 34 additions and 0 deletions
15
crates/nu-command/tests/commands/let_.rs
Normal file
15
crates/nu-command/tests/commands/let_.rs
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
use nu_test_support::{nu, pipeline};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn let_parse_error() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".", pipeline(
|
||||||
|
r#"
|
||||||
|
let in = 3
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert!(actual
|
||||||
|
.err
|
||||||
|
.contains("'in' is the name of a builtin Nushell variable"));
|
||||||
|
}
|
|
@ -30,6 +30,7 @@ mod into_filesize;
|
||||||
mod into_int;
|
mod into_int;
|
||||||
mod last;
|
mod last;
|
||||||
mod length;
|
mod length;
|
||||||
|
mod let_;
|
||||||
mod lines;
|
mod lines;
|
||||||
mod ls;
|
mod ls;
|
||||||
mod math;
|
mod math;
|
||||||
|
|
|
@ -88,6 +88,14 @@ pub enum ParseError {
|
||||||
)]
|
)]
|
||||||
LetInPipeline(String, String, #[label("let in pipeline")] Span),
|
LetInPipeline(String, String, #[label("let in pipeline")] Span),
|
||||||
|
|
||||||
|
#[error("Let used with builtin variable name.")]
|
||||||
|
#[diagnostic(
|
||||||
|
code(nu::parser::let_builtin_var),
|
||||||
|
url(docsrs),
|
||||||
|
help("'{0}' is the name of a builtin Nushell variable. `let` cannot assign to it.")
|
||||||
|
)]
|
||||||
|
LetBuiltinVar(String, #[label("already a builtin variable")] Span),
|
||||||
|
|
||||||
#[error("Incorrect value")]
|
#[error("Incorrect value")]
|
||||||
#[diagnostic(code(nu::parser::incorrect_value), url(docsrs), help("{2}"))]
|
#[diagnostic(code(nu::parser::incorrect_value), url(docsrs), help("{2}"))]
|
||||||
IncorrectValue(String, #[label("unexpected {0}")] Span, String),
|
IncorrectValue(String, #[label("unexpected {0}")] Span, String),
|
||||||
|
@ -311,6 +319,7 @@ impl ParseError {
|
||||||
ParseError::UnexpectedKeyword(_, s) => *s,
|
ParseError::UnexpectedKeyword(_, s) => *s,
|
||||||
ParseError::BuiltinCommandInPipeline(_, s) => *s,
|
ParseError::BuiltinCommandInPipeline(_, s) => *s,
|
||||||
ParseError::LetInPipeline(_, _, s) => *s,
|
ParseError::LetInPipeline(_, _, s) => *s,
|
||||||
|
ParseError::LetBuiltinVar(_, s) => *s,
|
||||||
ParseError::IncorrectValue(_, s, _) => *s,
|
ParseError::IncorrectValue(_, s, _) => *s,
|
||||||
ParseError::MultipleRestParams(s) => *s,
|
ParseError::MultipleRestParams(s) => *s,
|
||||||
ParseError::VariableNotFound(s) => *s,
|
ParseError::VariableNotFound(s) => *s,
|
||||||
|
|
|
@ -2286,6 +2286,15 @@ pub fn parse_let(
|
||||||
parse_var_with_opt_type(working_set, &spans[1..(span.0)], &mut idx);
|
parse_var_with_opt_type(working_set, &spans[1..(span.0)], &mut idx);
|
||||||
error = error.or(err);
|
error = error.or(err);
|
||||||
|
|
||||||
|
let var_name =
|
||||||
|
String::from_utf8_lossy(working_set.get_span_contents(lvalue.span))
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
if ["in", "nu", "env", "nothing"].contains(&var_name.as_str()) {
|
||||||
|
error =
|
||||||
|
error.or(Some(ParseError::LetBuiltinVar(var_name, lvalue.span)));
|
||||||
|
}
|
||||||
|
|
||||||
let var_id = lvalue.as_var();
|
let var_id = lvalue.as_var();
|
||||||
let rhs_type = rvalue.ty.clone();
|
let rhs_type = rvalue.ty.clone();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue