Skip to content

Dereferencing Deref types other than references is missing suggestion #59819

@oli-obk

Description

@oli-obk

The b = a assignment below suggests dereferencing a in order to make the code compile. The same suggestion applies to y = x, but no suggestion is produced by the compiler. I think we should report this suggestion for all type mismatches that satisfy GotType: Deref<Target = ExpectedType>

struct Foo(i32);

impl std::ops::Deref for Foo {
    type Target = i32;
    fn deref(&self) -> &i32 {
        &self.0
    }
}

fn main() {
    let x = Foo(42);
    let y: i32 = x;
    let a = &42;
    let b: i32 = a;
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
  --> src/main.rs:12:18
   |
12 |     let y: i32 = x;
   |                  ^ expected i32, found struct `Foo`
   |
   = note: expected type `i32`
              found type `Foo`

error[E0308]: mismatched types
  --> src/main.rs:14:18
   |
14 |     let b: i32 = a;
   |                  ^
   |                  |
   |                  expected i32, found &{integer}
   |                  help: consider dereferencing the borrow: `*a`
   |
   = note: expected type `i32`
              found type `&{integer}`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions