rust-lang / rust-lang/rust-analyzer

`replace_let_with_if_let` panics on unterminated string literal

Open
#22,106 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-assists C-bug I-panic
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the crash by applying Replace let with if let to the unterminated string example, then inspect crates/syntax/src/ast/syntax_factory/constructors.rs:1141 and the SyntaxFactory::expr_if path. Done means malformed input no longer panics and the assist either transforms the binding or reports that it cannot handle the incomplete expression.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.