Closed
Description
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());
| +++++