Skip to content

Bad suggestion for &mut in combination with Default (maybe also other traits) #100289

Closed

Description

Given the following code:

fn foo(bar: &mut usize) {
    todo!()
}

fn main() {
    foo(&mut Default::default()); // Works
    foo(Default::default()); // Doesn't
}

The current output is:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `&mut usize: Default` is not satisfied
 --> src/main.rs:7:9
  |
7 |     foo(Default::default()); // Doesn't
  |     --- ^^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `&mut usize`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the following other types implement trait `Default`:
            f32
            f64
            i128
            i16
            i32
            i64
            i8
            isize
          and 6 others

For more information about this error, try `rustc --explain E0277`.

This output is confusing, because changing the type to e.g. i64 won't solve the problem and instead cause a lot of new ones. For a HashMap, I currently get the suggestion to use a HashSet. The correct solution would be to recognize that usize: Default and suggest to add a &mut. This may generalize to other similar situations (&) or traits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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