Skip to content

Suggest to directly use a value instead of wrapping the unwrapped value #99416

Closed
@kellerkindt

Description

@kellerkindt

Given the following code: this

pub struct MyWrapper(u32);

fn main() {
    let value = MyWrapper(123);
    some_fn(value.0);
}

fn some_fn(wrapped: MyWrapper) {
    drop(wrapped);
}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:6:13
  |
6 |     some_fn(value.0);
  |             ^^^^^^^ expected struct `MyWrapper`, found `u32`
  |
help: try wrapping the expression in `MyWrapper`
  |
6 |     some_fn(MyWrapper(value.0));
  |             ++++++++++       +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

Compiling playground v0.0.1 (/playground)
error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:6:13
  |
6 |     some_fn(value.0);
  |             ^^^^^^^ expected struct `MyWrapper`, found `u32`
  |
help: try using `value` directly
  |
6 |     some_fn(value);
  |             +++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Just encountered this as I was refactoring code to properly use my tuple struct instead of an raw integer identifier.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions