Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest unwrap_or_else when a closure is passed to unwrap_or instead of suggesting calling it #102320

Closed
amrbashir opened this issue Sep 26, 2022 · 5 comments · Fixed by #102441
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@amrbashir
Copy link

amrbashir commented Sep 26, 2022

rustc should suggest unwrap_or_else if the argument is a closure instead of suggesting to call the closure

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=15e99f8e75edf68460c457c09e4e2fa4

fn main() {
  let x = "com.example.app";
  let y: Option<&str> = None;
  let s = y.unwrap_or(|| x.split('.').nth(1).unwrap());
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:4:23
  |
4 |   let s = y.unwrap_or(|| x.split('.').nth(1).unwrap());
  |             --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&str`, found closure
  |             |
  |             arguments to this function are incorrect
  |
  = note: expected reference `&str`
               found closure `[closure@src/main.rs:4:23: 4:25]`
note: associated function defined here
help: use parentheses to call this closure
  |
4 |   let s = y.unwrap_or((|| x.split('.').nth(1).unwrap())());
  |                       +                               +++

Ideally the output should look like:

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
 --> src/main.rs:4:23
  |
4 |   let s = y.unwrap_or(|| x.split('.').nth(1).unwrap());
  |             --------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `&str`, found closure
  |             |
  |             arguments to this function are incorrect
  |
  = note: expected reference `&str`
               found closure `[closure@src/main.rs:4:23: 4:25]`
note: associated function defined here
help: use "unwrap_or_else"
  |
4 |   let s = y.unwrap_or_else(|| x.split('.').nth(1).unwrap());
  |                      +++++                              
@amrbashir amrbashir added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 26, 2022
@chenyukang
Copy link
Member

@rustbot claim

@compiler-errors
Copy link
Member

This probably can be generalized if the code is written carefully enough.

@chenyukang
Copy link
Member

This probably can be generalized if the code is written carefully enough.

Do you mean, we should handle other types like Result which may use unwrap_or with a closure?

@amrbashir
Copy link
Author

I know nothing about the internals of rustc diagnostics but a general way would be to check if a similar method in name accepts a closure and suggest that first, if nothing similar can be found then suggest calling the closure

@compiler-errors
Copy link
Member

@chenyukang: I mean something more like what @amrbashir is suggesting, where you check if {FUNCTION}_else exists whose signature is compatible with a closure. But that requires doing a method probe, then doing some delicate logic to check argument compatibility.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 4, 2022
…se, r=compiler-errors

Suggest unwrap_or_else when a closure is given

Fixes rust-lang#102320

r? `@compiler-errors`
@bors bors closed this as completed in 0188273 Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants