@@ -244,7 +244,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
244244 }
245245 }
246246
247- let mut compatible_variants = expected_adt
247+ let compatible_variants: Vec < String > = expected_adt
248248 . variants
249249 . iter ( )
250250 . filter ( |variant| variant. fields . len ( ) == 1 )
@@ -265,19 +265,33 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
265265 None
266266 }
267267 } )
268- . peekable ( ) ;
268+ . collect ( ) ;
269269
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+ ) ;
281295 }
282296 }
283297 }
0 commit comments