@@ -2045,14 +2045,27 @@ 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 ( _) , _) if expected. tuple_fields ( ) . count ( ) == 1 => {
2048
- err. multipart_suggestion (
2049
- "use a trailing comma to create a tuple with one element" ,
2050
- vec ! [
2051
- ( span. shrink_to_lo( ) , "(" . into( ) ) ,
2052
- ( span. shrink_to_hi( ) , ",)" . into( ) ) ,
2053
- ] ,
2054
- Applicability :: MaybeIncorrect ,
2055
- ) ;
2048
+ if let Ok ( code) = self . tcx . sess ( ) . source_map ( ) . span_to_snippet ( span) {
2049
+ if code. starts_with ( '(' ) && code. ends_with ( ')' ) {
2050
+ let before_close = span. hi ( ) - BytePos :: from_u32 ( 1 ) ;
2051
+
2052
+ err. span_suggestion (
2053
+ span. with_hi ( before_close) . shrink_to_hi ( ) ,
2054
+ "use a trailing comma to create a tuple with one element" ,
2055
+ "," . into ( ) ,
2056
+ Applicability :: MaybeIncorrect ,
2057
+ ) ;
2058
+ } else {
2059
+ err. multipart_suggestion (
2060
+ "use a trailing comma to create a tuple with one element" ,
2061
+ vec ! [
2062
+ ( span. shrink_to_lo( ) , "(" . into( ) ) ,
2063
+ ( span. shrink_to_hi( ) , ",)" . into( ) ) ,
2064
+ ] ,
2065
+ Applicability :: MaybeIncorrect ,
2066
+ ) ;
2067
+ }
2068
+ }
2056
2069
}
2057
2070
// If a character was expected and the found expression is a string literal
2058
2071
// containing a single character, perhaps the user meant to write `'c'` to
0 commit comments