Suggest single quotes when char expected, str provided by chordtoll · Pull Request #92507 · rust-lang/rust (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation10 Commits1 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

@chordtoll

If a type mismatch occurs where a char is expected and a string literal is provided, suggest changing the double quotes to single quotes.

We already provide this suggestion in the other direction ( ' -> " ).

Especially useful for new rust devs used to a language in which single/double quotes are interchangeable.

Fixes #92479.

@rustbot rustbot added the T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.

label

Jan 3, 2022

@rust-highfive

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @petrochenkov (or someone else) soon.

Please see the contribution instructions for more information.

@joshtriplett

@chordtoll Could you please add a test that demonstrates the new suggestion?

fmease

fmease

@petrochenkov

We already provide this suggestion in the other direction ( ' -> " ).

Looks like we actually do not.
let s: &str = 'abc'; has the "abc" suggestion from the lexer just because multi-character literals are impossible.
However, let s: &str = 'a'; produces a "mismatched types" error without the suggestion.
So maybe you can also add another branch to the same match in compiler/rustc_infer/src/infer/error_reporting/mod.rs to address this opposite case.

@petrochenkov

r=me after squashing commits.

@chordtoll

@petrochenkov

@bors

📌 Commit 3087c4d has been approved by petrochenkov

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Jan 4, 2022

fmease

if let Some(code) =
code.strip_prefix('"').and_then(|s
{
if code.chars().nth(1).is_none() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no, I think my suggestion also fires for empty string literals "", sorry 😟! I think it's too late to change in this PR since it's already approved.

if code.chars().nth(1).is_none() {
if code.chars().nth(1).is_none() && !code.is_empty() {

Hmm that code is not that self-explanatory.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point- I'll open another PR to add the 0-character check

bors added a commit to rust-lang-ci/rust that referenced this pull request

Jan 4, 2022

@bors

…askrgr

Rollup of 7 pull requests

Successful merges:

Failed merges:

r? @ghost @rustbot modify labels: rollup

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Feb 8, 2022

@matthiaskrgr

Do not suggest char literal for zero-length strings

PR rust-lang#92507 adds a hint to switch to single quotes when a char is expected and a single-character string literal is provided.

The check to ensure the string literal is one character long missed the 0-char case, and would incorrectly offer the hint.

This PR adds the missing check, and a test case to confirm the new behavior.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request

Feb 8, 2022

@matthiaskrgr

Do not suggest char literal for zero-length strings

PR rust-lang#92507 adds a hint to switch to single quotes when a char is expected and a single-character string literal is provided.

The check to ensure the string literal is one character long missed the 0-char case, and would incorrectly offer the hint.

This PR adds the missing check, and a test case to confirm the new behavior.

Labels

S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.