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:
pwygab 2022-06-25 05:55:25 +08:00 committed by GitHub
parent f02076daa8
commit 1345f97202
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 0 deletions

View 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"));
}

View File

@ -30,6 +30,7 @@ mod into_filesize;
mod into_int;
mod last;
mod length;
mod let_;
mod lines;
mod ls;
mod math;

View File

@ -88,6 +88,14 @@ pub enum ParseError {
)]
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")]
#[diagnostic(code(nu::parser::incorrect_value), url(docsrs), help("{2}"))]
IncorrectValue(String, #[label("unexpected {0}")] Span, String),
@ -311,6 +319,7 @@ impl ParseError {
ParseError::UnexpectedKeyword(_, s) => *s,
ParseError::BuiltinCommandInPipeline(_, s) => *s,
ParseError::LetInPipeline(_, _, s) => *s,
ParseError::LetBuiltinVar(_, s) => *s,
ParseError::IncorrectValue(_, s, _) => *s,
ParseError::MultipleRestParams(s) => *s,
ParseError::VariableNotFound(s) => *s,

View File

@ -2286,6 +2286,15 @@ pub fn parse_let(
parse_var_with_opt_type(working_set, &spans[1..(span.0)], &mut idx);
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 rhs_type = rvalue.ty.clone();