rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.31k stars 1.62k forks source link

Quick fix on an unsaved file can change the wrong lines #13650

Open max-sixty opened 2 years ago

max-sixty commented 2 years ago

(Thank you for the excellent extension!)

rust-analyzer version: rust-analyzer version: 0.3.1285-standalone (45ec315e0 2022-11-11)

rustc version: rustc 1.64.0 (a55dd71d5 2022-09-19)

I have the following code:

                let fix_left = match op.associativity() {
                    Associativity::Right => true,
                    _ => false,
                };
                let fix_right = match op.associativity() {
                    Associativity::Left => true,
                    _ => false,
                };

There is an option to apply a quick fix to each block. I select it:

image

...and I successfully get the following code:

                let fix_left = matches!(op.associativity(), Associativity::Right);
                let fix_right = match op.associativity() {
                    Associativity::Left => true,
                    _ => false,
                };

But — then I go to apply the quick fix for the second block — and it mangles the lines, removing the let fix_right text:

                let fix_left = matches!(op.associativity(), Associativity::Right)   _ => false,
                };

Saving the file between the two actions resolves the issue.

Thanks!

jhgg commented 2 years ago

This is unfortunately unfixable for quickfixes that are a result of flycheck (cargo check), since it is basically going off of a cached version of the diagnostics prior to save.

I wonder if we can disable the quick-fixes if you modify the file flycheck has run to avoid confusion here.

flodiebold commented 2 years ago

I wonder if we can disable the quick-fixes if you modify the file flycheck has run to avoid confusion here.

That's also annoying though since if you do it in the right order, you can actually apply multiple fixes without saving in between. I guess we could disable quick-fixes where the touched code doesn't match anymore.