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

Do not suggest char literal for zero-length strings #92715

Merged
merged 1 commit into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2068,7 +2068,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
if let Some(code) =
code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
{
if code.chars().nth(1).is_none() {
if code.chars().count() == 1 {
err.span_suggestion(
span,
"if you meant to write a `char` literal, use single quotes",
Expand Down
3 changes: 2 additions & 1 deletion src/test/ui/inference/char-as-str-multi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// When a MULTI-character string literal is used where a char should be,
// When a MULTI/NO-character string literal is used where a char should be,
// DO NOT suggest changing to single quotes.

fn main() {
let _: char = "foo"; //~ ERROR mismatched types
let _: char = ""; //~ ERROR mismatched types
}
10 changes: 9 additions & 1 deletion src/test/ui/inference/char-as-str-multi.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ LL | let _: char = "foo";
| |
| expected due to this

error: aborting due to previous error
error[E0308]: mismatched types
--> $DIR/char-as-str-multi.rs:6:19
|
LL | let _: char = "";
| ---- ^^ expected `char`, found `&str`
| |
| expected due to this

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.