@@ -45,7 +45,7 @@ pub use coercion::can_coerce;
4545use fn_ctxt:: FnCtxt ;
4646use rustc_data_structures:: unord:: UnordSet ;
4747use rustc_errors:: codes:: * ;
48- use rustc_errors:: { struct_span_code_err, Applicability , ErrorGuaranteed } ;
48+ use rustc_errors:: { pluralize , struct_span_code_err, Applicability , ErrorGuaranteed } ;
4949use rustc_hir as hir;
5050use rustc_hir:: def:: { DefKind , Res } ;
5151use rustc_hir:: intravisit:: Visitor ;
@@ -421,6 +421,36 @@ fn report_unexpected_variant_res(
421421 err. multipart_suggestion_verbose ( descr, suggestion, Applicability :: MaybeIncorrect ) ;
422422 err
423423 }
424+ Res :: Def ( DefKind :: Variant , _) if expr. is_none ( ) => {
425+ let fields = & tcx. expect_variant_res ( res) . fields . raw ;
426+
427+ let span = qpath. span ( ) . shrink_to_hi ( ) . to ( span. shrink_to_hi ( ) ) ;
428+ let ( msg, sugg) = if fields. is_empty ( ) {
429+ ( "use the struct variant pattern syntax" . to_string ( ) , " {}" . to_string ( ) )
430+ } else {
431+ let msg = format ! (
432+ "the struct variant's field{s} {are} being ignored" ,
433+ s = pluralize!( fields. len( ) ) ,
434+ are = pluralize!( "is" , fields. len( ) )
435+ ) ;
436+ let fields = fields
437+ . iter ( )
438+ . map ( |field| format ! ( "{}: _" , field. name. to_ident_string( ) ) )
439+ . collect :: < Vec < _ > > ( )
440+ . join ( ", " ) ;
441+ let sugg = format ! ( " {{ /* {} */ }}" , fields, ) ;
442+ ( msg, sugg)
443+ } ;
444+
445+ err. span_label ( span, format ! ( "not a {expected}" ) ) ;
446+ err. span_suggestion_verbose (
447+ qpath. span ( ) . shrink_to_hi ( ) . to ( span. shrink_to_hi ( ) ) ,
448+ msg,
449+ sugg,
450+ Applicability :: HasPlaceholders ,
451+ ) ;
452+ err
453+ }
424454 _ => err. with_span_label ( span, format ! ( "not a {expected}" ) ) ,
425455 }
426456 . emit ( )
0 commit comments