diff options
| author | León Orell Valerian Liehr <me@fmease.dev> | 2024-05-28 04:15:54 +0200 |
|---|---|---|
| committer | León Orell Valerian Liehr <me@fmease.dev> | 2024-05-28 09:40:02 +0200 |
| commit | 27cdc0df4e73b76466c72ae69ab6545f84089ad2 (patch) | |
| tree | 1d08887d49c450dbc673d039de259a1301cce98d /compiler | |
| parent | 0a59f113629aafb6e5ee55ad04a2d451a11d8466 (diff) | |
| download | rust-27cdc0df4e73b76466c72ae69ab6545f84089ad2.tar.gz rust-27cdc0df4e73b76466c72ae69ab6545f84089ad2.zip | |
Don't suggest turning non-char-literal exprs of ty `char` into string literals
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_infer/src/infer/error_reporting/mod.rs | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index e0894ed31bf..c7affaa17e9 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2064,10 +2064,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { // 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. (ty::Ref(_, r, _), ty::Char) if r.is_str() => { - suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral { - start: span.with_hi(span.lo() + BytePos(1)), - end: span.with_lo(span.hi() - BytePos(1)), - }) + if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) + && code.starts_with("'") + && code.ends_with("'") + { + suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral { + start: span.with_hi(span.lo() + BytePos(1)), + end: span.with_lo(span.hi() - BytePos(1)), + }); + } } // 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` |
