@@ -2041,11 +2041,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
2041
2041
if let ValuePairs :: Types ( ty:: error:: ExpectedFound { expected, found } ) =
2042
2042
trace. values
2043
2043
{
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)
2047
2044
match ( expected. kind ( ) , found. kind ( ) ) {
2048
2045
( 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)
2049
2049
( ty:: Tuple ( _) , _) if expected. tuple_fields ( ) . count ( ) == 1 => {
2050
2050
if let Ok ( code) = self . tcx . sess ( ) . source_map ( ) . span_to_snippet ( span) {
2051
2051
if let Some ( code) =
@@ -2060,6 +2060,41 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
2060
2060
}
2061
2061
}
2062
2062
}
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
+ }
2063
2098
_ => { }
2064
2099
}
2065
2100
}
0 commit comments