Skip to content

Commit 8429dcd

Browse files
authored
Rollup merge of #92715 - chordtoll:empty-string, r=davidtwco
Do not suggest char literal for zero-length strings PR #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.
2 parents d7c2eda + 9622a42 commit 8429dcd

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

Diff for: compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20532053
if let Some(code) =
20542054
code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
20552055
{
2056-
if code.chars().nth(1).is_none() {
2056+
if code.chars().count() == 1 {
20572057
err.span_suggestion(
20582058
span,
20592059
"if you meant to write a `char` literal, use single quotes",

Diff for: src/test/ui/inference/char-as-str-multi.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// When a MULTI-character string literal is used where a char should be,
1+
// When a MULTI/NO-character string literal is used where a char should be,
22
// DO NOT suggest changing to single quotes.
33

44
fn main() {
55
let _: char = "foo"; //~ ERROR mismatched types
6+
let _: char = ""; //~ ERROR mismatched types
67
}

Diff for: src/test/ui/inference/char-as-str-multi.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ LL | let _: char = "foo";
66
| |
77
| expected due to this
88

9-
error: aborting due to previous error
9+
error[E0308]: mismatched types
10+
--> $DIR/char-as-str-multi.rs:6:19
11+
|
12+
LL | let _: char = "";
13+
| ---- ^^ expected `char`, found `&str`
14+
| |
15+
| expected due to this
16+
17+
error: aborting due to 2 previous errors
1018

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

0 commit comments

Comments
 (0)