DioxusLabs / DioxusLabs/dioxus

dx fmt corrupts RSX onclick closures with cfg-gated let bindings

Open
#5,523 1 comment 1 reaction 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

**Problem**

Disclaimer: the problem occured to me, AI was used to minify the breaking code and summarize the issue. I hope that is OK

`dx fmt --file` rewrites a valid Dioxus RSX event handler into invalid Rust.

The breakage appears when an `onclick: { ... }` handler contains:

- an outer `#[cfg(...)] let`
- a `move |_| { ... }` closure
- an inner `#[cfg(...)] let`
- an `if let Err(err)` block with `error.set(format!(...))`

After formatting, `dx fmt` inserts stray fragments like `.clone();` and truncates the `error.set(format!(...))` call, so the file no longer compiles.

**Steps To Reproduce**

Steps to reproduce the behavior:

- Save this as `repro.rs`:
```rust
use dioxus::prelude::*;

fn app() -> Element {
let mut uploading = use_signal(|| false);
let mut error = use_signal(String::new);
let create_photo_collection = String::new();

rsx! {
button {
onclick: {
#[cfg(target_os = "android")]
let create_photo_collection_gallery = create_photo_collection.clone();

move |_| {
uploading.set(true);
error.set(String::new());

#[cfg(target_os = "android")]
let create_photo_collection_gallery_call =
create_photo_collection_gallery.clone();

if let Err(err) = Err::<(), _>("x") {
error.set(format!(
"{}: {}",
"err",
err
));
uploading.set(false);
return;
}
}
},
"x"
}
}
}

fn main() {}
```
- Run:
```bash
dx fmt --file repro.rs
```
- Inspect the formatted file.

**Expected behavior**

`dx fmt` should preserve valid Rust/RSX and produce a formatted file that still compiles.

Instead, it rewrites the snippet into broken code like:

```rust
#[cfg(target_os = "android")]
let create_photo_collection_gallery_call =
create_photo_collection_gallery.clone();
.clone();

if let Err(err) = Err::<(), _>("x") {
err
));
uploading.set(false);
return;
}
```

**Screenshots**

N/A

**Environment:**

- Dioxus version: dioxus 0.7.7
- Rust version: rustc 1.95.0
- OS info: Linux
- App platform: N/A (formatter / RSX source issue)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.