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.
This commit is contained in:
commit
8429dcdb79
3 changed files with 12 additions and 3 deletions
|
@ -2053,7 +2053,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||||
if let Some(code) =
|
if let Some(code) =
|
||||||
code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
|
code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
|
||||||
{
|
{
|
||||||
if code.chars().nth(1).is_none() {
|
if code.chars().count() == 1 {
|
||||||
err.span_suggestion(
|
err.span_suggestion(
|
||||||
span,
|
span,
|
||||||
"if you meant to write a `char` literal, use single quotes",
|
"if you meant to write a `char` literal, use single quotes",
|
||||||
|
|
|
@ -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.
|
// DO NOT suggest changing to single quotes.
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _: char = "foo"; //~ ERROR mismatched types
|
let _: char = "foo"; //~ ERROR mismatched types
|
||||||
|
let _: char = ""; //~ ERROR mismatched types
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,14 @@ LL | let _: char = "foo";
|
||||||
| |
|
| |
|
||||||
| expected due to this
|
| 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`.
|
For more information about this error, try `rustc --explain E0308`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue