Skip to content

Commit 9622a42

Browse files
committed
Only suggest char literal for single-character strings
1 parent 02fe61b commit 9622a42

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2068,7 +2068,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20682068
if let Some(code) =
20692069
code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
20702070
{
2071-
if code.chars().nth(1).is_none() {
2071+
if code.chars().count() == 1 {
20722072
err.span_suggestion(
20732073
span,
20742074
"if you meant to write a `char` literal, use single quotes",
+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
}

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)