@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
2
2
use clippy_utils:: source:: { indent_of, snippet} ;
3
3
use clippy_utils:: { expr_or_init, get_attr, path_to_local, peel_hir_expr_unary, sym} ;
4
4
use rustc_data_structures:: fx:: { FxHashMap , FxIndexMap } ;
5
- use rustc_errors:: Applicability ;
5
+ use rustc_errors:: { Applicability , Diag } ;
6
6
use rustc_hir:: def:: { DefKind , Res } ;
7
7
use rustc_hir:: intravisit:: { Visitor , walk_expr} ;
8
8
use rustc_hir:: { self as hir, HirId } ;
@@ -80,15 +80,12 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
80
80
continue ;
81
81
}
82
82
let first_bind_ident = apa. first_bind_ident . unwrap ( ) ;
83
- span_lint_and_then (
84
- cx,
85
- SIGNIFICANT_DROP_TIGHTENING ,
86
- first_bind_ident. span ,
87
- "temporary with significant `Drop` can be early dropped" ,
88
- |diag| {
89
- match apa. counter {
90
- 0 | 1 => { } ,
91
- 2 => {
83
+ let emit_sugg = |diag : & mut Diag < ' _ , _ > | match apa. counter {
84
+ 0 | 1 => { } ,
85
+ 2 => {
86
+ if !apa. last_stmt_span . is_dummy ( ) {
87
+ #[ expect( clippy:: if_not_else, reason = "for symmetry with the check above" ) ]
88
+ if !apa. last_method_span . is_dummy ( ) {
92
89
let indent = " " . repeat ( indent_of ( cx, apa. last_stmt_span ) . unwrap_or ( 0 ) ) ;
93
90
let init_method = snippet ( cx, apa. first_method_span , ".." ) ;
94
91
let usage_method = snippet ( cx, apa. last_method_span , ".." ) ;
@@ -100,26 +97,39 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
100
97
} else {
101
98
format ! ( "\n {indent}{init_method}.{usage_method};" )
102
99
} ;
103
-
104
100
diag. multipart_suggestion_verbose (
105
101
"merge the temporary construction with its single usage" ,
106
102
vec ! [ ( apa. first_stmt_span, stmt) , ( apa. last_stmt_span, String :: new( ) ) ] ,
107
103
Applicability :: MaybeIncorrect ,
108
104
) ;
109
- } ,
110
- _ => {
111
- diag. span_suggestion (
112
- apa. last_stmt_span . shrink_to_hi ( ) ,
113
- "drop the temporary after the end of its last usage" ,
114
- format ! (
115
- "\n {}drop({});" ,
116
- " " . repeat( indent_of( cx, apa. last_stmt_span) . unwrap_or( 0 ) ) ,
117
- first_bind_ident
118
- ) ,
119
- Applicability :: MaybeIncorrect ,
120
- ) ;
121
- } ,
105
+ } else {
106
+ diag. help ( "merge the temporary construction with its single usage" ) ;
107
+ diag. span_note ( apa. last_stmt_span , "single usage here" ) ;
108
+ }
109
+ }
110
+ } ,
111
+ _ => {
112
+ if !apa. last_stmt_span . is_dummy ( ) {
113
+ diag. span_suggestion (
114
+ apa. last_stmt_span . shrink_to_hi ( ) ,
115
+ "drop the temporary after the end of its last usage" ,
116
+ format ! (
117
+ "\n {}drop({});" ,
118
+ " " . repeat( indent_of( cx, apa. last_stmt_span) . unwrap_or( 0 ) ) ,
119
+ first_bind_ident
120
+ ) ,
121
+ Applicability :: MaybeIncorrect ,
122
+ ) ;
122
123
}
124
+ } ,
125
+ } ;
126
+ span_lint_and_then (
127
+ cx,
128
+ SIGNIFICANT_DROP_TIGHTENING ,
129
+ first_bind_ident. span ,
130
+ "temporary with significant `Drop` can be early dropped" ,
131
+ |diag| {
132
+ emit_sugg ( diag) ;
123
133
diag. note ( "this might lead to unnecessary resource contention" ) ;
124
134
diag. span_label (
125
135
apa. first_block_span ,
0 commit comments