@@ -2041,11 +2041,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20412041 if let ValuePairs :: Types ( ty:: error:: ExpectedFound { expected, found } ) =
20422042 trace. values
20432043 {
2044- // If a tuple of length one was expected and the found expression has
2045- // parentheses around it, perhaps the user meant to write `(expr,)` to
2046- // build a tuple (issue #86100)
20472044 match ( expected. kind ( ) , found. kind ( ) ) {
20482045 ( ty:: Tuple ( _) , ty:: Tuple ( _) ) => { }
2046+ // If a tuple of length one was expected and the found expression has
2047+ // parentheses around it, perhaps the user meant to write `(expr,)` to
2048+ // build a tuple (issue #86100)
20492049 ( ty:: Tuple ( _) , _) if expected. tuple_fields ( ) . count ( ) == 1 => {
20502050 if let Ok ( code) = self . tcx . sess ( ) . source_map ( ) . span_to_snippet ( span) {
20512051 if let Some ( code) =
@@ -2060,6 +2060,41 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20602060 }
20612061 }
20622062 }
2063+ // If a character was expected and the found expression is a string literal
2064+ // containing a single character, perhaps the user meant to write `'c'` to
2065+ // specify a character literal (issue #92479)
2066+ ( ty:: Char , ty:: Ref ( _, r, _) ) if r. is_str ( ) => {
2067+ if let Ok ( code) = self . tcx . sess ( ) . source_map ( ) . span_to_snippet ( span) {
2068+ if let Some ( code) =
2069+ code. strip_prefix ( '"' ) . and_then ( |s| s. strip_suffix ( '"' ) )
2070+ {
2071+ if code. chars ( ) . nth ( 1 ) . is_none ( ) {
2072+ err. span_suggestion (
2073+ span,
2074+ "if you meant to write a `char` literal, use single quotes" ,
2075+ format ! ( "'{}'" , code) ,
2076+ Applicability :: MachineApplicable ,
2077+ ) ;
2078+ }
2079+ }
2080+ }
2081+ }
2082+ // If a string was expected and the found expression is a character literal,
2083+ // perhaps the user meant to write `"s"` to specify a string literal.
2084+ ( ty:: Ref ( _, r, _) , ty:: Char ) if r. is_str ( ) => {
2085+ if let Ok ( code) = self . tcx . sess ( ) . source_map ( ) . span_to_snippet ( span) {
2086+ if let Some ( code) =
2087+ code. strip_prefix ( '\'' ) . and_then ( |s| s. strip_suffix ( '\'' ) )
2088+ {
2089+ err. span_suggestion (
2090+ span,
2091+ "if you meant to write a `str` literal, use double quotes" ,
2092+ format ! ( "\" {}\" " , code) ,
2093+ Applicability :: MachineApplicable ,
2094+ ) ;
2095+ }
2096+ }
2097+ }
20632098 _ => { }
20642099 }
20652100 }
0 commit comments