rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.67k stars 2.41k forks source link

`cargo fix`: duplicate diagnostic detection does not handle insert-only replacements #13027

Open ehuss opened 3 years ago

ehuss commented 3 years ago

The following example:

macro_rules! foo {
    () => {
        &1;
    };
}

fn main() {
    foo!();
    foo!();
}

generates a suggestion (on beta 1.55) to insert a fix like this:

let _ = &1;

However, since this diagnostic is triggered from macros, rustc emits two separate machine-applicable suggestions at the exact same spot. That causes rustfix to end up changing the code to:

let _ = let _ = &1;

which fails to compile.

rust-lang/rustfix#131 added detection to avoid applying duplicate suggestions, but I believe it does not handle the case when it is an insert_only suggestion.

torhovland commented 5 months ago

https://github.com/rust-lang/rustfix/pull/131 seems to work because in the case of duplicate replacements, it is enough to check if a replacement is the same before and after. A duplicate insertion wouldn't be able to do that, and would need a more fundamental fix.