rust-lang / rust-lang/rust-clippy

Clippy fix for unwrap_or(T::default()) in the absence of type inference.

Open
#7,079 6 comments 0 reactions 1 assignee View on GitHub

@nhamovitz is already working on this.

Since Oct 21, 2021.

C-bug E-medium I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

I ran clippy --fix on:

fn main() {
    let s = None.unwrap_or(String::default());
    let x = s.len();
    println!("{}", x);
}

This fixes to

fn main() {
    let s = None.unwrap_or_default();
    let x = s.len();
    println!("{}", x);
}

But now we've lost the type information and it no longer compiles. It should fix to

fn main() {
    let s: String = None.unwrap_or_default();
    let x = s.len();
    println!("{}", x);
}
Meta
  • cargo clippy -V: clippy 0.1.51 (2fd73fab 2021-03-23)
  • rustc -Vv:
    rustc 1.51.0 (2fd73fabe 2021-03-23)
    binary: rustc
    commit-hash: 2fd73fabe469357a12c2c974c140f67e7cdd76d0
    commit-date: 2021-03-23
    host: x86_64-apple-darwin
    release: 1.51.0
    LLVM version: 11.0.1
    

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.