rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.32k stars 12.58k forks source link

Auto-fix wrongly suggesting scope issues during struct manipulations #71027

Open jerryajay opened 4 years ago

jerryajay commented 4 years ago

Hi, there's a auto-fix bug. I'm using VSCode. The code explains itself:

I tried this code:

use std::fmt;
use std::fmt::Error;
use std::fmt::Formatter;

struct Structure(i32);

#[derive(Debug)]
struct Point {
    x : i32,
    y : i32,
}

impl fmt::Display for Structure {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "The value inside the structure is: {}", self.0)
    }
}

fn main() {
    let s = Structure(12);
    println!("{}", s);
    let point = {x = 12; y = 45};
    println!("{}")
}

I expected to see the auto-fix suggest ways to print Point correctly.

Instead, auto-fix in VSCode suggests changing x and y to s in line 23. However, the error message from rust displays fine.

Meta

rustc --version --verbose:

rustc 1.42.0 (b8cedc004 2020-03-09)
binary: rustc
commit-hash: b8cedc00407a4c56a3bda1ed605c6fc166655447
commit-date: 2020-03-09
host: x86_64-unknown-linux-gnu
release: 1.42.0
LLVM version: 9.0
Backtrace

``` RUST_BACKTRACE=1 cargo build Compiling hello_world v0.1.0 (/home/jerry/rust-experiment/hello_world) error: 1 positional argument in format string, but no arguments were given --> src/main.rs:26:15 | 26 | println!("{}") | ^^ error[E0425]: cannot find value `x` in this scope --> src/main.rs:23:9 | 23 | x = 12; | ^ help: a local variable with a similar name exists: `s` error[E0425]: cannot find value `y` in this scope --> src/main.rs:24:9 | 24 | y = 45; | ^ help: a local variable with a similar name exists: `s` error: aborting due to 3 previous errors For more information about this error, try `rustc --explain E0425`. error: could not compile `hello_world`. ```

jonas-schievink commented 4 years ago

It's not really clear what this issue is about. Are you using RLS? I'm assuming it's displaying the suggestions in this error message?

error[E0425]: cannot find value `x` in this scope
  --> src/main.rs:23:18
   |
23 |     let point = {x = 12; y = 45};
   |                  ^ help: a local variable with a similar name exists: `s`

If so, that is definitely a correct error message and help, even if the result is not what you wanted (help is only a suggestion, not a solution, after all).

Maybe we can add extra diagnostics for syntax that looks like a C struct initializer, but I don't think too many people hit this.

jerryajay commented 4 years ago

Yes, RLS is being used. Rust is powerful for a blatant help message like this. I'll submit a pull request for the extra diagnostics - time permitting.