Skip to content

Suggest single quotes when char expected, str provided #92507

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jan 4, 2022

Conversation

chordtoll
Copy link
Contributor

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
Copy link
Contributor

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 3, 2022
@joshtriplett
Copy link
Member

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

@petrochenkov
Copy link
Contributor

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 petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 4, 2022
@petrochenkov
Copy link
Contributor

r=me after squashing commits.

@chordtoll chordtoll force-pushed the suggest-single-quotes branch from 0edffee to 3087c4d Compare January 4, 2022 06:09
@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 4, 2022

📌 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
if let Some(code) =
code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
{
if code.chars().nth(1).is_none() {
Copy link
Member

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.

Suggested change
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.

Copy link
Contributor Author

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
…askrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#91754 (Modifications to `std::io::Stdin` on Windows so that there is no longer a 4-byte buffer minimum in read().)
 - rust-lang#91884 (Constify `Box<T, A>` methods)
 - rust-lang#92107 (Actually set IMAGE_SCN_LNK_REMOVE for .rmeta)
 - rust-lang#92456 (Make the documentation of builtin macro attributes accessible)
 - rust-lang#92507 (Suggest single quotes when char expected, str provided)
 - rust-lang#92525 (intra-doc: Make `Receiver::into_iter` into a clickable link)
 - rust-lang#92532 (revert rust-lang#92254 "Bump gsgdt to 0.1.3")

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 25fcc0e into rust-lang:master Jan 4, 2022
@rustbot rustbot added this to the 1.59.0 milestone Jan 4, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 8, 2022
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
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.
# for free to join this conversation on GitHub. Already have an account? # to comment
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest using single quotes when confusing single character strings and chars
7 participants