@@ -244,7 +244,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
244
244
}
245
245
}
246
246
247
- let mut compatible_variants = expected_adt
247
+ let compatible_variants: Vec < String > = expected_adt
248
248
. variants
249
249
. iter ( )
250
250
. filter ( |variant| variant. fields . len ( ) == 1 )
@@ -265,19 +265,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
265
265
None
266
266
}
267
267
} )
268
- . peekable ( ) ;
268
+ . collect ( ) ;
269
269
270
- if compatible_variants. peek ( ) . is_some ( ) {
271
- if let Ok ( expr_text) = self . tcx . sess . source_map ( ) . span_to_snippet ( expr. span ) {
272
- let suggestions = compatible_variants. map ( |v| format ! ( "{}({})" , v, expr_text) ) ;
273
- let msg = "try using a variant of the expected enum" ;
274
- err. span_suggestions (
275
- expr. span ,
276
- msg,
277
- suggestions,
278
- Applicability :: MaybeIncorrect ,
279
- ) ;
280
- }
270
+ if let [ variant] = & compatible_variants[ ..] {
271
+ // Just a single matching variant.
272
+ err. multipart_suggestion (
273
+ & format ! ( "try wrapping the expression in `{}`" , variant) ,
274
+ vec ! [
275
+ ( expr. span. shrink_to_lo( ) , format!( "{}(" , variant) ) ,
276
+ ( expr. span. shrink_to_hi( ) , ")" . to_string( ) ) ,
277
+ ] ,
278
+ Applicability :: MaybeIncorrect ,
279
+ ) ;
280
+ } else if compatible_variants. len ( ) > 1 {
281
+ // More than one matching variant.
282
+ err. multipart_suggestions (
283
+ & format ! (
284
+ "try wrapping the expression in a variant of `{}`" ,
285
+ self . tcx. def_path_str( expected_adt. did)
286
+ ) ,
287
+ compatible_variants. into_iter ( ) . map ( |variant| {
288
+ vec ! [
289
+ ( expr. span. shrink_to_lo( ) , format!( "{}(" , variant) ) ,
290
+ ( expr. span. shrink_to_hi( ) , ")" . to_string( ) ) ,
291
+ ]
292
+ } ) ,
293
+ Applicability :: MaybeIncorrect ,
294
+ ) ;
281
295
}
282
296
}
283
297
}
0 commit comments