Skip to content

replace_let_with_if_let panics on unterminated string literal #22106

@Albab-Hasan

Description

@Albab-Hasan

when invoking Replace let with if let on a let binding whose initializer is an unterminated string literal r-a panics inside the assist handler. the syntax tree is parseable but incomplete and SyntaxFactory::expr_if calls ast.then_branch().unwrap() on the constructed node without guarding against the case where the branch is absent returning None for malformed input and causing an unconditional unwrap failure.

reproduce:

let s = "foo

place cursor on the l of let and apply Replace let with if let.

expected:
the assist should either transform the binding correctly or report that it cannot handle the incomplete expression, without crashing the server.

actual:
thread 'main' panicked at 'called Option::unwrap() on a None value', crates/syntax/src/ast/syntax_factory/constructors.rs:1141

the language server crashes and the editor loses all diagnostics and completions until it restarts.

env:

  • rustc: 1.94.0
  • rust-analyzer: 0.3.2854
  • OS: Ubuntu
  • Editor: VS Code

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions