-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Conversation
r? @giraffate (rustbot has picked a reviewer for you, use r? to override) |
clippy_lints/src/redundant_locals.rs
Outdated
// the local does not impl Drop trait. see #11599 | ||
let local_ty = cx.typeck_results().node_type(local.hir_id); | ||
if let Some(drop_trait_id) = cx.tcx.lang_items().drop_trait(); | ||
if !cx.tcx.infer_ctxt().build().type_implements_trait( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it won't work if the type is composed of other types implementing Drop
.
struct WithDrop(usize);
impl Drop for WithDrop {..}
struct V(WithDrop);
let first = V(WithDrop(1));
let second = WithDrop(2);
let first = first;
clippy_utils::ty::needs_ordered_drop
would be better.
But also, the lint already has a bit of logic for checking if it affects drop behavior further down and it already checks if the type has a significant dtor that affects drop ordering:
rust-clippy/clippy_lints/src/redundant_locals.rs
Lines 108 to 116 in 81400e2
/// 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)) | |
} |
We should probably just remove the get_enclosing_scope(bind) != get_enclosing_scope(rebind)
check in there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I couldn't figure out why affects_drop_behavior wasn't working. 😅
We should probably just remove the get_enclosing_scope(bind) != get_enclosing_scope(rebind) check from there.
Yes, it works perfectly.
I've addressed them. could you r? @y21 |
Failed to set assignee to
|
Thanks! @bors r=y21 |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Fixes #11599
changelog: [
redundant_locals
] No longer lints which implements Drop trait to avoid reordering