|
1 |
| -use rustc_hir::{Arm, Expr, ExprKind, Node}; |
| 1 | +use rustc_hir::{Arm, Expr, ExprKind, Node, StmtKind}; |
2 | 2 | use rustc_middle::ty;
|
3 | 3 | use rustc_span::sym;
|
4 | 4 |
|
5 | 5 | use crate::{
|
6 | 6 | lints::{
|
7 |
| - DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag, UndroppedManuallyDropsDiag, |
8 |
| - UndroppedManuallyDropsSuggestion, |
| 7 | + DropCopyDiag, DropCopySuggestion, DropRefDiag, ForgetCopyDiag, ForgetRefDiag, |
| 8 | + UndroppedManuallyDropsDiag, UndroppedManuallyDropsSuggestion, |
9 | 9 | },
|
10 | 10 | LateContext, LateLintPass, LintContext,
|
11 | 11 | };
|
@@ -163,10 +163,24 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
|
163 | 163 | );
|
164 | 164 | }
|
165 | 165 | sym::mem_drop if is_copy && !drop_is_single_call_in_arm => {
|
| 166 | + let sugg = if let Some((_, node)) = cx.tcx.hir().parent_iter(expr.hir_id).nth(0) |
| 167 | + && let Node::Stmt(stmt) = node |
| 168 | + && let StmtKind::Semi(e) = stmt.kind |
| 169 | + && e.hir_id == expr.hir_id |
| 170 | + && let Ok(value) = cx.sess().source_map().span_to_snippet(arg.span) |
| 171 | + { |
| 172 | + DropCopySuggestion::Suggestion { |
| 173 | + sugg: expr.span, |
| 174 | + replace: format!("let _ = {}", value), |
| 175 | + } |
| 176 | + } else { |
| 177 | + DropCopySuggestion::Note |
| 178 | + }; |
| 179 | + |
166 | 180 | cx.emit_span_lint(
|
167 | 181 | DROPPING_COPY_TYPES,
|
168 | 182 | expr.span,
|
169 |
| - DropCopyDiag { arg_ty, label: arg.span }, |
| 183 | + DropCopyDiag { arg_ty, label: arg.span, sugg }, |
170 | 184 | );
|
171 | 185 | }
|
172 | 186 | sym::mem_forget if is_copy => {
|
|
0 commit comments