@@ -45,7 +45,7 @@ pub use coercion::can_coerce;
45
45
use fn_ctxt:: FnCtxt ;
46
46
use rustc_data_structures:: unord:: UnordSet ;
47
47
use rustc_errors:: codes:: * ;
48
- use rustc_errors:: { struct_span_code_err, Applicability , ErrorGuaranteed } ;
48
+ use rustc_errors:: { pluralize , struct_span_code_err, Applicability , ErrorGuaranteed } ;
49
49
use rustc_hir as hir;
50
50
use rustc_hir:: def:: { DefKind , Res } ;
51
51
use rustc_hir:: intravisit:: Visitor ;
@@ -421,6 +421,43 @@ fn report_unexpected_variant_res(
421
421
err. multipart_suggestion_verbose ( descr, suggestion, Applicability :: MaybeIncorrect ) ;
422
422
err
423
423
}
424
+ Res :: Def ( DefKind :: Variant , _) if expr. is_none ( ) => {
425
+ let fields = tcx
426
+ . expect_variant_res ( res)
427
+ . fields
428
+ . raw
429
+ . iter ( )
430
+ . map ( |field| field. name . to_ident_string ( ) )
431
+ . collect :: < Vec < _ > > ( ) ;
432
+
433
+ err. span_label ( span, format ! ( "not a {expected}" ) ) ;
434
+
435
+ let span = qpath. span ( ) . shrink_to_hi ( ) . to ( span. shrink_to_hi ( ) ) ;
436
+ err. span_suggestion_verbose (
437
+ span,
438
+ "use the struct variant pattern syntax" ,
439
+ if fields. is_empty ( ) {
440
+ " {}" . to_string ( )
441
+ } else {
442
+ format ! ( " {{ {} }}" , fields. join( ", " ) )
443
+ } ,
444
+ Applicability :: MachineApplicable ,
445
+ ) ;
446
+
447
+ if !fields. is_empty ( ) {
448
+ let len = fields. len ( ) ;
449
+
450
+ let msg = format ! (
451
+ "if you don't care about {these} field{s}, you can explicitly ignore {them}" ,
452
+ these = pluralize!( "this" , len) ,
453
+ s = pluralize!( len) ,
454
+ them = if len == 1 { "it" } else { "them" } ,
455
+ ) ;
456
+ err. span_suggestion_verbose ( span, msg, " { .. }" , Applicability :: MachineApplicable ) ;
457
+ }
458
+
459
+ err
460
+ }
424
461
_ => err. with_span_label ( span, format ! ( "not a {expected}" ) ) ,
425
462
}
426
463
. emit ( )
0 commit comments