Skip to content

Broken snippets being applied in assert!, debug_assert! and format! #19069

@airblast-dev

Description

@airblast-dev

rust-analyzer version: rust-analyzer 1 (248bd51 2025-01-18)

rustc version: rustc 1.83.0 (90b35a623 2024-11-26)

editor or extension: Zed, Neovim (via rustaceanvim)

relevant settings: No environment variables, default settings for both editors and rustaceanvim.

repository link (if public, optional): No need, see the snippet.

When applying a snippet in assert!, debug_assert! or format! any whitespace (excluding ones inside string literals) get removed, and possibly broken code is inserted.
The problematic snippets I tested are box and unsafe but there might be more.
The exact behavior depends on the snippet, but using another snippet breaks things further and instead of just removing whitespaces. See a few examples in the reproducer.

code snippet to reproduce:

This is the code before applying the snippet.

fn main() {
    let a = true;
    assert!(if a == false { true } else { false });
}

After applying the snippet the whitespace's are removed from whatever was inside of it.
The example is with the unsafe snippet but it works with any of the snippets mentioned above.

fn main() {
    let a = true;
    assert!(unsafe { ifa==false{true}else{false} })
}

Not only does this break any code that needs whitespace's, it tampers with undo's in Zed as well (not in Neovim so Im unsure if this part is an editor bug or configuration bug of some kind).
Issuing an undo after applying the snippet yields us with:

fn main() {
    let a = true;
    assert!(if a == false { true } else { false }.unsafe { ifa==false{true}else{false} })
}

There are many snippet combinations that could be broken so just leaving a few examples instead of all possible combinations.

Applying a box snippet followed by an unsafe snippet.
Reversiving the order of how the snippet is applied yields us the correct result except of the missing whitespace's.

fn main() {
    let a = true;
    assert!(Box::new(ifa==false{true}else{false}).unsafe {
        
    })
}

Applying a box snippet twice.

fn main() {
    let a = true;
    assert!(Box::new(ifa==false{true}else{false}).Box)
}

Metadata

Metadata

Labels

A-macromacro expansionC-bugCategory: bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions