@@ -21,11 +21,29 @@ use rustc_target::spec::abi;
21
21
/// Checks that it is legal to call methods of the trait corresponding
22
22
/// to `trait_id` (this only cares about the trait, not the specific
23
23
/// 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
+ ) {
25
30
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 ( ) ;
29
47
}
30
48
}
31
49
0 commit comments