1
Fork 0

Use shorter span for existing ' -> " structured suggestion

This commit is contained in:
Esteban Küber 2024-03-09 01:07:23 +00:00
parent 982918f493
commit 4a10b01f95
14 changed files with 51 additions and 41 deletions

View file

@ -1339,15 +1339,12 @@ pub enum TypeErrorAdditionalDiags {
span: Span, span: Span,
code: String, code: String,
}, },
#[suggestion( #[multipart_suggestion(infer_meant_str_literal, applicability = "machine-applicable")]
infer_meant_str_literal,
code = "\"{code}\"",
applicability = "machine-applicable"
)]
MeantStrLiteral { MeantStrLiteral {
#[primary_span] #[suggestion_part(code = "\"")]
span: Span, start: Span,
code: String, #[suggestion_part(code = "\"")]
end: Span,
}, },
#[suggestion( #[suggestion(
infer_consider_specifying_length, infer_consider_specifying_length,

View file

@ -2078,16 +2078,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// If a string was expected and the found expression is a character literal, // If a string was expected and the found expression is a character literal,
// perhaps the user meant to write `"s"` to specify a string literal. // perhaps the user meant to write `"s"` to specify a string literal.
(ty::Ref(_, r, _), ty::Char) if r.is_str() => { (ty::Ref(_, r, _), ty::Char) if r.is_str() => {
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) { suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
if let Some(code) = start: span.with_hi(span.lo() + BytePos(1)),
code.strip_prefix('\'').and_then(|s| s.strip_suffix('\'')) end: span.with_lo(span.hi() - BytePos(1)),
{ })
suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
span,
code: escape_literal(code),
})
}
}
} }
// For code `if Some(..) = expr `, the type mismatch may be expected `bool` but found `()`, // For code `if Some(..) = expr `, the type mismatch may be expected `bool` but found `()`,
// we try to suggest to add the missing `let` for `if let Some(..) = expr` // we try to suggest to add the missing `let` for `if let Some(..) = expr`

View file

@ -2216,12 +2216,21 @@ pub enum MoreThanOneCharSugg {
ch: String, ch: String,
}, },
#[suggestion(parse_use_double_quotes, code = "{sugg}", applicability = "machine-applicable")] #[suggestion(parse_use_double_quotes, code = "{sugg}", applicability = "machine-applicable")]
Quotes { QuotesFull {
#[primary_span] #[primary_span]
span: Span, span: Span,
is_byte: bool, is_byte: bool,
sugg: String, sugg: String,
}, },
#[multipart_suggestion(parse_use_double_quotes, applicability = "machine-applicable")]
Quotes {
#[suggestion_part(code = "{prefix}\"")]
start: Span,
#[suggestion_part(code = "\"")]
end: Span,
is_byte: bool,
prefix: &'static str,
},
} }
#[derive(Subdiagnostic)] #[derive(Subdiagnostic)]

View file

@ -95,11 +95,21 @@ pub(crate) fn emit_unescape_error(
} }
escaped.push(c); escaped.push(c);
} }
let sugg = format!("{prefix}\"{escaped}\""); if escaped.len() != lit.len() {
MoreThanOneCharSugg::Quotes { let sugg = format!("{prefix}\"{escaped}\"");
span: full_lit_span, MoreThanOneCharSugg::QuotesFull {
is_byte: mode == Mode::Byte, span: full_lit_span,
sugg, is_byte: mode == Mode::Byte,
sugg,
}
} else {
MoreThanOneCharSugg::Quotes {
start: full_lit_span
.with_hi(full_lit_span.lo() + BytePos((prefix.len() + 1) as u32)),
end: full_lit_span.with_lo(full_lit_span.hi() - BytePos(1)),
is_byte: mode == Mode::Byte,
prefix,
}
} }
}); });
dcx.emit_err(UnescapeError::MoreThanOneChar { dcx.emit_err(UnescapeError::MoreThanOneChar {

View file

@ -18,7 +18,7 @@ LL | let _: &str = '\"\"\"';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let _: &str = "\"\"\""; LL | let _: &str = "\"\"\"";
| ~~~~~~~~ | ~ ~
error: character literal may only contain one codepoint error: character literal may only contain one codepoint
--> $DIR/str-as-char.rs:10:19 --> $DIR/str-as-char.rs:10:19
@ -42,7 +42,7 @@ LL | let _: &str = 'a';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let _: &str = "a"; LL | let _: &str = "a";
| ~~~ | ~ ~
error: aborting due to 4 previous errors error: aborting due to 4 previous errors

View file

@ -18,7 +18,7 @@ LL | let v: Vec(&str) = vec!['1', '2'];
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let v: Vec(&str) = vec!["1", '2']; LL | let v: Vec(&str) = vec!["1", '2'];
| ~~~ | ~ ~
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -7,7 +7,7 @@ LL | 'nope'
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | "nope" LL | "nope"
| ~~~~~~ | ~ ~
error: aborting due to 1 previous error error: aborting due to 1 previous error

View file

@ -7,7 +7,7 @@ LL | static c: char = '●●';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | static c: char = "●●"; LL | static c: char = "●●";
| ~~~~ | ~ ~
error: character literal may only contain one codepoint error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-3.rs:5:20 --> $DIR/lex-bad-char-literals-3.rs:5:20
@ -18,7 +18,7 @@ LL | let ch: &str = '●●';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let ch: &str = "●●"; LL | let ch: &str = "●●";
| ~~~~ | ~ ~
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -7,7 +7,7 @@ LL | static c: char = '\x10\x10';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | static c: char = "\x10\x10"; LL | static c: char = "\x10\x10";
| ~~~~~~~~~~ | ~ ~
error: character literal may only contain one codepoint error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-5.rs:5:20 --> $DIR/lex-bad-char-literals-5.rs:5:20
@ -18,7 +18,7 @@ LL | let ch: &str = '\x10\x10';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let ch: &str = "\x10\x10"; LL | let ch: &str = "\x10\x10";
| ~~~~~~~~~~ | ~ ~
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -7,7 +7,7 @@ LL | let x: &str = 'ab';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let x: &str = "ab"; LL | let x: &str = "ab";
| ~~~~ | ~ ~
error: character literal may only contain one codepoint error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-6.rs:4:19 --> $DIR/lex-bad-char-literals-6.rs:4:19
@ -18,7 +18,7 @@ LL | let y: char = 'cd';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let y: char = "cd"; LL | let y: char = "cd";
| ~~~~ | ~ ~
error: character literal may only contain one codepoint error: character literal may only contain one codepoint
--> $DIR/lex-bad-char-literals-6.rs:6:13 --> $DIR/lex-bad-char-literals-6.rs:6:13
@ -29,7 +29,7 @@ LL | let z = 'ef';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let z = "ef"; LL | let z = "ef";
| ~~~~ | ~ ~
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/lex-bad-char-literals-6.rs:13:20 --> $DIR/lex-bad-char-literals-6.rs:13:20

View file

@ -7,7 +7,7 @@ LL | println!(' 1 + 1');
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | println!(" 1 + 1"); LL | println!(" 1 + 1");
| ~~~~~~~~ | ~ ~
error: aborting due to 1 previous error error: aborting due to 1 previous error

View file

@ -7,7 +7,7 @@ LL | let _foo = b'hello\0';
help: if you meant to write a byte string literal, use double quotes help: if you meant to write a byte string literal, use double quotes
| |
LL | let _foo = b"hello\0"; LL | let _foo = b"hello\0";
| ~~~~~~~~~~ | ~~ ~
error: character literal may only contain one codepoint error: character literal may only contain one codepoint
--> $DIR/issue-64732.rs:6:16 --> $DIR/issue-64732.rs:6:16
@ -18,7 +18,7 @@ LL | let _bar = 'hello';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let _bar = "hello"; LL | let _bar = "hello";
| ~~~~~~~ | ~ ~
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View file

@ -12,7 +12,7 @@ LL | let _spade = '♠️';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let _spade = "♠️"; LL | let _spade = "♠️";
| ~~~ | ~ ~
error: character literal may only contain one codepoint error: character literal may only contain one codepoint
--> $DIR/unicode-character-literal.rs:12:14 --> $DIR/unicode-character-literal.rs:12:14
@ -28,7 +28,7 @@ LL | let _s = 'ṩ̂̊';
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | let _s = "ṩ̂̊"; LL | let _s = "ṩ̂̊";
| ~~~ | ~ ~
error: character literal may only contain one codepoint error: character literal may only contain one codepoint
--> $DIR/unicode-character-literal.rs:17:14 --> $DIR/unicode-character-literal.rs:17:14

View file

@ -7,7 +7,7 @@ LL | println!('●●');
help: if you meant to write a `str` literal, use double quotes help: if you meant to write a `str` literal, use double quotes
| |
LL | println!("●●"); LL | println!("●●");
| ~~~~ | ~ ~
error: aborting due to 1 previous error error: aborting due to 1 previous error