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 }})
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 added the T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
label
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.
@chordtoll Could you please add a test that demonstrates the new suggestion?
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.
r=me after squashing commits.
📌 Commit 3087c4d has been approved by petrochenkov
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
| 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
…askrgr
Rollup of 7 pull requests
Successful merges:
- rust-lang#91754 (Modifications to
std::io::Stdinon 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_iterinto a clickable link) - rust-lang#92532 (revert rust-lang#92254 "Bump gsgdt to 0.1.3")
Failed merges:
r? @ghost
@rustbot modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request
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
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
Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Relevant to the compiler team, which will review and decide on the PR/issue.