Skip to content

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

Closed
@noeddl

Description

@noeddl

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.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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