diff --git a/crates/nu-parser/src/parse_keywords.rs b/crates/nu-parser/src/parse_keywords.rs index 1f8dd5988e..e43ff0682d 100644 --- a/crates/nu-parser/src/parse_keywords.rs +++ b/crates/nu-parser/src/parse_keywords.rs @@ -2894,7 +2894,9 @@ pub fn parse_let(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipeline // so that the var-id created by the variable isn't visible in the expression that init it for span in spans.iter().enumerate() { let item = working_set.get_span_contents(*span.1); - if item == b"=" && spans.len() > (span.0 + 1) { + // https://github.com/nushell/nushell/issues/9596, let = if $ + // let x = 'f', = at least start from index 2 + if item == b"=" && spans.len() > (span.0 + 1) && span.0 > 1 { let (tokens, parse_error) = lex( working_set.get_span_contents(nu_protocol::span(&spans[(span.0 + 1)..])), spans[span.0 + 1].start, @@ -3013,7 +3015,8 @@ pub fn parse_const(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipelin // so that the var-id created by the variable isn't visible in the expression that init it for span in spans.iter().enumerate() { let item = working_set.get_span_contents(*span.1); - if item == b"=" && spans.len() > (span.0 + 1) { + // const x = 'f', = at least start from index 2 + if item == b"=" && spans.len() > (span.0 + 1) && span.0 > 1 { let mut idx = span.0; // let rvalue = parse_multispan_value( // working_set, @@ -3151,7 +3154,8 @@ pub fn parse_mut(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipeline // so that the var-id created by the variable isn't visible in the expression that init it for span in spans.iter().enumerate() { let item = working_set.get_span_contents(*span.1); - if item == b"=" && spans.len() > (span.0 + 1) { + // mut x = 'f', = at least start from index 2 + if item == b"=" && spans.len() > (span.0 + 1) && span.0 > 1 { let (tokens, parse_error) = lex( working_set.get_span_contents(nu_protocol::span(&spans[(span.0 + 1)..])), spans[span.0 + 1].start, diff --git a/src/tests/test_parser.rs b/src/tests/test_parser.rs index 65cf35d385..57e24aed53 100644 --- a/src/tests/test_parser.rs +++ b/src/tests/test_parser.rs @@ -145,6 +145,25 @@ fn bad_var_name2() -> TestResult { fail_test(r#"let $foo-bar = 4"#, "valid variable") } +#[test] +fn assignment_with_no_var() -> TestResult { + let cases = [ + "let = if $", + "mut = if $", + "const = if $", + "let = 'foo' | $in; $x | describe", + "mut = 'foo' | $in; $x | describe", + ]; + + let expected = "valid variable"; + + for case in cases { + fail_test(case, expected)?; + } + + Ok(()) +} + #[test] fn long_flag() -> TestResult { run_test(