Require let to be a statement (#594)

This commit is contained in:
JT
2021-12-27 14:04:22 +11:00
committed by GitHub
parent de30236f38
commit 3706bef0a1
6 changed files with 58 additions and 31 deletions

View File

@ -195,6 +195,14 @@ pub enum ParseError {
#[diagnostic(code(nu::parser::file_not_found), url(docsrs))]
FileNotFound(String, #[label("File not found: {0}")] Span),
#[error("'let' statements can't be part of a pipeline")]
#[diagnostic(
code(nu::parser::let_not_statement),
url(docsrs),
help("use parens to assign to a variable\neg) let x = ('hello' | str length)")
)]
LetNotStatement(#[label = "let statement part of a pipeline"] Span),
#[error("{0}")]
#[diagnostic()]
LabeledError(String, String, #[label("{1}")] Span),

View File

@ -973,6 +973,10 @@ pub fn parse_let(
);
error = error.or(err);
if idx < (spans.len() - 1) {
error = error.or(Some(ParseError::ExtraPositional(spans[idx + 1])));
}
let mut idx = 0;
let (lvalue, err) =
parse_var_with_opt_type(working_set, &spans[1..(span.0)], &mut idx);

View File

@ -3352,6 +3352,16 @@ pub fn parse_block(
})
.collect::<Vec<Expression>>();
if let Some(let_call_id) = working_set.find_decl(b"let") {
for expr in output.iter() {
if let Expr::Call(x) = &expr.expr {
if let_call_id == x.decl_id && output.len() != 1 && error.is_none() {
error = Some(ParseError::LetNotStatement(expr.span));
}
}
}
}
for expr in output.iter_mut().skip(1) {
if expr.has_in_variable(working_set) {
*expr = wrap_expr_with_collect(working_set, expr);