Skip to content

Fix: avoid changing drop order #11603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions clippy_lints/src/redundant_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
}
37 changes: 37 additions & 0 deletions tests/ui/redundant_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 12 additions & 1 deletion tests/ui/redundant_locals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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