Auto merge of #11603 - koka831:fix/11599, r=y21

Fix: avoid changing drop order

Fixes https://github.com/rust-lang/rust-clippy/issues/11599

changelog: [`redundant_locals`] No longer lints which implements Drop trait to avoid reordering
This commit is contained in:
bors 2023-10-03 15:49:28 +00:00
commit b437069f59
3 changed files with 51 additions and 13 deletions

View File

@ -3,7 +3,7 @@
use clippy_utils::ty::needs_ordered_drop;
use rustc_ast::Mutability;
use rustc_hir::def::Res;
use rustc_hir::{BindingAnnotation, ByRef, Expr, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
use rustc_hir::{BindingAnnotation, ByRef, ExprKind, HirId, Local, Node, Pat, PatKind, QPath};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -66,7 +66,7 @@ fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
// the local does not change the effect of assignments to the binding. see #11290
if !affects_assignments(cx, mutability, binding_id, local.hir_id);
// the local does not affect the code's drop behavior
if !affects_drop_behavior(cx, binding_id, local.hir_id, expr);
if !needs_ordered_drop(cx, cx.typeck_results().expr_ty(expr));
// the local is user-controlled
if !in_external_macro(cx.sess(), local.span);
if !is_from_proc_macro(cx, expr);
@ -104,13 +104,3 @@ fn affects_assignments(cx: &LateContext<'_>, mutability: Mutability, bind: HirId
// the binding is mutable and the rebinding is in a different scope than the original binding
mutability == Mutability::Mut && hir.get_enclosing_scope(bind) != hir.get_enclosing_scope(rebind)
}
/// Check if a rebinding of a local affects the code's drop behavior.
fn affects_drop_behavior<'tcx>(cx: &LateContext<'tcx>, bind: HirId, rebind: HirId, rebind_expr: &Expr<'tcx>) -> bool {
let hir = cx.tcx.hir();
// the rebinding is in a different scope than the original binding
// and the type of the binding cares about drop order
hir.get_enclosing_scope(bind) != hir.get_enclosing_scope(rebind)
&& needs_ordered_drop(cx, cx.typeck_results().expr_ty(rebind_expr))
}

View File

@ -118,3 +118,40 @@ macro_rules! rebind {
let x = x;
}
}
struct WithDrop(usize);
impl Drop for WithDrop {
fn drop(&mut self) {}
}
struct InnerDrop(WithDrop);
struct ComposeDrop {
d: WithDrop,
}
struct WithoutDrop(usize);
fn drop_trait() {
let a = WithDrop(1);
let b = WithDrop(2);
let a = a;
}
fn without_drop() {
let a = WithoutDrop(1);
let b = WithoutDrop(2);
let a = a;
}
fn drop_inner() {
let a = InnerDrop(WithDrop(1));
let b = InnerDrop(WithDrop(2));
let a = a;
}
fn drop_compose() {
let a = ComposeDrop { d: WithDrop(1) };
let b = ComposeDrop { d: WithDrop(1) };
let a = a;
}

View File

@ -133,5 +133,16 @@ LL | let x = x;
|
= help: remove the redefinition of `x`
error: aborting due to 13 previous errors
error: redundant redefinition of a binding
--> $DIR/redundant_locals.rs:142:9
|
LL | let a = WithoutDrop(1);
| ^
LL | let b = WithoutDrop(2);
LL | let a = a;
| ^^^^^^^^^^
|
= help: remove the redefinition of `a`
error: aborting due to 14 previous errors