@@ -21,11 +21,29 @@ use rustc_target::spec::abi;
2121/// Checks that it is legal to call methods of the trait corresponding
2222/// to `trait_id` (this only cares about the trait, not the specific
2323/// method that is called).
24- pub fn check_legal_trait_for_method_call ( tcx : TyCtxt < ' _ > , span : Span , trait_id : DefId ) {
24+ pub fn check_legal_trait_for_method_call (
25+ tcx : TyCtxt < ' _ > ,
26+ span : Span ,
27+ receiver : Option < Span > ,
28+ trait_id : DefId ,
29+ ) {
2530 if tcx. lang_items ( ) . drop_trait ( ) == Some ( trait_id) {
26- struct_span_err ! ( tcx. sess, span, E0040 , "explicit use of destructor method" )
27- . span_label ( span, "explicit destructor calls not allowed" )
28- . emit ( ) ;
31+ let mut err = struct_span_err ! ( tcx. sess, span, E0040 , "explicit use of destructor method" ) ;
32+ err. span_label ( span, "explicit destructor calls not allowed" ) ;
33+
34+ let snippet = receiver
35+ . and_then ( |s| tcx. sess . source_map ( ) . span_to_snippet ( s) . ok ( ) )
36+ . unwrap_or_default ( ) ;
37+
38+ let ( suggestion, applicability) = if snippet. is_empty ( ) {
39+ ( snippet, Applicability :: Unspecified )
40+ } else {
41+ ( format ! ( "drop({})" , snippet) , Applicability :: MachineApplicable )
42+ } ;
43+
44+ err. span_suggestion ( span, "consider using `drop` function" , suggestion, applicability) ;
45+
46+ err. emit ( ) ;
2947 }
3048}
3149
0 commit comments