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

Rust should suggest single quotes when expecting char #109586

Closed
SergioB-dev opened this issue Mar 25, 2023 · 1 comment · Fixed by #109613
Closed

Rust should suggest single quotes when expecting char #109586

SergioB-dev opened this issue Mar 25, 2023 · 1 comment · Fixed by #109613
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@SergioB-dev
Copy link

SergioB-dev commented Mar 25, 2023

Code

fn convert_c_to_str(c: char) -> &str {
    match c {
        "A" => "T",
        "C" => "G",
        "T" => "A",
        "G" => "C",
        _ => c,
    }
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
3 |     match c {
  |           - this expression has type `char`
4 |         "A" => "T",
  |         ^^^ expected `char`, found `&str`

error[E0308]: mismatched types
 --> src/main.rs:5:9
  |
3 |     match c {
  |           - this expression has type `char`
4 |         "A" => "T",
5 |         "C" => "G",
  |         ^^^ expected `char`, found `&str`

error[E0308]: mismatched types
 --> src/main.rs:6:9
  |
3 |     match c {
  |           - this expression has type `char`
...
6 |         "T" => "A",
  |         ^^^ expected `char`, found `&str`

error[E0308]: mismatched types
 --> src/main.rs:7:9
  |
3 |     match c {
  |           - this expression has type `char`
...

Desired output

...

"Please use single quotes ('_') to generate chars"

Rationale and extra context

For beginners the error message could be a lot simpler. Sometimes they just want to make a char and using single quotes instead of double quotes is not terribly obvious.

Other cases

No response

Anything else?

No response

@SergioB-dev SergioB-dev 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 Mar 25, 2023
@Dylan-DPC Dylan-DPC added C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Mar 25, 2023
@jyn514 jyn514 added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Mar 25, 2023
@clubby789
Copy link
Contributor

clubby789 commented Mar 26, 2023

and as far as I know, all "mainstream" languages use a single quote to denote a character

Both JavaScript and Python have no character type, with " and ' being roughly interchangeable, so this is likely to be a common mistake for newcomers from other languages. Either way, whether it's a typo or a misunderstanding, having an IDE-applicable suggestion will help.

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Mar 27, 2023
…stion, r=compiler-errors

fix type suggestions in match arms

fix rust-lang#109586
@bors bors closed this as completed in 04b8523 Mar 27, 2023
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. 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.

4 participants