1
1
use clippy_utils:: diagnostics:: { span_lint_and_sugg, span_lint_hir_and_then} ;
2
+ use clippy_utils:: mir:: { dropped_without_further_use, enclosing_mir, expr_local, local_assignments} ;
2
3
use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context} ;
3
4
use clippy_utils:: sugg:: has_enclosing_paren;
4
5
use clippy_utils:: ty:: { expr_sig, is_copy, peel_mid_ty_refs, ty_sig, variant_of_res} ;
@@ -18,6 +19,7 @@ use rustc_hir::{
18
19
use rustc_index:: bit_set:: BitSet ;
19
20
use rustc_infer:: infer:: TyCtxtInferExt ;
20
21
use rustc_lint:: { LateContext , LateLintPass } ;
22
+ use rustc_middle:: mir:: { Rvalue , StatementKind } ;
21
23
use rustc_middle:: ty:: adjustment:: { Adjust , Adjustment , AutoBorrow , AutoBorrowMutability } ;
22
24
use rustc_middle:: ty:: {
23
25
self , subst:: Subst , Binder , BoundVariableKind , EarlyBinder , FnSig , GenericArgKind , List , ParamTy , PredicateKind ,
@@ -1064,10 +1066,10 @@ fn needless_borrow_impl_arg_position<'tcx>(
1064
1066
// elements are modified each time `check_referent` is called.
1065
1067
let mut substs_with_referent_ty = substs_with_expr_ty. to_vec ( ) ;
1066
1068
1067
- let mut check_referent = |referent| {
1069
+ let mut check_reference_and_referent = |reference , referent| {
1068
1070
let referent_ty = cx. typeck_results ( ) . expr_ty ( referent) ;
1069
1071
1070
- if !is_copy ( cx, referent_ty) {
1072
+ if !( is_copy ( cx, referent_ty) || referent_dropped_without_further_use ( cx . tcx , reference ) ) {
1071
1073
return false ;
1072
1074
}
1073
1075
@@ -1109,7 +1111,7 @@ fn needless_borrow_impl_arg_position<'tcx>(
1109
1111
1110
1112
let mut needless_borrow = false ;
1111
1113
while let ExprKind :: AddrOf ( _, _, referent) = expr. kind {
1112
- if !check_referent ( referent) {
1114
+ if !check_reference_and_referent ( expr , referent) {
1113
1115
break ;
1114
1116
}
1115
1117
expr = referent;
@@ -1137,6 +1139,20 @@ fn has_ref_mut_self_method(cx: &LateContext<'_>, trait_def_id: DefId) -> bool {
1137
1139
} )
1138
1140
}
1139
1141
1142
+ fn referent_dropped_without_further_use ( tcx : TyCtxt < ' _ > , reference : & Expr < ' _ > ) -> bool {
1143
+ let mir = enclosing_mir ( tcx, reference. hir_id ) ;
1144
+ if let Some ( local) = expr_local ( tcx, reference)
1145
+ && let [ location] = * local_assignments ( mir, local) . as_slice ( )
1146
+ && let StatementKind :: Assign ( box ( _, Rvalue :: Ref ( _, _, place) ) ) =
1147
+ mir. basic_blocks [ location. block ] . statements [ location. statement_index ] . kind
1148
+ && !place. has_deref ( )
1149
+ {
1150
+ dropped_without_further_use ( mir, place. local , location) . unwrap_or ( false )
1151
+ } else {
1152
+ false
1153
+ }
1154
+ }
1155
+
1140
1156
// Iteratively replaces `param_ty` with `new_ty` in `substs`, and similarly for each resulting
1141
1157
// projected type that is a type parameter. Returns `false` if replacing the types would have an
1142
1158
// effect on the function signature beyond substituting `new_ty` for `param_ty`.
0 commit comments