@@ -2,9 +2,10 @@ use super::method::probe::ProbeScope;
22use super :: method:: MethodCallee ;
33use super :: { Expectation , FnCtxt , TupleArgumentsFlag } ;
44
5+ use crate :: errors;
56use crate :: type_error_struct;
67use rustc_ast:: util:: parser:: PREC_POSTFIX ;
7- use rustc_errors:: { struct_span_err , Applicability , Diagnostic , ErrorGuaranteed , StashKey } ;
8+ use rustc_errors:: { Applicability , Diagnostic , ErrorGuaranteed , StashKey } ;
89use rustc_hir as hir;
910use rustc_hir:: def:: { self , CtorKind , DefKind , Namespace , Res } ;
1011use rustc_hir:: def_id:: DefId ;
@@ -44,23 +45,15 @@ pub fn check_legal_trait_for_method_call(
4445 trait_id : DefId ,
4546) {
4647 if tcx. lang_items ( ) . drop_trait ( ) == Some ( trait_id) {
47- let mut err = struct_span_err ! ( tcx. sess, span, E0040 , "explicit use of destructor method" ) ;
48- err. span_label ( span, "explicit destructor calls not allowed" ) ;
49-
50- let ( sp, suggestion) = receiver
51- . and_then ( |s| tcx. sess . source_map ( ) . span_to_snippet ( s) . ok ( ) )
52- . filter ( |snippet| !snippet. is_empty ( ) )
53- . map ( |snippet| ( expr_span, format ! ( "drop({snippet})" ) ) )
54- . unwrap_or_else ( || ( span, "drop" . to_string ( ) ) ) ;
55-
56- err. span_suggestion (
57- sp,
58- "consider using `drop` function" ,
59- suggestion,
60- Applicability :: MaybeIncorrect ,
61- ) ;
62-
63- err. emit ( ) ;
48+ let sugg = if let Some ( receiver) = receiver. filter ( |s| !s. is_empty ( ) ) {
49+ errors:: ExplicitDestructorCallSugg :: Snippet {
50+ lo : expr_span. shrink_to_lo ( ) ,
51+ hi : receiver. shrink_to_hi ( ) . to ( expr_span. shrink_to_hi ( ) ) ,
52+ }
53+ } else {
54+ errors:: ExplicitDestructorCallSugg :: Empty ( span)
55+ } ;
56+ tcx. sess . emit_err ( errors:: ExplicitDestructorCall { span, sugg } ) ;
6457 }
6558}
6659
0 commit comments