diff options
| author | bors <bors@rust-lang.org> | 2023-10-03 15:49:28 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-10-03 15:49:28 +0000 |
| commit | b437069f59798c998309ff2b91eb381539722d8b (patch) | |
| tree | 676070ef4fa4db598d429b8ab624b16cac5d3958 | |
| parent | 29958f076439de7d6d4839d5d90ecae9b46bda80 (diff) | |
| parent | c7152679efd5cd7c433f77eae2109170e4cc34e8 (diff) | |
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
| -rw-r--r-- | clippy_lints/src/redundant_locals.rs | 14 | ||||
| -rw-r--r-- | tests/ui/redundant_locals.rs | 37 | ||||
| -rw-r--r-- | tests/ui/redundant_locals.stderr | 13 |
3 files changed, 51 insertions, 13 deletions
diff --git a/clippy_lints/src/redundant_locals.rs b/clippy_lints/src/redundant_locals.rs index a1ebba6a6a8..a26bb98c5e2 100644 --- a/clippy_lints/src/redundant_locals.rs +++ b/clippy_lints/src/redundant_locals.rs @@ -3,7 +3,7 @@ use clippy_utils::is_from_proc_macro; 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 @@ impl<'tcx> LateLintPass<'tcx> for RedundantLocals { // 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)) -} diff --git a/tests/ui/redundant_locals.rs b/tests/ui/redundant_locals.rs index c5d93e4365d..e81db300f15 100644 --- a/tests/ui/redundant_locals.rs +++ b/tests/ui/redundant_locals.rs @@ -118,3 +118,40 @@ fn macros() { 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; +} diff --git a/tests/ui/redundant_locals.stderr b/tests/ui/redundant_locals.stderr index 13b872e9576..6e9da8fccbd 100644 --- a/tests/ui/redundant_locals.stderr +++ b/tests/ui/redundant_locals.stderr @@ -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 |
