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

E0308 suggests to use to_string() in wrong position if if and else have incompatible types #83943

Closed
noeddl opened this issue Apr 6, 2021 · 0 comments · Fixed by #83952
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

@noeddl
Copy link
Contributor

noeddl commented Apr 6, 2021

Given the following code:

if true { "A".to_string() } else { "B" };

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=78d185647e3370e312d4cd675d94230e

The current output is:

error[E0308]: `if` and `else` have incompatible types
 --> src/main.rs:4:40
  |
4 |     if true { "A".to_string() } else { "B" };
  |               ---------------        --^^^--
  |               |                      | |
  |               |                      | expected struct `String`, found `&str`
  |               |                      help: try using a conversion method: `{ "B" }.to_string()`
  |               expected because of this

Ideally the output should look like:

error[E0308]: `if` and `else` have incompatible types
 --> src/main.rs:4:40
  |
4 |     if true { "A".to_string() } else { "B" };
  |               ---------------          ^^^
  |               |                        |
  |               |                        expected struct `String`, found `&str`
  |               |                        help: try using a conversion method: `"B".to_string()`
  |               expected because of this

This might be related to #83320.

@noeddl noeddl 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 Apr 6, 2021
@estebank estebank self-assigned this Apr 6, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Apr 7, 2021
Account for `ExprKind::Block` when suggesting .into() and deref

Fix rust-lang#83943.
@bors bors closed this as completed in e1efa17 Apr 8, 2021
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.

2 participants