diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 6fdfc8e39f10d..8d41497541a19 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2053,7 +2053,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", diff --git a/src/test/ui/inference/char-as-str-multi.rs b/src/test/ui/inference/char-as-str-multi.rs index 21bbc6f20b294..c29a15025a9c2 100644 --- a/src/test/ui/inference/char-as-str-multi.rs +++ b/src/test/ui/inference/char-as-str-multi.rs @@ -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 } diff --git a/src/test/ui/inference/char-as-str-multi.stderr b/src/test/ui/inference/char-as-str-multi.stderr index c3ba17a5579ad..297ca2b548f71 100644 --- a/src/test/ui/inference/char-as-str-multi.stderr +++ b/src/test/ui/inference/char-as-str-multi.stderr @@ -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`.