@@ -2045,35 +2045,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
2045
2045
// parentheses around it, perhaps the user meant to write `(expr,)` to
2046
2046
// build a tuple (issue #86100)
2047
2047
( ty:: Tuple ( _) , _) => {
2048
- if let [ expected_tup_elem] =
2049
- expected. tuple_fields ( ) . collect :: < Vec < _ > > ( ) [ ..]
2050
- {
2051
- if same_type_modulo_infer ( expected_tup_elem, found) {
2052
- if let Ok ( code) =
2053
- self . tcx . sess ( ) . source_map ( ) . span_to_snippet ( span)
2054
- {
2055
- if code. starts_with ( '(' ) && code. ends_with ( ')' ) {
2056
- let before_close = span. hi ( ) - BytePos :: from_u32 ( 1 ) ;
2057
-
2058
- err. span_suggestion (
2059
- span. with_hi ( before_close) . shrink_to_hi ( ) ,
2060
- "use a trailing comma to create a tuple with one element" ,
2061
- "," . into ( ) ,
2062
- Applicability :: MaybeIncorrect ,
2063
- ) ;
2064
- } else {
2065
- err. multipart_suggestion (
2066
- "use a trailing comma to create a tuple with one element" ,
2067
- vec ! [
2068
- ( span. shrink_to_lo( ) , "(" . into( ) ) ,
2069
- ( span. shrink_to_hi( ) , ",)" . into( ) ) ,
2070
- ] ,
2071
- Applicability :: MaybeIncorrect ,
2072
- ) ;
2073
- }
2074
- }
2075
- }
2076
- }
2048
+ self . emit_tuple_wrap_err ( & mut err, span, found, expected)
2077
2049
}
2078
2050
// If a character was expected and the found expression is a string literal
2079
2051
// containing a single character, perhaps the user meant to write `'c'` to
@@ -2136,6 +2108,44 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
2136
2108
diag
2137
2109
}
2138
2110
2111
+ fn emit_tuple_wrap_err (
2112
+ & self ,
2113
+ err : & mut DiagnosticBuilder < ' tcx > ,
2114
+ span : Span ,
2115
+ found : Ty < ' tcx > ,
2116
+ expected : Ty < ' tcx > ,
2117
+ ) {
2118
+ let [ expected_tup_elem] = & expected. tuple_fields ( ) . collect :: < Vec < _ > > ( ) [ ..]
2119
+ else { return } ;
2120
+
2121
+ if !same_type_modulo_infer ( expected_tup_elem, found) {
2122
+ return ;
2123
+ }
2124
+
2125
+ let Ok ( code) = self . tcx . sess ( ) . source_map ( ) . span_to_snippet ( span)
2126
+ else { return } ;
2127
+
2128
+ if code. starts_with ( '(' ) && code. ends_with ( ')' ) {
2129
+ let before_close = span. hi ( ) - BytePos :: from_u32 ( 1 ) ;
2130
+
2131
+ err. span_suggestion (
2132
+ span. with_hi ( before_close) . shrink_to_hi ( ) ,
2133
+ "use a trailing comma to create a tuple with one element" ,
2134
+ "," . into ( ) ,
2135
+ Applicability :: MachineApplicable ,
2136
+ ) ;
2137
+ } else {
2138
+ err. multipart_suggestion (
2139
+ "use a trailing comma to create a tuple with one element" ,
2140
+ vec ! [
2141
+ ( span. shrink_to_lo( ) , "(" . into( ) ) ,
2142
+ ( span. shrink_to_hi( ) , ",)" . into( ) ) ,
2143
+ ] ,
2144
+ Applicability :: MachineApplicable ,
2145
+ ) ;
2146
+ }
2147
+ }
2148
+
2139
2149
fn values_str (
2140
2150
& self ,
2141
2151
values : ValuePairs < ' tcx > ,
0 commit comments