Skip to content

Diagnostic for .unwrap_or_default() suggests removing unrelated code #149487

@nabijaczleweli

Description

@nabijaczleweli

Code

fn main() {
    let mut tx_heights: std::collections::BTreeMap<(), Option<()>> = <_>::default();
    tx_heights.get(&()).unwrap_or_default();
}

Current output

error[E0277]: the trait bound `&Option<()>: Default` is not satisfied
 --> a.rs:3:25
  |
3 |     tx_heights.get(&()).unwrap_or_default();
  |                         ^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `&Option<()>`
  |
  = help: the trait `Default` is implemented for `Option<T>`
note: required by a bound in `Option::<T>::unwrap_or_default`
 --> /rustc/6b00bc3880198600130e1cf62b8f8a93494488cc/library/core/src/option.rs:1073:5
help: consider removing this method call, as the receiver has type `BTreeMap<(), Option<()>>` and `BTreeMap<(), Option<()>>: Default` trivially holds
  |
3 -     tx_heights.get(&()).unwrap_or_default();
3 +     tx_heights.unwrap_or_default();
  |

error: aborting due to 1 previous error

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

nightly's even worse:

error[E0277]: the trait bound `&Option<()>: Default` is not satisfied
    --> a.rs:3:25
     |
   3 |     tx_heights.get(&()).unwrap_or_default();
     |                         ^^^^^^^^^^^^^^^^^ the trait `Default` is not implemented for `&Option<()>`
     |
help: the trait `Default` is implemented for `Option<T>`
    --> /mnt/filling/store/nabijaczleweli/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:2226:1
     |
2226 | impl<T> const Default for Option<T> {
     | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: required by a bound in `Option::<T>::unwrap_or_default`
    --> /mnt/filling/store/nabijaczleweli/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:1095:12
     |
1093 |     pub const fn unwrap_or_default(self) -> T
     |                  ----------------- required by a bound in this associated function
1094 |     where
1095 |         T: [const] Default,
     |            ^^^^^^^^^^^^^^^ required by this bound in `Option::<T>::unwrap_or_default`
help: consider removing this method call, as the receiver has type `BTreeMap<(), Option<()>>` and `BTreeMap<(), Option<()>>: Default` trivially holds
     |
   3 -     tx_heights.get(&()).unwrap_or_default();
   3 +     tx_heights.unwrap_or_default();
     |

error: aborting due to 1 previous error

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

Desired output

Not this.

Rust Version

rustc 1.93.0-nightly (cc3eee7fb 2025-11-28)
binary: rustc
commit-hash: cc3eee7fbe17ea4b7238531cb97e1b7b8bd6afce
commit-date: 2025-11-28
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.5

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-imprecise-spansDiagnostics: spans don't point to exactly the erroneous codeD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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