From 3d62528d8ce472db16ffc663fc342b3143f87b09 Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Thu, 26 May 2022 08:13:14 +0800 Subject: [PATCH] Makes a more helpful error for `let` in pipeline (#5632) * a more helpful error for let in pipeline * a more helpful error for let in pipeline fmt * changed help message * type-o Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com> --- crates/nu-parser/src/errors.rs | 11 +++++++++++ crates/nu-parser/src/parser.rs | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/nu-parser/src/errors.rs b/crates/nu-parser/src/errors.rs index 4f51d004af..a2ed170d31 100644 --- a/crates/nu-parser/src/errors.rs +++ b/crates/nu-parser/src/errors.rs @@ -78,6 +78,16 @@ pub enum ParseError { )] BuiltinCommandInPipeline(String, #[label("not allowed in pipeline")] Span), + #[error("Let statement used in pipeline.")] + #[diagnostic( + code(nu::parser::unexpected_keyword), + url(docsrs), + help( + "Assigning '{0}' to '{1}' does not produce a value to be piped. If the pipeline is meant to apply to '{0}' by itself, use 'let {1} = ({0} | ...)'." + ) + )] + LetInPipeline(String, String, #[label("let in pipeline")] Span), + #[error("Incorrect value")] #[diagnostic(code(nu::parser::incorrect_value), url(docsrs), help("{2}"))] IncorrectValue(String, #[label("unexpected {0}")] Span, String), @@ -300,6 +310,7 @@ impl ParseError { ParseError::ExpectedKeyword(_, s) => *s, ParseError::UnexpectedKeyword(_, s) => *s, ParseError::BuiltinCommandInPipeline(_, s) => *s, + ParseError::LetInPipeline(_, _, s) => *s, ParseError::IncorrectValue(_, s, _) => *s, ParseError::MultipleRestParams(s) => *s, ParseError::VariableNotFound(s) => *s, diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index c7ec102231..5ecef6cca2 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -4371,7 +4371,12 @@ pub fn parse_expression( expand_aliases_denylist, ) .0, - Some(ParseError::BuiltinCommandInPipeline("let".into(), spans[0])), + Some(ParseError::LetInPipeline( + String::from_utf8_lossy(working_set.get_span_contents(spans[spans.len() - 1])) + .to_string(), + String::from_utf8_lossy(working_set.get_span_contents(spans[1])).to_string(), + spans[0], + )), ), b"alias" => ( parse_call(