Skip to content
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

significant_drop_in_scrutinee: Trigger lint only if lifetime allows early significant drop #12740

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lrh2000
Copy link
Contributor

@lrh2000 lrh2000 commented Apr 30, 2024

I want to argue that the following code snippet should not trigger significant_drop_in_scrutinee (#8987). The iterator holds a reference to the locked data, so it is expected that the mutex guard must be alive until the entire loop is finished.

use std::sync::Mutex;

fn main() {
    let mutex_vec = Mutex::new(vec![1, 2, 3]);
    for number in mutex_vec.lock().unwrap().iter() {
        dbg!(number);
    }
}

However, the lint should be triggered when we clone the vector. In this case, the iterator does not hold any reference to the locked data.

-     for number in mutex_vec.lock().unwrap().iter() {
+     for number in mutex_vec.lock().unwrap().clone().iter() {

Unfortunately, it seems that regions on the types of local variables are mostly erased (ReErased) in the late lint pass. So it is hard to tell if the final expression has a lifetime relevant to the value with a significant drop.

In this PR, I try to make a best-effort guess based on the function signatures. To avoid false positives, no lint is issued if the result is uncertain. I'm not sure if this is acceptable or not, so any comments are welcome.

Fixes #8987

changelog: [significant_drop_in_scrutinee]: Trigger lint only if lifetime allows early significant drop.

r? @flip1995

@rustbot
Copy link
Collaborator

rustbot commented Apr 30, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @flip1995 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 30, 2024
@bors
Copy link
Collaborator

bors commented May 2, 2024

☔ The latest upstream changes (presumably #12745) made this pull request unmergeable. Please resolve the merge conflicts.

@flip1995
Copy link
Member

flip1995 commented May 2, 2024

I think those are really valuable changes, but won't get to reviewing those for quite a long time, so re-assigning

r? clippy

@rustbot rustbot assigned blyxyas and unassigned flip1995 May 2, 2024
@blyxyas
Copy link
Member

blyxyas commented May 4, 2024

Hi @lrh2000, thanks for the contribution! I've looked at it and nothing triggers my alerts, but we'd prefer if you could split this PR into multiple others. This would help both in the review & merging process and with bisecting future possible bugs.

Thanksies for your contribution! ❤️

@lrh2000
Copy link
Contributor Author

lrh2000 commented May 5, 2024

Hi @lrh2000, thanks for the contribution! I've looked at it and nothing triggers my alerts, but we'd prefer if you could split this PR into multiple others. This would help both in the review & merging process and with bisecting future possible bugs.

Thanksies for your contribution! ❤️

Hi @blyxyas, thank you for your comment and advice. I am willing to do that. I plan to use three PRs to cover those three changelogs. (Maybe we can have more, like one PR for each commit, but I think there's a tradeoff.)

changelog: [significant_drop_in_scrutinee]: Fix false positives due to false drops of place expressions.
changelog: [significant_drop_in_scrutinee]: Trigger lint only if lifetime allows early significant drop.
changelog: [significant_drop_in_scrutinee]: Trigger lint also for scrutinees in while let and if let.

So this PR will only cover the second changelog. I'll temporarily mark it as a draft until the first PR (#12764) is merged. Since all these changes are in one file, they must be merged one at a time, or else there will be a bunch of conflicts to resolve (and break unit tests that depend on multiple fixes or features).

@lrh2000 lrh2000 marked this pull request as draft May 5, 2024 16:57
@bors
Copy link
Collaborator

bors commented May 13, 2024

☔ The latest upstream changes (presumably #12764) made this pull request unmergeable. Please resolve the merge conflicts.

@lrh2000 lrh2000 marked this pull request as ready for review May 13, 2024 17:23
@lrh2000
Copy link
Contributor Author

lrh2000 commented May 13, 2024

OK, now this PR contains only one commit "Trigger lint only if lifetime allows early significant drop". This commit should be atomic enough to be reviewed separately.

I've updated the PR description accordingly.

@bors
Copy link
Collaborator

bors commented May 16, 2024

☔ The latest upstream changes (presumably #12813) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

significant_drop_in_scrutinee is confusing and not very useful for for loops
5 participants